PHP CHAT ROOM SOURCE CODE

{{{+++ TEST CHAT ROOM +++ }}}

### DOWNLOAD COMPLETE SOURCE CODE AS ZIP FILE ###

Click the first link above that says, “test chat room” to enter and test out the chatroom. It’s working and download the complete source code if you want one for your site too.

Okay, in a previous page I laid out how I approached development for a chat program and I could write some more about that, but I got impatient documenting the process and went straight to rapid development. It’s done. So I’m going to post the source code. To use it install it in a separate directory on your webserver.

File permissions “sudo chmod 664 *” for all the files within the directory worked for me. Remember you have to be able to modify the messages.txt file, but nothing else needs to be writable. The webserver’s default user “root” should be able to do that for you with the 664 permission, but play around with the permissions for the messages.txt file if you need to.

You’ll need to create these four files…

  1. messages.txt – just an empty text file (some hosts don’t like people to upload blank files, so add a little text to the file if necessary to get it uploaded if necessary).
  2. index.php – this is the login interface, you could also name it login.php but index.php is more convenient
  3. user.php – this is the basic interface
  4. chatroom.php – this is the chat window which appears in the interface

Let’s write the code for index.php/login.php (either name is fine, but index.php is more useful but less descriptive) first…

<html>
<title>Login to PHP CHAT</title>
<body>
### LOGIN TO PHP CHAT ###

<form action=”user.php” method=”post”>
Enter a screen name to login: <br>
<input name=”name” type=”text” required>
<br>
<input type=”submit” value=”ENTER”>

</form>
### LOGIN TO PHP CHAT ###

</body>
</html>

That’s it. Now the code for user.php…

<html>
<title>Chat Room User Interface</title>

<body>

<iframe src=”chatroom.php” height=”400″ width=”500″></iframe>
<br>
</html>

<?php
session_start();
?>

<?php
if (!empty($_POST[‘name’])){
$name = $_POST[‘name’];
}
else {
$name = $_SESSION[‘name’];
}
$_SESSION[“name”] = $name;

//check to see if a name is supplied, if not redirect back to login.

if(empty($name)) {//if a name wasn’t supplied during login or if backdoor is used kick them out

Header( “Location: login.php” );
exit();
}

echo “— Welcome to the chatroom : ” . $name . ” —“;

$message = $_POST[‘message’];
if(!empty($message)){
//open file
$handle = fopen(“messages.txt”,”a”);

//write to file
fwrite($handle, $name . “:” . $message . “\n”);

//close file
fclose($handle);

}
?>
<form action=”user.php” method=”post”>
< Enter Your Message >
<br>
<input name=”message” type=”text” autofocus required>
<br>
<input type=”submit” value=”Talk”>

</form>
</body>
</html>

Now for the last file which is user.php…

<html>
<title>
Chat Room User Interface
</title>

<body>

<iframe src=”chatroom.php” height=”400″ width=”500″></iframe>
<br>
</html>

<?php
session_start();
?>

<?php
//sets the page to return to when exiting

if (!empty($_POST[‘name’])){
$name = $_POST[‘name’];
}
else {
$name = $_SESSION[‘name’];
}
$_SESSION[“name”] = $name;

//check to see if a name is supplied, if not redirect back to login.

if(empty($name)) {//if a name wasn’t supplied during login or if backdoor is used kick them out

Header( “Location: login.php” );
exit();
}

echo “— Welcome to the chatroom : ” . $name . ” —“;

$message = $_POST[‘message’];
if(!empty($message)){
//open file
$handle = fopen(“messages.txt”,”a”);

//write to file
fwrite($handle, $name . “:” . $message . “\n”);

//close file
fclose($handle);

}
?>

<form action=”user.php” method=”post”>
< Enter Your Message >
<br>
<input name=”message” type=”text” autofocus required>
<br>
<input type=”submit” value=”Talk”>
</form>

</body>
</html>