Chat application using php

Chat application using php

Hi, In this tutorial I am going to explain how to create chat application using php. In chat application we are going to create to ajax web chat using php, mysql, jquery and css.



My Database having two tables,

1. webchat_lines => Which is going to have the details of chat history

2. webchat_users => Which is going to have the history of users.


To optimize the load time, the stylesheets are included in the head section, and the JavaScript files in the footer, just before the closing body tag.

We are using the jScrollPane plugin to create the scrollable area with the chats entries. This plugin comes with its own stylesheet, which is the first thing we’ve included into the page.

The markup of the chat consists of four main divs – the top bar, the chat container, the user container and the bottom bar. The latter holds the login and submit forms. The submit form is hidden by default and only shown if the user has successfully logged in the chat system.

Lastly we include the JavaScript files. Starting with the jQuery library, we add the mousewheel plugin (used by jScrollPane), the jScrollPane plugin itself and our script.js file.


Now that we have the database in place, lets start discussing the PHP scripts that drive the chat.

The first file we are going to take a closer look at, is ajax.php. It handles the AJAX requests sent from the jQuery front end and outputs JSON formatted data.


The DB class is our database manager. The constructor is private, which means that no objects can be created from the outside, and the initialization is only possible from the init() static method. It takes an array with MySQL login details, and creates an instance of the class, held in the self::$instance static variable. This way we can be sure that only one connection to the database can exists in the same time.

The rest of the classes take advantage of the static query() method to communicate with the database.


This is a simple base class. It’s main purpose is to define the constructor, which takes an array with parameters, and saves only the ones that are defined in the class.


Here is the ChatLine class. It extends ChatBase, so you can easily create an object of this class by providing an array with a text, author, and gravatar elements. The gravatar property contains a md5 hash of the person’s email address. This is required so we can fetch the user’s gravatar from

This class also defines a save method, which the object to our database. As it returns the MySQLi object, contained in the DB class, you can check whether the save was successful by checking the affected_rows property (we will come back to this in the Chat class).


The same is also valid here. We have the name and gravatar properties (notice the protected access modifier – this means that they will be accessible in the ChatBase class, so we can set them in the constructor).

The difference is that we also have an update() method, which updates the last_activity timestamp to the current time. This shows that this person keeps a chat window open and is displayed as online in the users section.

Chat.class.php – Part 1

This is where all the work gets done. Remember the switch statement in ajax.php above? It maps the supported actions with the corresponding methods from this class. Each of these methods returns an array, as it is later converted to a JSON object with the internal json_encode() function (this happens at the bottom of ajax.php).

When the user logs in, their name and gravatar get saved as elements of the $_SESSION array, and become available on consecutive requests. We will be using this to validate that the user is allowed to add chats later on.

You can also see how we are preparing the gravatar hash. This is done according to their best practices guide and ensures that if the person has configured a Gravatar, it will be properly displayed.

Chat.class.php – Part 2

As you will see in the next part of this tutorial, jQuery sends a getUsers() request every 15 seconds. We are using this to delete chats older than 5 minutes and inactive users from the database. We could potentially delete those records in getChats, but that is requested once every second and the extra processing time could severely impact the performance of our app.

Another thing worth noting, is that, in the getChats() method, we are using the gmdate function to output a GMT time. In the frontend, we use the hour and minute values to feed the JavaScript date object, and as a result all the times are displayed in the user’s local time.

Screen Shots Of Chat Application.

Landing Page.

chat application home

When First user Enter into the application.

chat application single

When multiple user Enter into application

chat application double

When Conversation starts into the application

chat application

Thanks!. If you like this article don’t forget to share and comment.