Jump to content


Photo

AJAX/PHP Instant Messaging System


  • Please log in to reply
24 replies to this topic

#1 JoeyMagz

JoeyMagz

    Young Padawan

  • Members
  • Pip
  • 79 posts
  • Gender:Male
  • Location:Chesapeake, VA

Posted 09 August 2008 - 05:58 AM

*EDIT*
I know that I said I was going to be updating this a lot more, and I am, but I'm helping Dan out with some stuff right now. Once I complete that, I'm going to be dedicating any free time I get to this project. It may be a few weeks, but trust me, I will be getting back to this because I really want to. Also, since this first post is getting pretty long, I will be posting all new additions in the last post that I made, unless of course I'm modifying one of the scripts on this page.

Also, as a wise man once told me, "Don't try to re-invent the wheel." With that in mind, I'm clearing the ajax.js file so that I don't need to program effects later on, and I will instead use jQuery for our AJAX needs. Starting with the next update, the ajax.js file will contain jQuery calls. So, don't get all scared when you look through this post and see the ajax.js file is gone, it will be re-posted on the next page once it has been updated.
*END EDIT*

Ok, this is just an idea for a website messaging system, not a private messaging system. I wanted to make live messages to individual users or even multiple users sort of like an internet messaging system right on a webpage. It would follow the same principles as a private messaging system with database set-up, etc. The only difference is that there would be an ajax script checking for new messages every second or couple of seconds. The problem is, this may use a lot of system resources on the server. The cool thing about this would be that you would be able to make an instant messaging system for people browsing your website using php/ajax. I'm not sure if there is one already, but it seems like a pretty good idea.

Database set-up:

Members Table:
id - int primary key auto_increment.
username - varchar 15.
password - varchar 32. will be a md5 and sha1 hash of the password then the username.
e-mail - varchar 35.
admin - tinyint. will either be a value of 1 (admin) or the default, 0 (regular member)
last_ip - varchar 15. will store the last ip address person logged in with.

Buddy List:
uid - User ID from members table. int. primary key.
buddy - Buddies id. int.
blocked - tinyint.

Messages:
id - message id. int primary key auto_increment.
from - users id. int.
to - buddies id. int.
message - tinytext. Max message size of 255 characters. Much better than previous blob (65,535 characters :angrylooking:)
time - timestamp CURRENT_TIMESTAMP.

The smartest way to work this is have a cron job or windows task set up to remove all messages 10 minutes or older from the database to save space. After you have the database set-up its simply a matter of having ajax set-up to run a php script to check for new messages every second.

Also, you would be able to make this look any way you wanted it to and you can pretty much make it do anything you wanted it to, right down to playing a sound when you receive a message. People would possibly even be able to play their own sounds when they received a message.

config.php
<?php

header("Cache-Control: Private"); // makes sure information is cached for only current user
session_start(); // starts or resumes session

$_dbUsr = "username"; // database username.
$_dbPass = "password"; // database password.
$_db = "database"; // database you're using.
$_dbHost = "localhost"; // most likely using localhost.		

$_conn = @mysql_connect($_dbHost, $_dbUsr, $_dbPass);	
if(!$_conn) { // This executes the connect code and checks it for errors.		
	print "Error: Problem connecting to database server. Please check your config files.";	
}	
$_select = @mysql_select_db($_db, $_conn);	
if(!$_select) { // Executes the select database code and checks it for errors.		
	print "Error: Problem selecting database. Please check that database exists and is linked properly in config files.";
}	
?>

register.php
<?php
require("config.php");

$_user = mysql_real_escape_string($_POST['user']);
$_pass = md5(sha1($_POST['pass'].$_POST['user']));
$_email = mysql_real_escape_string($_POST['email']);

$memtab = "name of your members table";
$_ip = $_SERVER['REMOTE_ADDR'];

if ((!$_user) || (!$_pass) || (!$_email)) {
	print "You must fill in all fields before clicking the register button. Please go back and try again.";
} else {
	$sel = @mysql_query("SELECT * FROM $memtab WHERE username = '$_user'");
	if($sel) {
		print "A user with that username exists. Please go back and choose a new username.";
	} else { 
		$ins = @mysql_query("INSERT INTO $memtab (username, password, email, last_ip) VALUES(\"$_user\", \"$_pass\", \"$_email\", \"$_ip\")");

		if ($ins) {
			print "Registration complete. You may now begin using 'title of instant messenger'.";
		} else {
			print "Error: Could not create user. Please go back and try again.";
		}
	}
}
?>

login.php
<?php	
require("config.php");		
$memtab = "name of your members table";	
$_user = mysql_real_escape_string($_GET['user']);	
$_pass = md5(sha1($_GET['pass'].$_GET['user']));		
$_sel = @mysql_query("SELECT * FROM $memtab WHERE username=$_user AND password=$_pass");  
while($r = @mysql_fetch_array($_sel)) {	
	$_SESSION['user'] = $r["username"]; // There is going to be a session_start() function on top of index.php which will be the main page.
	$admin = $r["admin"];
}
if($admin) { // If the admin column has a value of 1
	print "<script language='text/javascript' src='ajax.js'>buddyLoad($_SESSION['user'],1);</script>";
} else {
	print "<script language='text/javascript' src='ajax.js'>buddyLoad($_SESSION['user'],0);</script>";
}
?>

sendim.php
<?php
if(!$_GET['m'] || !$_GET['from'] || !$_GET['to']) {
	print "No message was sent.";
} else {
	$user = mysql_real_escape_string($_GET['from']);
	$buddy = mysql_real_escape_string($_GET['to']);
	$msg = mysql_real_escape_string($_GET['m']);

	$ins = @mysql_query("INSERT INTO messages ('from', 'to', 'message') VALUES(\"$user\", \"$buddy\", \"$msg\")"); /* The reason for only inserting the from, to, and message fields is because the message id should be set to auto_increment and the time should default to CURRENT_TIMESTAMP. */
	
	if(!$ins) {
		print "Message could not be sent. The server may be down.";
	}
}
?>

checkims.php
<?php
require("config.php");
require("style.php"); // This will be the script that styles the way an instant message looks.

$user = mysql_real_escape_string($_GET['user']);
$buddy = mysql_real_escape_string($_GET['buddy']);
$time = $_GET['time'];

$sel = @mysql_query("SELECT * FROM messages WHERE to='$user' AND from='$buddy' ORDER BY time DESC LIMIT 1");
while($r = @mysql_fetch_array($sel)) {
	$id = $r["id"];
	$message = $r["message"];
	$dbtime = $r["time"];
}

if ($time == $dbtime) { 
	return 
} else { 
	style($user,$buddy,$dbtime,$message); // Style the instant message and display it.
}

?>

More stuff to come... Be sure to add your own input.

WHAT'S NEW
v.0.5.5 - Minor config.php and ajax.js updates.
v.0.5.3 - Javascript update. Corrected some errors in the ajax.js file.
v.0.5.2 - PHP update. Completed checkims.php.
v.0.5.1 - Security and PHP update. Changed all htmlspecialchars and stripslashes to just mysql_real_escape_string. Cleaned up a lot of the code in the php files.
v.0.5 - PHP update. Added register.php.
v.0.4.1 - Security, PHP, and Database update. Added last_ip column to members table. Updated the SQL statement used in checkims.php. Now there is a htmlspecialchars() function added to all variables which call upon $_GET strings.
v.0.4 - PHP update. Added checkims.php.
v.0.3.1 - Database and Security update. Added time column to messages table. Changed password to md5 and sha1 hash of password + username.
v.0.3 - PHP update. Added config.php, login.php, and sendim.php files.
v.0.2.1 - Javascript update. Added checkIMs() and Timer() functions to ajax.js file.
v.0.2 - Javascript update. Created ajax.js file.
v.0.1 - Project started. Databases created.

Edited by JoeyMagz, 30 January 2011 - 12:42 AM.


#2 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 11 August 2008 - 11:46 AM

My suggestion: if you're going to post this in the tutorials section, you should comment your code well enough that people can understand where you're going with it :(

Otherwise, i can move this to the PHP/ASP help section and we can turn it into a huge debate about how to best go about making one of these :)

#3 JoeyMagz

JoeyMagz

    Young Padawan

  • Members
  • Pip
  • 79 posts
  • Gender:Male
  • Location:Chesapeake, VA

Posted 11 August 2008 - 05:33 PM

yea i think that would be best...because this is a work in progress right now...i was planning on commenting as i went along.. =D I'll add some comments to the javascript stuff now though, but i still think moving it to the help section would be best.

#4 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 11 August 2008 - 05:40 PM

Moved :angrylooking:

#5 Spide®™

Spide®™

    P2L Jedi

  • Members
  • PipPipPip
  • 767 posts
  • Gender:Male
  • Location:UK England

Posted 11 August 2008 - 07:39 PM

This sounds great, I don't know the first thing about scripting / codeing
But i had a brain wave ( or maybe not ), when reading your post

Say if i Designed a website, and it had members,
Could you have this setup like,

If i was doing live help ( Tutorials section on my website ) so i can help ppl around Photoshop
But have the code Email my members when i was doing live help, to tell them i'am online, and the messanger is open

Allso have a admin login, so the admin can open and close the messanger ?

Maybe Webcam ?

Probely a silly idea
but you never know lol

Allso can the GFX be made to how anybody wants it to look like ?

Edited by Designe®™, 11 August 2008 - 07:40 PM.


#6 023-jimmy

023-jimmy

    Young Padawan

  • Members
  • Pip
  • 44 posts

Posted 13 August 2008 - 04:42 AM

Sounds interresting. I will be following your progress through this topic :).

I have been working with a combination of ajax and php too lately, but still discovering how to really get the most out of it. Reading through your code gives me a better understanding of how to use the code.

EDIT

I see you call the function your creating in the function. Don't know how to describe, but I mean this piece of code:

function Timer() {
	setInterval("checkIMs()", 1000);
	Timer();
}

The function your creating is Timer(); and inside that you call the function Timer();. This doesn't look logical?

Edited by 023-jimmy, 13 August 2008 - 12:31 PM.


#7 JoeyMagz

JoeyMagz

    Young Padawan

  • Members
  • Pip
  • 79 posts
  • Gender:Male
  • Location:Chesapeake, VA

Posted 13 August 2008 - 07:40 PM

Oops. Yea you're right jimmy thank you =D I'm still actually learning javascript as I program this! ^_^ I just read up on setTimeout and setInterval. It seems my code would have been correct if I used setTimeout, but since setInterval calls functions repeatedly I don't need to call the Timer() function again. Thanks again!

To answer Designe:
E-Mail members - I'll add some functions for that in the php files. Thanks for the idea. ^_^

Admin Open/Close Messenger - Another good idea, and pretty easy to do, will definitely add that to php functions.

Graphics - Since this will be using a webpage window as the actual messenger, the owner/designer will be able to customize every aspect of the way it looks. :)

Edit

Web Cam - After reading more into the webcam API, I need to create an ActiveX dll that will capture the footage from the webcam and then send that footage to the receiving users webpage. I don't think it will be too hard to do, but that's going to have to wait until after this first version is completed ^_^ lol That's going to require some real coding in vb.net or may even require c++. I'll start looking into that when I get home, as I'm at the base right now (I'm in the coast guard.) and I don't want to start browsing all kinds of websites because my internet is monitored. :biggrin:

Another Edit

Designe - I just checked out your deviant page. Dude, I can't even believe someone is that crazy with graphics. I think I'll go out on a limb here and just say that you're graphics skills are amazing. :blink:

Edited by JoeyMagz, 13 August 2008 - 09:28 PM.


#8 023-jimmy

023-jimmy

    Young Padawan

  • Members
  • Pip
  • 44 posts

Posted 14 August 2008 - 06:39 PM

Have you already started at an online example? Or you just have the javascript code?

#9 Arsenal19

Arsenal19

    Young Padawan

  • Members
  • Pip
  • 41 posts

Posted 14 August 2008 - 09:22 PM

This sounds like a great project.

If you need any help with the javascript or Ajax stuff let me know , i can prolly help.

Arsenal19

#10 JoeyMagz

JoeyMagz

    Young Padawan

  • Members
  • Pip
  • 79 posts
  • Gender:Male
  • Location:Chesapeake, VA

Posted 15 August 2008 - 09:21 AM

No, I haven't started anything yet. Basically, whenever I get time I come onto pixel2life forums and post some more functions and stuff.. lol I'm hoping that when I finish all of the features, someone will actually put it on their website for everyone to see with their own design and maybe some of their own scripting features. It'll be nice to see this baby grow, in a sense.

Thanks Arsenal :hitit:

#11 023-jimmy

023-jimmy

    Young Padawan

  • Members
  • Pip
  • 44 posts

Posted 15 August 2008 - 12:58 PM

Well, when you're finished with it. I could put it on my website, for a testing periode. See how it will handle when lots of people use it. But before that happens I need to finish off my website, which will be a community for lovers of the harder styles music. But still have lots of work to do.

#12 dotbart

dotbart

    Young Padawan

  • Members
  • Pip
  • 141 posts
  • Gender:Male
  • Location:Diepenbeek
  • Interests:Webdesign, Webdeveloppement, DJ, ...

Posted 20 August 2008 - 09:02 AM

Hey!
I like the ambition, I made the exact same as you about a year ago (well, maybe technology, database,... might differ but it's still a PHP/AJAX chatting script)

It features multiple conversations and uses XML to transfer the messages.

you can find the working script here
If you want, PM me and I can send you the code. If you need any advice, PM me aswell! :-)

Oh and by the way, don't expect it to be safe/bug-free/secure.... it's a test project :)



B

#13 JoeyMagz

JoeyMagz

    Young Padawan

  • Members
  • Pip
  • 79 posts
  • Gender:Male
  • Location:Chesapeake, VA

Posted 20 August 2008 - 05:41 PM

yours is more of a chatroom type set up. I'm making more of a instant messaging system set up. Where you have a screen name and when people IM you, it opens a new window on your screen and shows the IM. Yours is still cool though =D

#14 Spide®™

Spide®™

    P2L Jedi

  • Members
  • PipPipPip
  • 767 posts
  • Gender:Male
  • Location:UK England

Posted 21 August 2008 - 11:57 AM

Thanks Joey, hope you get all your plans done
It would be great to see this

Oh yeah i have another question lol
About the skinning side of things, will you have to code in the gfx like CSS ? or will thay be a simple skin editor
like drag and drop, or simler, with not much coding to do, for ppl like me, who don't understand code lol

#15 JoeyMagz

JoeyMagz

    Young Padawan

  • Members
  • Pip
  • 79 posts
  • Gender:Male
  • Location:Chesapeake, VA

Posted 21 August 2008 - 01:05 PM

It's going to be CSS for the graphics.

#16 023-jimmy

023-jimmy

    Young Padawan

  • Members
  • Pip
  • 44 posts

Posted 24 August 2008 - 12:17 PM

Just noticed something else. You've created the function Timer();. But you need to call the function too.

So:
function Timer() {	
	setInterval("checkIMs()", 1000);	
}

Will be:
function Timer() {	
	setInterval("checkIMs()", 1000);	
}
Timer();


#17 JoeyMagz

JoeyMagz

    Young Padawan

  • Members
  • Pip
  • 79 posts
  • Gender:Male
  • Location:Chesapeake, VA

Posted 26 August 2008 - 10:01 PM

That's going to be in the actual instant message window jimmy. I don't want the script to be calling for instant message updates on the buddy list window, that's going to be another function which actually checks for new instant messages and opens up another window. BTW, I'm going to be updating this some more soon, it's just that I'm in the coast guard and I'm trying to get all my paperwork and practicals done so I can leave for "A" school. I'll be updating more once I'm on the wait list, which should be by september 4th or 5th. Then all of my concentration will be on getting this thing done and polished.

Edited by JoeyMagz, 25 September 2008 - 12:37 AM.


#18 JoeyMagz

JoeyMagz

    Young Padawan

  • Members
  • Pip
  • 79 posts
  • Gender:Male
  • Location:Chesapeake, VA

Posted 25 September 2008 - 12:37 AM

Just added the register.php page to this. I have some more code to add when I get home on Friday. Also, in case you were wondering, I'm going to be adding some ajax code that checks for missing fields or duplicate username and e-mails in the javascript file, the stuff in register.php is just a double check to see if the javascript missed anything. At home right now I have a completed checkims.php as well as another couple scripts.

#19 UnrealMedia

UnrealMedia

    Young Padawan

  • Members
  • Pip
  • 72 posts

Posted 25 September 2008 - 12:44 PM

Very nice. I wish you would comment it though. We could learn something from it then. I hate being a copy and paste whore :)

#20 023-jimmy

023-jimmy

    Young Padawan

  • Members
  • Pip
  • 44 posts

Posted 09 October 2008 - 09:42 AM

Really curious about your progress in this. Are you still busy with this? If so, show us some updates :offtopic:!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users