Get PHP Code for Chatting Online with phpFreeChat

php code for chatting online

Want to run a chat room on your website to make it more social? phpFreeChat is an open source, AJAX-based chat server that you can install on your site. Compared to other chat software, phpFreeChat is a breeze to set up. It doesn’t even require a database! Here I will give you my php code for chatting online.

phpFreeChat is also highly customizable. Below is a chat room I designed for using in office environments where chatting is forbidden.

php code for chatting online

Installation

Check that your web server meets the following requirements:

  • PHP >= 5.3.0
  • Apache server with mod_rewrite and .htaccess enabled
  • Write access for the installation directory

Now visit the Sourceforge project page. Ignore the notice that says, “As of 2006-02-04, this project is no longer under active development.” As of this writing, the beta (2.* branch) was last updated in November of 2012.

For this tutorial, we’re going to use the 1.5 branch, not the beta. The beta is still lacking some important features, such as private messaging and multi-channel management. To get version 1.5, navigate to the “Files” tab boxed in red.

Click on “branch 1.x (stable)” and download one of the archives from the “1.5″ directory. Upload and extract the *.tar.gz or *.zip archive to a directory of your choice on your web server.

In your browser, go to the URL of the directory you uploaded your files to. You will be greeted by the default chat room on your website.

Configuration

You’ll probably want to personalize your chat room at least a little bit. To see some examples of how you can suit it to your own needs, go to “your-phpfreechat-installation.com/demo“. Each demo provides source code for various use cases.

The main file to edit is index.php. Note: Every time you upload a new version of index.php, you must go into the chat room on your website and (as admin) enter the command “/rehash” to effect the changes.

At the top of index.php, you’ll see this code:

require_once dirname(__FILE__)."/src/phpfreechat.class.php";
$params = array();
$params["title"] = "Quick chat";
$params["nick"] = "guest".rand(1,1000);  // set up the intitial nickname
$params['firstisadmin'] = true;
//$params["isadmin"] = true; // makes everybody admin: do not use it on production servers ;)
$params["serverid"] = md5(__FILE__); // calculate a unique id for this chat
$params["debug"] = false;
$chat = new phpFreeChat( $params );

Here you can change the name of your chat room and the initial nickname that guests are given. If you want to force users to enter their own nicknames, remove the $params["nick"] line.

I recommend that you set $params['firstisadmin'] to false to prevent random visitors from automatically gaining administrator privileges.

By default, the admin username is “admin,” and there is no password to use this account. Add a line like this to create a password-protected administrator account:

$params["admins"] = array('BigBoss' => 'p@ssw0rd');

PhpFreeChat will store all chat history and display it in the chat room for every user to see unless you change the number of stored messages. For example, to change this number to ten, add the line:

$params["max_msg"] = 10;

To see more options, check out the full list of possible parameters.

Styling Your Chat Room

PhpFreeChat comes with eight themes, most of which are the same as the default theme but with different emoji sets:

  • Blune
  • Cerutti
  • Default
  • Green
  • Msn
  • Phoenity
  • Phpbb2
  • Zilveer

Themes are stored in the “themes” subdirectory of your installation. If you want to use a theme other than the default, add the theme name to your index.php file:

$params["theme"] = 'msn';

To use your own theme, just upload it to the themes folder and change the above line to point to your theme.

All of the styling in phpFreeChat is done with CSS. If you just want to change a few things, you can get away with editing the files in “your-phpfreechat-installation.com/themes/default“. Alternatively, you can use another theme as a template by copying and renaming its folder.

Commands

Getting the most out of your chat room requires learning a few simple commands. To use any of these, just enter the command into the chat as if it were a regular message. If you’re an IRC user, you’ll feel right at home.

  • /help“: View the list of commands (it will show up below the chat window).
  • /nick newnickname“: Change your nickname.
  • /identify p@ssw0rd“: To log in as admin, change your nickname to the administrator’s username and then identify yourself with the password.
  • /op username“: As admin, give another user administrative privileges.
  • /join room“: Join or create a room.
  • /whois username“: Get a user’s IP address.
  • /kick JerkFace [ because JerkFace is a jerkface ]“: Kick a user and give a reason.
  • /invite username [ roomname ]“: Invite a user to join a room
  • /privmsg username“: Send a private message

See the reference page for more chat commands.

Conclusion

Now you know how easy it can be to create a chat room on your website. If you’re a web developer, you can grab the source code and integrate phpFreeChat into your other web applications.

What other chat software would you recommend for our readers? Comment below and let us know what you think.