if you want to make a chat room application that uses only one log file, i.e. everyone on your site is logged into the same room, it is not that hard, using php and ajax with some jquery. here's the process: you want a user to type in a message and send it, correct?, you'll need a form for that:
<form name="message" action="">
<input name="usermsg" type="text" id="usermsg" size="63" />
<input name="submitmsg" type="submit" id="submitmsg" value="Send" />
</form>
that's the markup for the form, next, you'll need something that seamlessly takes in the user input, whatever the user typed into the text box and sends it to a script, that's where ajax comes in:
<script type="text/javascript" src="jquery-1.8.0.min.js"></script>
<script type="text/javascript" >
//when the user clicks the button with the id submitmsg, the input is taken
$("#submitmsg").click(function(){
var clientmsg = $("#usermsg").val();
//after the input's value is taken, it's sent to a script called
//pst.php
$.post("post.php", {text: clientmsg});
//after the message is sent, the input's value is set to null
//to allow the user to type a new message
$("#usermsg").attr("value", "");
return false;
});
</script>
after doing this, we need to see how the script post.php looks like and what it does, basically, it grabs the input sent to it via ajax and writes it into a file, the file is then loaded onto the webpage and all the messages sent between users can then be viewed, ajax is used further to reload the file after a certain amount time so that the user's are always up to speed with whatever messages it contains, here's the php script:
<?
session_start();
if(isset($_SESSION['name'])){
$text = $_POST['text'];
$fp = fopen("log.html", 'a');
fwrite($fp, "<div class='msgln'><b>".$_SESSION['name']."</b>: ".stripslashes(htmlspecialchars($text))."<br></div>");
fclose($fp);
}
?>
note that i've used a session, this is to get the name of the user that logged in and also output it to the log file.i'm sure you can see how the login system would fit into this, anyways, after writing data to the file, we need to upload it so the users can see:
<div id="chatbox">
<?php
if(file_exists("log.html") && filesize("log.html") > 0){
$handle = fopen("log.html", "r");
$contents = fread($handle, filesize("log.html"));
fclose($handle);
echo $contents;
}
?>
</div>
that's the division where the log file will be loaded, now, just one thing left, we need to reload the file after a certain amount of time, and maybe add an auto-scrolling feature:
//Load the file containing the chat log
function loadLog(){
var oldscrollHeight = $("#inner").attr("scrollHeight") - 20;
$.ajax({
url: "log.html",
cache: false,
success: function(html){
$("#inner").html(html); //Insert chat log into the #chatbox div
var newscrollHeight = $("#inner").attr("scrollHeight") - 20;
if(newscrollHeight > oldscrollHeight){
$("#inner").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
}
},
});
}
setInterval (loadLog, 2500); //Reload file every 2.5 seconds
and that's it, that should work, hope this helps in case you've not yet gotten a useful answer, you would have waited a long time.