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 )
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.