How to Easily Make a PHP Chat Application

Today I will tell you about creating simple Chat application using our existing login system. This will be useful and most simple solution. We will able to chat with our logged members. We will use database to store messages.

Step 4. PHP

As you remember – we already had ready easy authentication system. I moved it to external library file (inc/login.inc.php). This will useful for us – now code more structured and it comfortable to use in different places of code.

After I created new library to work with chat (inc/chat.inc.php). This class have next functions:

acceptMessages – function accepts sent messages and saves them in database table

getMessages – returns list of recent 15 messages

Then I created two php files: messages.php and main.php. First file generates list of recent messages. It refreshes the list each 5 seconds (it is enough to our chat). Second file generates a login form and one text input field for the chat. After we login, we can use this field to post messages in the chat.

2 bert-coder,
Yes, in That chat application – we have to refresh chat.
please point to next:
<meta http-equiv="refresh" content="5">
By the way – you can focus your attention to new series of articles where we are making nice ajax chat.

hi again another question coz im new to this one.. what is the use of ob_start and flush?? right now i just modified the chat by adding who’s online and making the user fetch from database and putting them into session. thanks alot for this one ..

Hi Jeff,
As I see – you are working at localhost. So I can advise you to check your sql database (and check – if any db used linked to this database, possible that no). Also I can advise you to use WAMP software (as best software for local simulation Apache+MySQL+PHP for windows).

Hi alok0077,
Yes, of course we can, in this case you have to expand array with members: $this->aExistedMembers
And, you always welcome to read our recent Chat-related tutorials where we develop our own modern chat software.

i m using wamp server as my localhost.. but still cannot run.. the error displayed is “The requested URL /LiveChat/’.$_SERVER['PHP_SELF'].’” was not found on this server.”…..wht i hv done r changed the name of
$this->sDbName = ‘database_name’;
$this->sDbUser = ‘root’;
$this->sDbPass = ”;

Hi Manish,
Basically, you had to unpack our package to your localhost somewhere, as example at:
c:/wamp/www/my_chat/
Then, add one SQL table from step 3 into new database or into any of your existed databased,
after, configure inc/chat.inc.php, where you have to set variables for your database (database name, username and password).

Hi admin. I am a new web dev going into my senior year, and am trying to make a small chat room for my private school. I used the documents from the sample and am working on all necessary corrections, but I am in need of more knowledge in terms of how i can create:
1. User privilege settings (i.e. admin, mod, user)
2. A ranking system
3. Private messages
4. Avatars
5. Profiles
6. Banning system
7. Registration
8. How to log an ip address into a system so that only the allowed ip addresses could access the website.

Hi Black,
I would recommend you to start reading our recent tutorials about creating better chat software rather than this old one. All because current one doesn’t support keeping members in database at all.

Hello Sumee,
Please pay attention to array $aExistedMembers in inc/login.inc.php, it contains a set of possible members. At the left you can see usernames, at the right (values) – MD5 hashes of your actual passwords.

hii admin, my name is maddy0101 and i want to try bulid a chat application and i follow your giving code and its not runing. i have some problem like when i want to run folder of application when i put all the files and .inc folder but its not displayed any thing and when i run the main.php file its giving error like:-
Warning: require_once(inc-login.inc.php) [function.require-once]: failed to open stream: No such file or directory in C:\wamp\www\Chat\main.php on line 9
and so many more plz give me a proper response and code so that i can create this application. Please give me reply as soon as posible. and if posible please send your response at my email magarwal0101@gmail.com. i have download your pakage but its not runing . and i want to ask to that you have mention database_name,user_name, can i use another database name like ”xyx”.
Regards:-
Maddy0101

Hello Manish,
Yes, You very correctly observed – first of all you need to put your own details for your database (it is in ‘inc/chat.inc.php file). Of course, you don’t need to use predefined values – you have to use your own.
As for paths – make sure that ‘inc’ folder is in your ‘chat’ folder (with exactly the same structure as in our package)

Hey i need a little help…
i have done all of it and it workes fine, but i have the following 2 problems:
1: The messages is posted UNDER the other messages instead of OVER the other messages.
2: When it hits the max ammount of messages, new messages will not be shown… (i think it is because of my first problem)
please help…. For a demonstration, visit ….

Hello SnowPatch,
If you like to display the latest messages in the bottom (reverse), you have to change function getMessages (chat.inc.php), try to use PHP array sorting functions to change position of elements.

Brilliant idea. I have tried to get the code to work, and it almost works expcept for one thing:
I can’t display any of the messages i write in the application. The things i write appear in the database, when i check that manually, but it will not display on the website. Any ideas?

Hello Stian,
Yes, really interesting. I think that you have debug inc/chat.inc.php::getMessages function.
And, you can also just open ‘messages.php’ in your browser. this file should echo all messages using this getMessages function.

I first logged in using User1/qwerty, and this appears on all the time. I tried to type something else, and hit “say”, but see nothing updated. Even logged in with User2/qwerty, and typed hello then “say’ button, but nothing happened (nothing updated!).
What is wrong?

Hello there! My name is Andrew and I have been doing web development for years. Frankly, not only web-development, and system-development too. This site is the place where I get to teach and share my experience for the web. Read more