ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.


Creating a chat script with PHP and Ajax, Part 2

(Page 1 out of 5)


Welcome to the second part of the 'Creating a chat script with PHP and Ajax' series. It's been a while since the previous part, and much has happened since then. 37Signals, a very popular "web 2.0" company, has released a web application called Campfire which is actually a chat script based on Ajax and Ruby on Rails, and it has many of the features we'll be implementing in this article series.

In this part we'll start from scratch again, but this time we'll start with a solid structure. Unlike the previous part, which was more or less a hack job, we'll start using JSON and several other libraries to make everything easier for us. I will also show you how to add a few more features, like a user list.

This article will be quite fast paced, and some things won't be explained in detail. That's why it's highly recommended you download the projects files by clicking here so it's easier to follow along. You can also view a live demo of the project, by clicking here.

Let's get started with defining how our web app should look like.

The structure of our web app

Our web app needs to be able to do several different things, like doing Ajax requests, using a MySQL database, and more. Instead of writing everything ourselves, why not use publicly available libraries?

In our web app we're going to use the following libraries:
- ADOdb 4.8 for database abstraction
- Prototype 1.4.0 for all the JS wizardry
- JSON-PHP for the JSON
- JSON JS library for the JSON (on the JS side)
- Lumberjack JavaScript Logger

Next up is the structure of our web app. In this tutorial, I'll be using the following directory structure:

- ajaxchat
 --- libs
  ----- adodb
 --- js
 --- includes

It's obvious that all the libraries are in the 'libs' directory, and all our JavaScript files will go into the 'js' directory.

Finally, you will also have to create a database with the following structure:

CREATE TABLE `message` (
  `messageid` mediumint(9) NOT NULL AUTO_INCREMENT,
  `message` text NOT NULL,
  `time` int(11) NOT NULL DEFAULT '0',
  `username` varchar(255) NOT NULL DEFAULT '',
  `token` varchar(5) NOT NULL DEFAULT '',
  PRIMARY KEY  (`messageid`)

  `userid` smallint(6) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) NOT NULL DEFAULT '',
  `password` varchar(15) NOT NULL DEFAULT '',
  `lastactive` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY  (`userid`),
  UNIQUE KEY `username` (`username`)

Before writing the main thing of our web app (the chat), let's first create a few basic necessities, like a database config file, and a few other things.

Next: Basic Necessities & Chat Client »

8 Responses to “Creating a chat script with PHP and Ajax, Part 2”

  1. » Creare una chat con PHP ed Ajax Says:

    […] 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. […]

  2. Matthijs Says:

    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?

  3. Bernhard Froehlich Says:

    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.

  4. berhard kuisper Says:

    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

  5. Aaron Hancock Says:

    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.

  6. » Ajax article Says:

    […] […]

  7. Michael Says:

    There are some Problems with UTF-8 with German Umlaute (ä ü ö). How can i handle this?

  8. Prasanna Says:


    This is a gr8!!!!!!!!!! article…

    Will download the code to dwell deep into it.


Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction & Structure
  2. Basic Necessities & Chat Client
  3. Creating the login
  4. Ping? Pong!
  5. Sending new messages
Bookmark Article
Download Article
Download this article as a PDF file