Click here to Skip to main content
13,898,462 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


20 bookmarked
Posted 3 Oct 2013
Licenced CPOL

Simple PHP Chat Script

, 11 Oct 2013
Rate this:
Please Sign up or sign in to vote.
This is a simple script for chat, made with PHP and JavaScript, using OOP and AJAX.
AD - Design Tutorial : How To Make a Minion with Adobe Firewors


View more skill oriented articles -


This is a simple script for chat, made with PHP and JavaScript, using OOP and AJAX.


  • PHP 5+
  • Browser with JavaScript enabled (preferable with support for CSS3).

This Chat script does not require database, it works with text files saved on server, so, it is fast and uses verry few server's resources. The user can add smiles in chat, and can format the text with Bold, Italic, an Underline., you can test it online to this link: Script Chat Simple. The nickname, chat room, and the On / Off setting for beep sound are saved in cookies (for one day), so, to the next visit they will be read and used from cookie. The script has been tested successfully on Mozilla Firefox, Internet Explorer, Google Chrome and Opera. This script is free (does not provide support or personal modifications).


<?php include('chat.php'); ?>
<link rel="stylesheet" type="text/css" href="chatfiles/chatstyle.css" />

And move it into the HEAD section of the page where you included the script.

  • After you have unzipped the archive, copy on your server the file "chat.php", and the directories: chatfiles, chattxt, and chatex (with all their files, in the same directory where you have the file in which you want to include the chat). The test.php file is for test.
  • Set read-write-execute permissions to the directory chattxt, CHMOD 0777 (or 0755). In this directory PHP will save the TXT files with the chat for each chatroom.
  • In the PHP file in which you want to include this chat script, add the following code:
  • To have a valid HTML page, take this line of code (for CSS properties) from the beginning of the file "chat.php":
  • This script can also be included in ".html" files, using <object> or <iframe> tag (but the server must run PHP). So, to add the chat script into a ".html" file (blog, forum), write /copy in that page one of the following codes:
    <object type="text/html" data="dir/chat.php" 
          width="620" height="480"></object>
    <iframe src="dir/chat.php" width="100%" height="480" 
       scrolling="auto" frameborder="0" name="frame_chat"> </iframe>
    • Object tag:
    • Or IFrame:
    • "dir/chat.php" is the path to the directory and file where you have the script. If it's in the same folder, write only "chat.php".

Admin settings

define('MAXROWS', 30);
define('CHATLINK', 1);
define('CADMPASS', 'adminpass');

Then select the Chat Room you want to empty, and use the password set to CADMPASS.

In this case, you must add session_start(); to the beginning of your PHP file, as you can see in "test.php" file.

define('CHATADD', 1);
if(CHATADD !== 1) {
    if(isset($_SESSION['username'])) define('CHATUSER', $_SESSION['username']);

In the directory "chatex/" there are two additional WAV files: beep2.wav and beep3.wav. By default, the script uses beep1.wav. In case you want to change the audio sound that is played when a new text chat is added in chat room, change in "chatfunctions.js" file (line 13) the value of the variable:

var beepfile = 'beep1.wav';

If you want more chat rooms, add $chatrooms[] variables, with this syntax:

$chatrooms[] = 'chatroom_name';
  • In the file setchat.php (in chatfiles/ directory) you can change the default settings.
  • By default, the script saves and displays the last 30 rows of chat, you can change this number to the line of code:
  • The script allows users to add links in chat (with attribute rel="nofallow"), but if you want to remove this feature, replace the number 1 with 0, to the line of code:
  • To the following line of code you can set a password used when you want to empty chat rooms (replace "adminpass" with the password you want):
  • To empty chat rooms, access in your browser the file chatfiles/setchat.php with ?mod=admin in URL. For example:
  • If your web site has a register /login script, and you want than only the logged users to can add texts in chat, set the value of CHATADD to 0. Then set $_SESSION['username'] with the session your script uses to keep logged users; to the following code, in "setchat.php".
  • The script initially contains two chat rooms: English, and Nature, if you want only one chat room, delete the line of code that contains the variable $chatrooms[] followed by the name of the chatroom. To change the name of the chatroom, modify the value of those variables.

The script can create automatically the text files for each chat room, but PHP must have writable permissions (CHMOD 0777, or 0755) in the "chattxt" directory. The script can be easily changed to be used in other languages, just translate the text in the file "texts.php" (in "chatfiles/" folder). If you want to modify the colors and design of the chat, edit CSS properties in "chatstyle.css" (or "chatstyle_mini.css", the mini-version), in "chatfiles/" folder. The file "chatstyle_mini.css" can be used for a smaller graphics version of this chat script (width 245 px). In this version, to save space, the area with online users is not displayed.


9,000 + downloads! Thanks guys.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Emerald kwekowe
Chief Technology Officer Microbold Smart - Systems
Nigeria Nigeria
Passionate about web and applications technology.

You may also be interested in...

Comments and Discussions

QuestionHow to keep the list of online users "frozen" Pin
Member 1146533219-Feb-15 12:37
memberMember 1146533219-Feb-15 12:37 
QuestionHow do you stop the chat from emptying itself? Pin
Member 114208236-Feb-15 17:52
memberMember 114208236-Feb-15 17:52 
QuestionSir how to add private massages commond in simple chat?? Pin
Chat4Enjoy28-Dec-14 11:16
professionalChat4Enjoy28-Dec-14 11:16 
QuestionLink Pin
Nelek11-Oct-14 11:59
protectorNelek11-Oct-14 11:59 
AnswerRe: Link Pin
Emerald kwekowe15-Oct-14 6:13
memberEmerald kwekowe15-Oct-14 6:13 
GeneralRe: Link Pin
Nelek15-Oct-14 10:03
protectorNelek15-Oct-14 10:03 
Questioncan i have something like tutorial for given chat code in php Pin
Member 111295024-Oct-14 6:54
memberMember 111295024-Oct-14 6:54 
AnswerRe: can i have something like tutorial for given chat code in php Pin
Emerald kwekowe11-Oct-14 9:56
memberEmerald kwekowe11-Oct-14 9:56 
Questionplease answer we badly need it for our thesis !! Pin
Member 1090797526-Jun-14 4:00
memberMember 1090797526-Jun-14 4:00 
AnswerRe: please answer we badly need it for our thesis !! Pin
Emerald kwekowe18-Sep-14 10:54
memberEmerald kwekowe18-Sep-14 10:54 
Questionhow to change the date/timestamp in the chart after the user name? Pin
my interest22-Apr-14 7:29
membermy interest22-Apr-14 7:29 
QuestionDelete specific message Pin
Member 1069786910-Apr-14 9:44
memberMember 1069786910-Apr-14 9:44 
AnswerRe: Delete specific message Pin
Syed M Hussain21-Oct-14 5:10
memberSyed M Hussain21-Oct-14 5:10 
Generalwow. great scripts. Thanks!! Pin
Member 105004829-Apr-14 1:20
memberMember 105004829-Apr-14 1:20 
QuestionThank you Pin
Member 101479005-Feb-14 7:02
memberMember 101479005-Feb-14 7:02 
QuestionCheck if the name is already used Pin
Member 105649521-Feb-14 6:16
memberMember 105649521-Feb-14 6:16 
Questiondefault setting to empty chat room Pin
Member 1051477022-Jan-14 10:51
memberMember 1051477022-Jan-14 10:51 
Questionlogin admin Pin
salvo7222-Jan-14 6:59
membersalvo7222-Jan-14 6:59 
Questionchat code Pin
Member 1053934421-Jan-14 8:45
memberMember 1053934421-Jan-14 8:45 
Questionproblem with chat messages not posting on different computers Pin
Member 1051477010-Jan-14 6:01
memberMember 1051477010-Jan-14 6:01 
AnswerRe: problem with chat messages not posting on different computers Pin
Emerald kwekowe11-Jan-14 1:14
memberEmerald kwekowe11-Jan-14 1:14 
Questionproblem with chat messages not posting on different computers Pin
Member 105147709-Jan-14 14:42
memberMember 105147709-Jan-14 14:42 
AnswerRe: problem with chat messages not posting on different computers Pin
Emerald kwekowe10-Jan-14 1:53
memberEmerald kwekowe10-Jan-14 1:53 
GeneralRe: problem with chat messages not posting on different computers Pin
Member 1051477010-Jan-14 3:32
memberMember 1051477010-Jan-14 3:32 
GeneralRe: problem with chat messages not posting on different computers Pin
Member 1051477022-Jan-14 7:51
memberMember 1051477022-Jan-14 7:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190306.1 | Last Updated 11 Oct 2013
Article Copyright 2013 by Emerald kwekowe
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid