Version: 1.4.0b
Type: Sample Code (HOWTO)
Category: BBS/Discussion
License: GNU General Public License
Description: A chat program powered by mySQL and PHP. Inside the raw text file are instructions and all the files necessary, it will cost you a bit of time weeding through the files in there, but it shouldnt be too horrid. I suggest you have a working knowledge of mySQL. It is meant to be entirely buildable, and I have even left a function out, so you may learn how to make your own while making the missing function.
<?php /********************************************************************************************************************************* The chat application, in PHP. Instructions: Step 1: Set up your mySQL. This is pretty simple, just make two databases, "validation" and "chat". Step 2: (Optional, but preferred) Add a table to the "chat" database. The table should be made like so (I am taking into account you have a decent knowledge of mySQL): Fields for the "lobby" table: "id", a tinyint type, with a length of 4, autoincrement and primary key. "name", a varchar type, with a length of 50, default "anonymous". "message", a text type. Step 3: Add a table to the "validation" database with the name of "chatrooms". Fields for the "chatrooms" table: "id", a tinyint type, with a length of 2, autoincrement and primary key. "key", a varchar type, with a length of 100. "room", a varchar type, length of 100. "creator", a varchar type, length of 50. "timestamp", a datetime type, default of "0000-00-00 00:00:00". "comments" a text type. "is_lobby", a tinyint type with a length of 1, default of 0. Step 4: If you did step 2, add a new row to the "lobby" table, and to the "chatroom" table, with these values... In the "lobby" table: "id", let this take care of itself. "name", whatever your online handle or name is. "message", a quick little message you want to show up as the first message in the chat room. In the "chatrooms" table: "id", once again, let it take care of itself. "key", this is more of a toughie. Whip up a PHP script that outputs the md5 of your password. Put that in there. "room", "lobby" should be the value here. "creator", whatever you put into the "name" field of "lobby" "timestamp", put the current date and time in here in the correct format. "comments", put whatever you want to here, doesnt matter much. "is_lobby", set this little guy to "1". Step 5: The simplest step so far. Go through this file and save out its stuff in the proper filenames, pretty straightforward. Pretty easy to do if you know some mySQL. ^_^;;;; *********************************************************************************************************************************/ /************** index.php *************/ ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <?php /* Code last edited on 2/13/2004 Shinji's personal chat program. Code is under the GPL liscence. -- Beta -- v1.4.0 -- ChangeKey script now works. (2/13/2004) v1.3.1 -- Set the Change Key Link so it sends necessary data. (2/13/2004) v1.3.0 -- Reset Room button now links to a working script. (2/4/2004) v1.2.2 -- Changed another link for the same purpose. (2/4/2004) v1.2.1 -- Changed a link so the reset room code works properly. (2/4/2004) v1.2.0 -- New Room Button links to a working script. (2/3/2004) v1.1.4 -- added buttons for new abilities. (2/3/2004) v1.1.0 -- code working, optimised. New production phase begun. (2/3/2004) v1.0.0 -- code working IN THEORY. (2/2/2004) -- Alpha -- v2.0 -- moved to mySQL. Automated shizzo. (2/2/2004) v1.4 -- moved the functions back to a separate script. (2/2/2004) v1.3 -- moved the functions to here. (2/2/2004) v1.2 -- Actual code started. (2/2/2004) v1.1 -- Instability resolved. (2/1/2004) v1.0.1 -- Design Completed. Unstable compliance with tables. (2/1/2004) v1.0.0 -- Project started. Basic form design and code getting structured. (2/1/2004) */ $version = "1.4.0b"; include("getscripts.php"); $room = $_REQUEST['room']; $user = $_REQUEST['user']; if ($room=="") { $room = "lobby"; } ?> <html> <head> <title>Chat Room Main</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script language="JavaScript" type="text/JavaScript"> <!-- function MM_jumpMenu(targ,selObj,restore){ //v3.0 (I) eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'"); if (restore) selObj.selectedIndex=0; } function MM_findObj(n, d) { //v4.01 (Hate) var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) { d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);} if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n]; for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); if(!x && d.getElementById) x=d.getElementById(n); return x; } function MM_jumpMenuGo(selName,targ,restore){ //v3.0 (Javashit) var selObj = MM_findObj(selName); if (selObj) MM_jumpMenu(targ,selObj,restore); } //--> </script> </head> <body> <form name="chat" method="post" action="post.php<?php echo("?room=$room"); ?>"> <p>Room: <select name="room" size="1" id="room" class="inputstyle" onChange="MM_jumpMenu('parent',this,0)"> <?php gettables($room); ?> <!-- The following uses .gif pixel art, if you would like to use the ones I have prepared, just e-mail me, and I will send them to you via email. --> </select> <a href="newroom.php"><img valign="center" src="/images/newroom.gif" border="0" alt="New room." title="New room."></a> <a href="resetroom.php?room=<?php echo($room); ?>"><img valign="center" src="images/resetroom.gif" border="0" alt="Reset room." title="Reset room."></a> <a href="changekey.php?room=<?php echo($room); ?>"><img valign="center" src="images/changekey.gif" border="0" alt="Change key." title="Change key."></a> <a href="killroom.php"><img valign="center" src="images/killroom.gif" border="0" alt="Kill room. (under development)" title="Kill room. (under development)"></a> </p> <p> <div> <!-- I suggest you use CSS here to make the div look like a form object. --> <?php getmsgs("20", "$room"); ?> </div> <p> Name : <input name="name" type="text" class="inputstyle" value="<?php echo("$user"); ?>" size="42"> <br> <br> Message : <textarea name="message" cols="40" rows="3" class="inputstyle"></textarea> </p> <p align="center"> <input name="room" type="hidden" value="<?php echo("$room"); ?>"> <input name="Submit" type="submit" class="inputstyle" value="Ok!"> </p> </form> <p> <?php echo("<code>v $version</code><br><a href='index.phps'>View the Source</a>"); ?> </p> </body> </html> <?php /***************** getscripts.php *****************/ ?> <?php /* Code last modified on 2/3/2004 By Saihenjin. Code held under the GPL Liscence. -- Beta -- v2.1.0 -- newroom script completed, placed in its own page. (2/3/2004) v2.0.0 -- moved to mySQL partially. Automated funstuffs. (2/2/2004) v1.0.0 -- Wiped clean, moving to mySQL. (2/2/2004) -- Alpha -- v1.1 -- functions completed. Testing stage. (2/2/2004) v1.0.1 -- moved other functions here for simplicity. (2/1/2004) v1.0.0 -- code begun. (2/1/2004) */ function getmsgs ($number, $room) { // Obselete code has been removed. Now runs with mySQL. // Connecting, selecting database $link = mysql_connect("your_server_locale", "guest", "guest"); mysql_select_db("chat"); // Perform SQL query $query = "SELECT COUNT(*) FROM $room"; $result = mysql_query($query); while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { $RealRows = $line["COUNT(*)"]; } $offset = $RealRows-$number; mysql_free_result($result); // $number is the number of rows. if ($offset < 0) { $query2 = "SELECT * FROM $room ORDER BY id ASC"; } else { $query2 = "SELECT * FROM $room ORDER BY id ASC LIMIT $offset, $number"; } $result2 = mysql_query($query2); while ($line2 = mysql_fetch_array($result2, MYSQL_ASSOC)) { $user = $line2["name"]; $message = $line2["message"]; // now that we have the vars, display them out. echo("<font size='2'>"); head($user, " : ", $message); echo("</font>"); } // Close it all up mysql_free_result($result2); mysql_close($link); } function gettables ($CurrRoom) { // Blah, blah, you know the routine. $link = mysql_connect("your_server_locale", "guest", "guest"); mysql_select_db("chat"); $query = "SHOW TABLES"; $result = mysql_query($query); while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { $tables = $line["Tables_in_chat"]; // Now return the tables correctly! if ($tables == "$CurrRoom") { // If its the first time through... echo("<option selected='SELECTED' value='index.php?room=$tables'>$tables</option>"); } else { // if not... echo("<option value='index.php?room=$tables'>$tables</option>n"); } } // Close it all up mysql_free_result($result); mysql_close($link); } ?> <?php /***************** post.php *****************/ ?> <?php // Code last edited on 2/3/2004 // post.php -- GPL, like everything here. /* -- Beta -- v2.1.0 -- Code complete, moving to next phase. (2/3/2004) v2.0.0 -- finished mySQL move. Cleaned out obselete code. (2/2/2004) v1.0 -- Moving to mySQL. (2/2/2004) -- Alpha -- v1.1 -- realized it doesnt recognize HTML, and removed unnecessaries. (2/2/2004) v1.0.1 -- code basics created. testing stage. (2/1/2004) v1.0.0 -- code begun, basic design begun. (2/1/2004) */ $room = $_REQUEST["room"]; $user = $_REQUEST["name"]; $message = $_REQUEST["message"]; // we got the vars, now do fun stuffs. ?> <HTML> <head> <meta http-equiv="refresh" content="0;URL=/chat/<?php echo("index.php?room=$room&user=$user"); ?>"> </head> <body> <?php $message = $message . "<br>"; $link = mysql_connect("your_server_locale","guest","guest"); mysql_select_db("chat"); $result = mysql_query("INSERT INTO $room(name,message) VALUES('$user','$message')") or die("query failed.<br>Query in question: INSERT INTO $room(name,message) VALUES('$user','$message')"); mysql_free_result($result); mysql_close($link); // Tell them it worked...or so we hope. die("It has been done, in accordance with the prophecy. <a href='index.php?room=$room&user=$user'>Back to the chat!</a>"); ?> </body> </html> <?php /***************** changekey.php *****************/ ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <?php /* Last modified: 2/13/2004 This and all its associated files are liscenced under the GPL. -- Beta -- v1.0.0 -- Code working completely, debugging begun. (2/13/2004) -- Alpha -- v1.1.0 -- Code completed, testing stage. (2/13/2004) v1.0.0 -- Project started (2/13/2004) */ $room = $_REQUEST['room']; $user = $_REQUEST['user']; $version = "1.0.0b"; $flag = $_REQUEST["flag"]; include("getscripts.php"); ?> <html> <head> <title>Chat Room, Change password.</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <?php // The following line should be uncommented if you are working on an addition at the moment, and then re-commented at completion. # die("This code is under construction at the moment, your action has been cancelled for security reasons."); // if you haven't posted the data yet... if($flag != "true") { ?> <form name="chat" method="post" action="changekey.php"> <p> <input name="room" type="hidden" value="<?php echo($room); ?>"> <input name="user" type="hidden" value="<?php echo($user); ?>"> <input name="flag" type="hidden" value="true"> Old Pass: <input name="oldkey" type="password" size="30"><br> New Pass: <input name="newkey1" type="password" size="30"><br> Confirm : <input name="newkey2" type="password" size="30"><br> </p> <p> <input name="Submit" type="submit" value="Reset!"> </p> </form> <?php // But if you have... } else { // Get the variables... $oldkey = $_REQUEST['oldkey']; $newkey1 = $_REQUEST['newkey1']; $newkey2 = $_REQUEST['newkey2']; $oldkey = md5($oldkey); // Connect to the database... $link = mysql_connect("your_server_locale", "guest", "guest"); mysql_select_db("validation", $link); // Now query the database... $query="SELECT * FROM chatrooms WHERE room='$room'"; $result = mysql_query($query); if ($numrows == "0") { die("That room doesn't exist. Go away now, you wannabe hacker! All you can do is dumpy mySQL injection on this chat application. Now go away, you silly English pig-dogs with your illegitimate faces!"); } // Alright, nice, nice. Now lets check their password! while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { $realkey = $line["key"]; // Get the ID of it so theres no big time guesswork later on. $id = $line["id"]; if ($realkey != $oldkey) { die("That is a wrong password, homeslice!"); } else { // Good old pass, now lets checkup on the new one. if ($newkey1 != $newkey2) { // Dear me! Incorrect confirmation! die("The new password you provided was not the same as its confirmation. Please make sure you typed them both correctly."); } else { $newkey = md5($newkey1); // Ok, those are good. Make a new connection to update the thing. $link2 = mysql_connect("your_server_locale", "guest", "guest"); mysql_select_db("validation", $link2); // Now do the actual updating. $query2="UPDATE chatrooms SET `key`='$newkey' WHERE id='$id'"; $result2 = mysql_query($query2) or die("The password could not be updated."); mysql_free_result($result2); mysql_close($link2); } } } mysql_free_result($result); mysql_close($link); echo("Your pass has successfully been changed. <a href='index.php?room=$room&user=$user'>Go back.</a>"); } // yay! thats it! ?> <p> <?php echo("<code>v $version</code><br><a href='changekey.phps'>View the Source</a>"); ?> </p> </body> </html> <?php /***************** newroom.php *****************/ ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <?php /* Last modified: 2/4/2004 This and all its associated files are liscenced under the GPL. Blah, blah. fill this in later. -- Beta -- v1.2.0 -- Validation bugs fixed...again. (2/4/2004) v1.1.2 -- Validation bugs have resurfaced. Fixing. (2/4/2004) v1.1.1 -- Fixed validation bugs. (2/3/2004) v1.1.0 -- Added validation features, should be working keyword is "should". (2/3/2004) v1.0.0 -- Code working. To do: implement user self-administration and other such ideas so chats are secure. (2/3/2004) -- Alpha -- v1.2.0 -- code should be working. Keyword is 'should'. (2/3/2004) v1.1.0 -- Design completed, code begun. (2/3/2004) v1.0.2 -- Design begun. (2/3/2004) v1.0.0 -- Project started (2/2/04) */ $version = "1.2.0b"; $flag = $_REQUEST["flag"]; $timestamp = date("Y-m-d H:i:s"); include("getscripts.php"); ?> <html> <head> <title>Chat Room -- New room</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <?php // The following line should be uncommented if you are working on an addition at the moment, and then re-commented at completion. # die("This code is under construction at the moment, your action has been cancelled for security reasons."); // if you haven't posted the data yet... if($flag != "true") { ?> <form name="chat" method="post" action="newroom.php"> <p align="center">Room Name: <input name="tablename" type="text" size="22"><br> Username Character Limit: <input name="namelimit" type="text" size="3"><br> </p> <hr> <p> Room Access Key: <input name="key" type="password" size="20"><br> Creator: <input name="creator" type="text" size="30"><br> Comments: <textarea name="comments" cols="27" rows="3"></textarea> </p> <p> <input name="flag" type="hidden" value="true"> <input name="Submit" type="submit" value="Create!"> </p> </form> <?php // But if you have... } else { // connecting, and all that blah... $link = mysql_connect("your_server_locale", "guest", "guest"); mysql_select_db("chat"); // Getting the variables we need... $namelimit = $_REQUEST['namelimit']; $tablename = $_REQUEST['tablename']; // we got the vars we need, now lets create that table! $query="CREATE TABLE `$tablename` (`id` tinyint NOT NULL AUTO_INCREMENT , `name` varchar($namelimit) NOT NULL DEFAULT 'anonymous' , `message` text NOT NULL , PRIMARY KEY (`id`)) TYPE=MYISAM PACK_KEYS=DEFAULT ROW_FORMAT=DEFAULT"; $result = mysql_query($query) or die("Room creation failed. Please check with a webadmin for assistance."); mysql_free_result($result); // now that we have the room made, next we have to set up its validation. // Getting variables. $creator = $_REQUEST['creator']; $comments = $_REQUEST['comments']; $room = $tablename; $key = $_REQUEST['key']; $key = md5($key); // Now lets run the query for the validation row for this big boy! mysql_select_db("validation"); // Now we have the right db selected, lets insert the data for it! $query="INSERT INTO `chatrooms` (`key`, `room`, `creator`, `timestamp`, `comments`) VALUES ('$key', '$room', '$creator', '$timestamp', '$comments')"; $result = mysql_query($query) or die("Room validation failed, see a webadmin for assistance."); // okie, now that thats all done, we hopefully finished without a hitch, so lets go on! mysql_free_result($result); mysql_close($link); // And thats the end of that chapter! now lets tell them it worked and allow them to go back! echo("Queries were successful! <a href='index.php'>Go back to the chats!</a>"); } ?> <p> <?php echo("<code>v $version</code><br><a href='newroom.phps'>View the Source</a>"); ?> </p> </body> </html> <?php /***************** reset.php *****************/ ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <?php /* Last modified: 2/4/2004 This and all its associated files are liscenced under the GPL. -- Beta -- v1.0.1 -- Fixed typos and other anomalies. (2/4/2004) v1.0.0 -- Code moved to the public view. (2/4/2004) -- Alpha -- v1.2.0 -- Majority of bugs fixed. (2/4/2004) v1.1.5 -- Code errors fixed. Debugging and bug finding stage. (2/4/2004) v1.1.0 -- Code should be working for reseting chatrooms (with validation, of course). (2/4/2004) v1.0.0 -- Project started (2/4/2004) */ $room = $_REQUEST['room']; $version = "1.0.1b"; $flag = $_REQUEST["flag"]; include("getscripts.php"); ?> <html> <head> <title>./chat -r <?php echo($room); ?></title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <?php // The following line should be uncommented if you are working on an addition at the moment, and then re-commented at completion. # die("This code is under construction at the moment, your action has been cancelled for security reasons."); // if you haven't posted the data yet... if($flag != "true") { ?> <form name="chat" method="post" action="resetroom.php"> <p> <input name="room" type="hidden" value="<?php echo($room); ?>" size="22"> <input name="flag" type="hidden" value="true"> Room Access Key: <input name="key" type="password" size="30"><br> Message: <input name="message" type="text" size="40"> </p> <p> <input name="Submit" type="submit" value="Reset!"> </p> </form> <?php // But if you have... } else { // connecting, and all that blah... $link = mysql_connect("your_server_locale", "guest", "guest"); mysql_select_db("validation", $link); // Getting the variables we need... $key = $_REQUEST['key']; $key = md5($key); // Now lets first make sure they have a valid room, here... $query="SELECT * FROM chatrooms WHERE room='$room'"; $result = mysql_query($query); if ($numrows == "0") { die("That room doesn't exist. Go away now, you wannabe hacker! All you can do is dumpy mySQL injection on this chat application. Your mother was a hamster and your father smelt of elderberries! Go away now, or I shall taunt you a second time!"); } // Alright, nice, nice. Now lets check their password! while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { $realkey = $line["key"]; if ($realkey != $key) { die("That is a wrong password, homeslice!"); } else { // Ok, thats a good password. Now lets go and begin making that table. $user = $line['creator']; $message = $_REQUEST['message']; $message = $message . "<br>"; // We gotta connect a second time, because the first query is still running. $link2 = mysql_connect("your_server_locale", "guest", "guest"); mysql_select_db("chat", $link2); // Now lets do what has to be done! $query2 = "DELETE FROM $room"; $query3 = "INSERT INTO `$room` (`name`, `message`) VALUES ('$user', '$message')"; $result2 = mysql_query($query2) or die("DUDE! It could SO totally not be reset! Thats forked up!"); // clear out the first one now... mysql_free_result($result2); // and start the next query! $result2 = mysql_query($query3) or die("HOLY FREAKING CRAP! The messages were reset, but the new message could not be entered!!!"); mysql_free_result($result2); mysql_close($link2); // now that we finished that, we can close this all up with the elses! } } mysql_free_result($result); mysql_close($link); echo("Room has been successfully reset. <a href='index.php?room=$room&user=$user'>Go back.</a>"); } // yay! thats it! ?> <p> <?php echo("<code>v $version</code><br><a href='resetroom.phps'>View the Source</a>"); ?> </p> </body> </html> <?php /* Special thanks to: PHP, quite obviously. Apache, the most wonderful server out there. Kawauso, though a bitch at times, you are one of the best people I know. Guin, you give me a reason to live. www.phpbuilder.com -- you give me inspiration. Sonja, you give me something to laugh at when I am stressed. www.google.com -- the best search engine out there. and finally, to anyone out there that helped that I forgot. */ ?>