Create a simple chatroom system in php without databases

In this post, you will be guided to create a simple chatroom system in php without any databases. This system will create a folder for each chatroom and will store data in a simple .txt file, which is later accessed from the server side to display the conversations in the chatroom. This chatroom is flexible and light weight, it automatically reloads itself in 10 seconds so that new information is fetched correctly.

The complete system is based on just a single php file in which file handling is used to retrieve and store the conversations from the chatroom.

Demo:

The index page when you start a coversation

The conversation section of the index page


Lets have some look on the major sections of the codeTo ask details when someone visits the page for the first time

if (isset($_GET['enSubmit']) && isset($_GET['uname']) && isset($_GET['rname'])){
echo'<meta http-equiv="refresh" content="10">';
$room=$_GET['rname'];
$uname=$_GET['uname'];
if (!is_dir($room)) mkdir($room);
$files = scandir($room);
foreach ($files as $user){
if ($user=='.' || $user=='..') continue;
$handle=fopen("$room/$user",'r');
$time = fread($handle, filesize("$room/$user"));
fclose($handle);
if ((time()-$time)>20) unlink("$room/$user");
}
$contents='';
$filename="$room.txt";
if (file_exists($filename)){
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
}

<!-- HTML code here -->
<form method="get" action="">
<table align="center" style="position: relative;
width: 450px;
height: 200px;
background-color: lightgoldenrodyellow;
padding-top: 30px;
padding-left: 30px;
padding-right: 30px;
border-bottom-left-radius: 30%;
border-top-right-radius: 20%;
align-self: center;
">
<tr>
<td style="text-align: left;width: 432px;color: #2214B9;font-family: cursive;font-size: x-large;">Name:</td>
<td style="font-family: 'Times New Roman', Times, serif; font-size: 17pt; text-align: left; color: #2214B9; width: 430px;">
<input name="uname" style="font-size: medium; width: 260px; color: #B01919;"></td>
</tr>
<tr>
<td style="text-align: left;width: 432px;color: #2214B9;font-family: cursive;font-size: x-large;">Room:</td>
<td style="font-family: 'Times New Roman', Times, serif; font-size: 17pt; text-align: left; color: #2214B9; width: 430px;">
<select name="rname" style="width: 260px; font-size: medium; color: #B01919;">
<option selected="">Sample Group 01</option>
<option>Sample Group 02</option>
</select></td>
</tr>
<tr>
<td style="font-family: 'Times New Roman', Times, serif;font-size: 17pt;text-align: center; color: #2214B9;padding-top:10px;padding-bottom:10px" colspan="2">
<input name="enSubmit" style=" font-size: 20pt;font-family: cursive;color: #19B024;" type="submit" value="Chat Now"></td>
</tr>
</table>
</form>

The section when user enters the details and click on chat now

if (isset($_POST['Send'])){
$text=$_POST['txt'];
$contents.="$uname: $text";
$handle = fopen("$filename", "a");
fwrite($handle, "$uname: $textn");
fclose($handle);
}

<!-- HTML part for the code-->
<form action="" method="post" name="myform">
<table style=" position: relative;
background-color: cadetblue;
width: 450px;
height: 100px;
PADDING: 10 10 10 10;
BORDER-RADIUS: 10px;
box-sizing: content-box;
box-shadow: 2px 2px 2px chocolate;
color: white;" align="center">
<tr>
<td style="font-family: 'Times New Roman', Times, serif;font-size: 17pt;text-align: center;width: 537; color: #2214B9; height: 350px;">
<textarea readonly="readonly" name="txtchat" style="width: 581px; color: #000000; height: 365px; background-color: #F4F8D1; font-family: 'times New Roman', Times, serif; font-size: 12pt;"><?php echo "Welcome to the $room chatroom...n$contents"?> </textarea>
</td>
<td style="font-family: 'Times New Roman', Times, serif;font-size: 17pt;text-align: center;color: #2214B9; height: 349px; width: 143px;">
<textarea readonly="readonly" contenteditable="false" name="txtusers" style=";width: 163px; height: 365px; background-color: #D1F8D8; font-family: 'times New Roman', Times, serif; font-size: 12pt; font-weight: bold; text-align: center;"><?php echo $users?></textarea></td>
</tr>
<tr>
<td style="width: 537;text-align: left; height: 39px; font-size: 14pt;">
<textarea id="txtt" name="txt" style="width: 581px; height: 79px; font-family: 'times New Roman', Times, serif; font-size: 12pt"></textarea></td>
<td style="padding-left: 8px; width: 143px; text-align: center;">
<input name="Send" style="font-size: 20pt; font-family: 'Times New Roman', Times, serif; color: #19B024;" type="submit" value="Send"></td>
</tr>
</table>
</form>

Thats all for it. Alternatively you can download the whole zip here.
Found bugs, feel free to report us, we will be glad fixing them !

Bikash Panda
Catch Me On

Bikash Panda

Blogger / Embedded System Developer at WeArGenius
A techie, tinkerer and tech lover, who loves to blog and feels everyone can learn tech provided they have the right attitude towards learning and passion. By profession, I am an IOT developer working in Smart Home/ Smart Grid domain.
Bikash Panda
Catch Me On

Latest posts by Bikash Panda (see all)

Bikash Panda

A techie, tinkerer and tech lover, who loves to blog and feels everyone can learn tech provided they have the right attitude towards learning and passion. By profession, I am an IOT developer working in Smart Home/ Smart Grid domain.