Make a simple Public Chat in php?

Posted on

hello friends,
first of all make a index.php and copy and paste the following code

<?php
session_start();
if(isset($_REQUEST[‘submit’]))
{
$_SESSION[‘username’]=$_REQUEST[‘username’];
}
if(isset($_REQUEST[‘submit1’]))
{
$f=fopen(“chat.txt”,”a\n”);
fwrite($f,$_SESSION[‘username’].” >>>  “.$_REQUEST[‘message’].”\n”);
}
if(isset($_REQUEST[‘logout’]))
{
unset($_SESSION[‘username’]);
}
?>
<html>
<head>
<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js”></script&gt;
<script type=”text/javascript”>
var auto_refresh = setInterval(
function ()
{
$(‘#chatbox’).load(‘dynamic.php’);
}, 1); // refresh every 10000 milliseconds
</script>
</head>
<body bgcolor=”#666666″>
<?php if(!isset($_SESSION[‘username’])) { ?>
JOIN PUBLIC CHATROOM >> <form method=”post”><input type=”text” name=”username” /><input type=”submit” value=”Join” name=”submit” /></form>
<?php } else { echo “<div style=’height:50px;width:100%;background-color:silver;color:blue;’>Welcome “.$_SESSION[‘username’].”<form method=’post’><input type=’submit’ name=’logout’ value=’Logout’ /></form></div>”; } ?>
<?php if(isset($_SESSION[‘username’])) { ?>
<div id=”chatbox” style=”width:100%;height:300px;background-color:#996600;overflow-y:scroll;”>
</div>
<form method=”post”>
<input type=”text” style=”width:500px;height:100px;” name=”message” /><input type=”hidden” value=”send” name=”submit1″ />
</form>
<?php } ?>
</body>
</html>

After that make a new file dynamic,php and paste Following Code

<table>
<tr>
<td>User >> Message</td>

</tr>

<?php
$file = @fopen(“chat.txt”, “r”);
//Output a line of the file until the end is reached
while(!feof($file))
{

echo “<tr><td>” .fgets($file). “</td></tr>”;

}
fclose($file);
?>

</table>

Give your suggestion about this script——

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s