Jump to content
xisto Community
Sign in to follow this  
iGuest

Thorough instant message system V0.9 [UPDATED to work]

Recommended Posts

I have made an instant message system which is quite complicated. To implement this you may need a reasonable knowledge of php, you will also need a login system that has parsed the username to a variable $username. For this specific example to work best you also need to be able to assign $user_level to a value more than 2 if an admin is logged in.

MYSQL tables are at the bottom.

FIRST. Create a file, name it formtemplates.php and give it the following content.

If you use this, Please link back to my site at
http://forums.xisto.com/no_longer_exists/, as this system took me a lot of time to make

<?if(isset($_SESSION['username'])) {$username = $_SESSION['username'];} else {$username = Guest;}$user = $_GET['user'];$priv_query = mysql_query("SELECT * FROM users WHERE username='$user'");$priv_num = mysql_num_rows($priv_query);if($priv_num == 1) {$value = "value="1" CHECKED";}$send_message = <<<HTML<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><form action="?page=message&a=send&b=1" method="post" name="" id="">  <table width="50%" border="0" align="center" cellpadding="4" cellspacing="0">	<tr>       <td width="22%">To</td>      <td width="78%"><input name="to_user" type="text" id="to" value="$user"></td>    </tr>	<tr>   <td>From</td>   <td> $username </td>	</tr>    <tr>       <td>Message</td>      <td><textarea name="message" id="message" rows="5" cols="50"> </textarea></td>    </tr>    <tr>       <td>Private</td>      <td><input type="checkbox" name="private" $value></td>    </tr>    <tr>       <td> </td><input name="from" type="hidden" id="from" value="$username">      <td><input type="submit" name="Submit" value="Submit"></td>    </tr>  </table></form>HTML;?>


SECOND. Include the following at the top of the content. This will produce a notice at the top which should stay for one minute before dissapearing again.

<?include 'db.php';///connect to database - file content is shown near the bottominclude 'formtemplates.php';///include the send form as shown above./////these variables are explained at the bottom :)$page = $_GET['page'];$a = $_GET['a'];$b = $_GET['b'];$user = $_GET['user'];if (isset($_SESSION['username'])) {/*-------------------------------------------------------------------------------------*/print "<i>";///you will need to adjust these to reflect your log on script - mine uses sessions with these variables$last_login = $_SESSION['last_login'];$username = $_SESSION['username'];$firstname = $_SESSION['first_name'];$userid = $_SESSION['userid'];///retrieve the latest known time the user logged in$last_login_ = mysql_query("SELECT last_login,im_update FROM users WHERE userid=$userid LIMIT 1") or die("MySQL Error: " . mysql_error());while($last = mysql_fetch_array($last_login_)) {$last_login = $last['last_login'];$im_update = $last['im_update'];}/////if ($im_update == 0) {/* to save complexity ive removed this bit - which links to the user table to allow users to decide if they wish to be updated*///get all messages for the user where the message date is after the last login time$sql_messagenew = mysql_query("SELECT * FROM message WHERE to_user='$username' AND date >'$last_login' ORDER BY id desc");$new_row = mysql_num_rows($sql_messagenew);//if there is more than 0 new records then display them - how you wish, the following way is one possibilityif ($new_row !== 0) {print "$firstname You have ($new_row) new Instant messages:";print "<table width="90%" border="1" align="center" cellpadding="4" cellspacing="0">";print "	<tr> ";print "      <td width="15%">To</td>";print "      <td width="15%">From</td>";print "      <td width="50%">Message</td>";print "      <td width="10%">Date</td>";print "      <td width="10%">Type</td>";print "    </tr>  ";/// display each row that matches the querywhile($row=mysql_fetch_array($sql_messagenew)) {print '<tr><td valign="top"><a href="?page=users&a='.$row['to_user'].' ">'.$row['to_user'].'</a></td><td valign="top"><a href="?page=users&a='.$row['from'].' ">'.$row['from'].'</a></td><td valign="top">'.$row['message'].'</td><td valign="top">'.$row['date'].'</td><td valign="top">'.$row['type'].'</td></tr>  ';}/*---*/print "</table>";////end table///print "<br /><br />";} else {//print "$firstname You Have (0) New Instant Messages.";//print "<br /><br />";}//get the current date      $date=time();$date = $date+18000;//adjust to gmt	$sql = mysql_query("UPDATE users SET last_login=FROM_UNIXTIME($date) WHERE userid='$userid'");//update the last log in time//} this is the close tag for choosing to revieve the updateprint "</i>    	n";/*----------------------------------------------------------------------------*/}?>




THIRD. The following is the script to send IM's and to view them to be placed in the body of the page. To access this you will need to go to file.php?page=message where 'file.php' can be a *.php file with any name.


<?if ($page == message) {/*----------------------------------Message---------------------------------*/Print "Instant Message System<br />";Print "<a href=?page=users>- Member List</a><br />";/*when page = users my site lists the active users. This is not explained in this tutorial - but can be done fairly quickly */if ($a == send) {///the section that allows messages to be sent...if ($b == 1) {///insert data to database      $date=time();$date = $date+18000;///set the time$date = $date + 60///set a minute delay to leave the IM's showing on the screen after page refreshes$to_user = $_POST['to_user'];$from = $_POST['from'];$message = $_POST['message'];$private = $_POST['private'];$type = "IM";///set the var's/* if private - my script doubles as a comment system, where it is not private i can link it to news posts etc e.g. send comments to news_1 and retrieve them when to_user isnt in the user table*/if(!isset($private)) {$private = 0;} else {$private = 1;}//some data validationif (empty($to_user) || empty ($from) || empty($message) || ($message == " ") || empty($date)) { $message = 'You Need To Complete All Required Fields';  	} else {//everything ok, strip slashes, link links and submit to db$message = stripslashes($message);$message = eregi_replace(    "(http|https|ftp)://([[:alnum:]/n+-=%&:_.~?]+[#[:alnum:]+]*)",    "<a href="1://2" target="_blank">1://2</a>",    $message);      mysql_query("INSERT INTO `message` ( `id` , `to_user` , `from` , `message` , `date` , `private` , `type` ) VALUES('','{$to_user}','{$from}','{$message}',FROM_UNIXTIME($date),'{$private}','{$type}')");  $message = "<br />Successfully Posted Message";}}print $message;print "<br /><br />";$change = read;print "<a href="?page=$page&a=$change">$change Message</a><br>	n";print "<br>    n";print $send_message;} else {///section = read...$a = read;print "<br/>";$change = send;print "<a href="?page=$page&a=$change">$change Message</a><br>	n";print "<br>    n";/*-------*////display them...print "<table width="90%" border="1" align="center" cellpadding="4" cellspacing="0">";print "	<tr> ";print "      <td width="15%">To</td>";print "      <td width="15%">From</td>";print "      <td width="50%">Message</td>";print "      <td width="10%">Date</td>";print "      <td width="10%">Type</td>";print "    </tr>  ";$user = $_GET['user'];///if not to a user, display all the public messagesif (empty($user)) {////////////*---*/$message_query = mysql_query("SELECT * FROM message WHERE private='0' order by id desc");$row_num_ = mysql_num_rows($message_query);while($row=mysql_fetch_array($message_query)) {print '<tr><td valign="top"><a href="?page=users&a='.$row['to_user'].' ">'.$row['to_user'].'</a></td><td valign="top"><a href="?page=users&a='.$row['from'].' ">'.$row['from'].'</a></td><td valign="top">'.$row['message'].'</td><td valign="top">'.$row['date'].'</td><td valign="top">'.$row['type'].'</td></tr>  ';}/*---*/} else {///the user isnt empty - is it to a website user, or to a news post?/*----------------------------*/$username = $_SESSION['username'];if ($user !== $username) {///if the user has admin settings allow him to view the private messages of other usersif($_SESSION['user_level'] > 2){/*---*/////////////////*------0--------*/////////$row_num = $row_num_0;$message_query_0 = mysql_query("SELECT * FROM message WHERE to_user='$user' ORDER BY id desc LIMIT 10");$row_num_0 = mysql_num_rows($message_query_0);while($row=mysql_fetch_array($message_query_0)) {print '<tr><td valign="top"><a href="?page=users&a='.$row['to_user'].' ">'.$row['to_user'].'</a></td><td valign="top"><a href="?page=users&a='.$row['from'].' ">'.$row['from'].'</a></td><td valign="top">'.$row['message'].'</td><td valign="top">'.$row['date'].'</td><td valign="top">'.$row['type'].'</td></tr>  ';}/*---*/} else {/*--user doesnt have admin settings - only able to view public messages to other users-*/////////////////*------1--------*/////////$row_num = $row_num_1;$message_query_1 = mysql_query("SELECT * FROM message WHERE private='0' AND to_user='$user' ORDER BY id desc LIMIT 10");$row_num_1 = mysql_num_rows($message_query_1);while($row=mysql_fetch_array($message_query_1)) {print '<tr><td valign="top"><a href="?page=users&a='.$row['to_user'].' ">'.$row['to_user'].'</a></td><td valign="top"><a href="?page=users&a='.$row['from'].' ">'.$row['from'].'</a></td><td valign="top">'.$row['message'].'</td><td valign="top">'.$row['date'].'</td><td valign="top">'.$row['type'].'</td></tr>  ';}/*---*/}} else {///user is the same as the one for which messages are requested - so show all their private messages/*---*/////////////////*------2--------*/////////$row_num = $row_num_2;$message_query_2 = mysql_query("SELECT * FROM message WHERE to_user='$user' ORDER BY id desc LIMIT 10");$row_num_2 = mysql_num_rows($message_query_2);while($row=mysql_fetch_array($message_query_2)) {print '<tr><td valign="top"><a href="?page=users&a='.$row['to_user'].' ">'.$row['to_user'].'</a></td><td valign="top"><a href="?page=users&a='.$row['from'].' ">'.$row['from'].'</a></td><td valign="top">'.$row['message'].'</td><td valign="top">'.$row['date'].'</td><td valign="top">'.$row['type'].'</td></tr>  ';}/*---*/}/*----------------------------*////////}print "</table>";////end table////*-------*/}///if a variable is for all... then show all ones available$c = $_GET['c'];if($c == all) {print "<table width="90%" border="1" align="center" cellpadding="4" cellspacing="0">";print "	<tr> ";print "      <td width="15%">To</td>";print "      <td width="15%">From</td>";print "      <td width="50%">Message</td>";print "      <td width="10%">Date</td>";print "      <td width="10%">Type</td>";print "    </tr>  ";///if admin show all messages, if not exclude - ive taken out pagination as there is another tut, and i cba :)if($_SESSION['user_level'] > 2){/*-----------------------------------------------------------------------------------------------------------------------*/////////////////*------3--------*/////////$row_num = $row_num_3;$message_query_3 = mysql_query("SELECT * FROM message ORDER BY id desc");$row_num_3 = mysql_num_rows($message_query_3);while($row=mysql_fetch_array($message_query_3)) {print '<tr><td valign="top"><a href="?page=users&a='.$row['to_user'].' ">'.$row['to_user'].'</a></td><td valign="top"><a href="?page=users&a='.$row['from'].' ">'.$row['from'].'</a></td><td valign="top">'.$row['message'].'</td><td valign="top">'.$row['date'].'</td><td valign="top">'.$row['type'].'</td></tr>  ';}/*-----------------------------------------------------------------------------------------------------------------------*/} else {  echo "Your userlevel does not authorise you to view this page";}/*-----------------------------------------------------------------------------------------------------------------------*/print "</table>";////end table///}/*----------------------------------End Message------------------------------*/}?>


The MYSQL tables i have used here are 'users' and 'message'

Message is shown below
CREATE TABLE `message` (  `id` int(11) NOT NULL auto_increment,  `to_user` varchar(30) NOT NULL default '',  `from` varchar(30) NOT NULL default 'Guest',  `message` text NOT NULL,  `date` datetime NOT NULL default '0000-00-00 00:00:00',  `private` enum('0','1') default '0',  `type` varchar(10) default 'IM',  PRIMARY KEY  (`id`)) TYPE=MyISAM AUTO_INCREMENT=33;

Users is shown below
CREATE TABLE `users` (  `userid` int(25) NOT NULL auto_increment,  `first_name` varchar(25) NOT NULL default '',  `last_name` varchar(25) NOT NULL default '',  `email_address` varchar(35) NOT NULL default '',  `mailing_list` enum('1','2') NOT NULL default '1',  `age` smallint(6) default NULL,  `location` text,  `username` varchar(25) NOT NULL default '',  `PASSWORD` varchar(255) NOT NULL default '',  `info` text NOT NULL,  `user_level` enum('0','1','2','3') NOT NULL default '1',  `signup_date` datetime NOT NULL default '0000-00-00 00:00:00',  `last_login` datetime NOT NULL default '0000-00-00 00:00:00',  `activated` enum('0','1') NOT NULL default '0',  `im_update` enum('0','1') default '0',  PRIMARY KEY  (`userid`),  KEY `PASSWORD` (`PASSWORD`),  KEY `email_address` (`email_address`),  KEY `activated` (`activated`),  KEY `mailing_list` (`mailing_list`)) TYPE=MyISAM COMMENT='Membership Information' AUTO_INCREMENT=33;

db.php is the file used to connect the the mysql database. If this is not working you may recieve a series of errors. You will need to create a file, insert the following php code, and save it as db.php.

The content is shown below:
<?//set database connection variables$hostname='localhost';$uname='';$password='';$database='';//establish database connection$connect = mysql_connect($hostname,$uname,$password);//display error if connection failsif ($connect==FALSE) {   print 'Unable to connect to database: '.mysql_error();} else {//select databasemysql_select_db($database);}?>

Information for link creation / running the script.
----------------------------------------------------------------------------
In php you can append data to urls e.g. file.php?a=1
you can then parse this to a php variable.

this script uses the following examples:

when the loaded page = ?page=message
it shows all the public messages.

when it = ?page=message&a=send&user=user1
it shows the send form with user1 in the to field

?page=message&a=read&user=user1
shows messages to user1.
If the same user user is logged in it shows all messages, if not it shows public messages.

If an admin requests it ($_SESSION['user_level'] over 2) it shows all messages.

when the form is sent it goes to ?page=message&a=sent&b=1
to insert the data to the database.
----------------------------------------------------------------------------

I admit that when i made it it was for personal use and not as a tutorial. I hope that ive given enough details, if not let me know and i will try and reslove it, or inform me of any changes you make so i can update this.

enjoy :) - once you have it working can you post a reply at the bottom so i know, if your the first person to do so

Share this post


Link to post
Share on other sites

i dont - only on my website, but cos im still developing that i havnt got many users on there. You're welcome to register if you like - you should get a welcome IM that will show you what its like.When ive got the site running ill email everyone who has registered to say - so If you dont want me to email you once ive got the site working then stick 'test' or 'dont email or 'delete me'' in the user info section of the log in form so i can delete you when its all up and running.http://forums.xisto.com/no_longer_exists/
Also, cos the way it displays is quite easily customised with a little html knowledge a demo wont really help (i think there are 3 sections that need changing - i was going to make it a template to include but couldnt be bothered - so your welcome to adjust it) - im planning to change the way mine displays after a while anyway - though this suffices. I figured if by some freak chance it became a popular system then if everyone was using my layout id look like a copy lol :)Basically at the moment it just loooks like this in a bordered table with a bit of spacing..:to from message date typeuser1 user2 hello 22:00 11/10/04 useruser1 admin hi 23:32 11/10/04 admin^but i might change mine to be more like:You have recieved (2) new IM's:from admin (admin)message: blahblah blahdate: ....from user 1 (User)message: blahblah blahdate: ....

Share this post


Link to post
Share on other sites

that records the time in the same field as the date column and prints it all togetherYou can split it with one line of php commands if you want

Share this post


Link to post
Share on other sites

the IM stuff here is finished... and cos it works within your own site i cant make a copy for it unless i give you my whole cms system - which isnt finished

Share this post


Link to post
Share on other sites

Yeah it works a bit like PMs, but when you get a new one it prints a little box at the top of the screen above the content for 2 minutes after its sent.If you want a demo, then go to my site, register, and send yourself a PM.If you dont want me to send you an email when i've finished my site then put 'delete me' in the info field when you sign up.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.