Creating a chat script with PHP and Ajax, Part 2
(Page 2 out of 5)The basic necessities
The first thing our web app needs is a 'global.php' file, which will setup a database connection, include all the other files and libraries and do a few other basic tasks. It looks like this:
error_reporting (E_ALL);
if (get_magic_quotes_gpc()) {
function stripslashes_deep($value)
{
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
$_POST = array_map('stripslashes_deep', $_POST);
$_GET = array_map('stripslashes_deep', $_GET);
$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
$_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}
// Get site path
$site_path = realpath(dirname(__FILE__) . '/../') . '/';
// Include libraries
include ($site_path . 'libs/JSON.php');
include ($site_path . 'libs/adodb/adodb.inc.php');
include ($site_path . 'includes/functions.php');
include ($site_path . 'includes/user-functions.php');
include ($site_path . 'includes/chat-functions.php');
// Connect to MySQL DB
$db = NewADOConnection('mysql');
$db->Connect('localhost', 'DB_USER_HERE', 'DB_PASSWORD', 'DB_NAME');
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
?>
Make sure to put your own database user, password and name in the file above, and save it as 'global.php' in the includes directory. The web app will also need a common 'functions.php', which holds frequently used functions like generate_password() and such. Click here to download the 'functions.php' and save it in the includes directory.
Now that we've got the basics pretty much laid out, let's start with the actual web app. The first thing we'll do is create the chat client itself.
Creating the chat client
To start off, our chat client only needs to do three things:
1. Let the visitor enter a username and login
2. Show all the incoming messages, and allow new messages to be sent
3. Keep a list of who's in the chat room
The HTML behind these three things is fairly standard:
rel="stylesheet" href="style.css" type="text/css" media="screen" />
Enter a username to enter the chatroom...
type="text" name="username" id="username" />
type="text" id="talk" name="talk"> type="submit" value="Say It!" onclick="send_message();" id="sayit" />
Welcome to the chatroom...
Users:
id="users">
The above page uses fairly basic HTML, and consists mainly of two div elements ("login" and "chat"), a few input elements and a list element for the user list. The page also includes several JavaScript files, including the Prototype, JSON and Logger libraries. To make the chat room look a bit prettier, an external stylesheet is used, which can be downloaded by clicking here.
If you try to use the page above, you'll find that nothing works yet, because we haven't written any of our custom JS files yet. But that's what we'll do now, starting with the login.
April 4th, 2006 at 2:47 am
[…] Tra le moltissime letture, mi ero dimenticato di far presente del primo di una serie di articoli dedicati alla creazione di una chat utilizzando il linguaggio di programmazione PHP ed ovviamente Ajax. Be’, lo faccio ora insieme all’annuncio dell’uscita della seconda parte. […]
April 4th, 2006 at 5:46 pm
Very interesting article. Will certainly download the code and study it some more. Thanks.
One question though: how is the escaping of the data to the db handled? I didn’t study all code yet, but in your article in the login script there’s only the code
// Make it safe
$username = htmlentities($username);
before the data is sent to the db.
Does ADODB handle the escaping itself?
April 5th, 2006 at 12:24 pm
It’s a good example for what can be done with Ajax but it’s no real world example. Webchats are supposed to serve at least 1000 Clients but this thing will kill the Server if there are more than ~100 and i testet the live demo - it’s having a lag of about 2sec and thats not good in real world.
But nevertheless a nice example.
April 9th, 2006 at 2:56 am
Its realy good chat but this is php. I code java chats and i have mor than 2000 users in this system , but with php server was slow or down.
Sorry java is greater for chatsystems or php for a alternativ session!
Best regards
April 18th, 2006 at 4:15 pm
Bernhard, I think the delay is due to the refresh/reload rate and not server lag. With a few tweaks, this would make a great small scale chat client.
April 24th, 2006 at 7:26 pm
[…] http://phpit.net/article/ajax-php-chat-part-two/1/ […]
June 28th, 2006 at 1:57 am
There are some Problems with UTF-8 with German Umlaute (ä ü ö). How can i handle this?
August 7th, 2006 at 11:52 am
Hi
This is a gr8!!!!!!!!!! article…
Will download the code to dwell deep into it.
Thanks
Prasanna