I've made a chat application for a website. Everything is working fine, however, I am trying to make a situation where when the user clicks 'logout', the chat box system will show to other users that the specific user has indeed logged out of the room, and then it will proceed to clear all old messages on the user's screen who has logged out, in case the user comes back in that same room they won't be able to see any of the old conversations they had.
The file which saves all the messages from the chat box is 'log.html'. For some reason, my code was partially working fine before, i.e. the log.html was being truncated when the user clicked logout, but I can never get that log.html file to write that the user has indeed logged out, now both of them don't work. Can someone look at my code and let me know if I'm even doing it right? By the way, the codes might not even contain the actual problem, but then again, it might, so feel free to ask if you need more information.
This code piece comes from group2.php, where the chatbox is stored:
session_start();
if(isset($_GET['logout'])){
//Simple exit message for chat log
$fp = fopen("log.html", 'a');
fwrite($fp, "<div class='msgln'><i>User ". $_SESSION['name'] ." has left the chat session.</i><br></div>");
fclose($fp);
//Truncate the log file
$fp = fopen("log.html", 'w');
fclose($fp);
session_destroy();
header("Location: main.php"); //Redirect the user
}
This is also some code from group2.php that might be of some help:
<div id="menu">
<p class="welcome">Welcome, <b><?php echo $_SESSION['name']; ?></b></p>
<p class="logout" name="logout" ><a id="exit" name="logout" href="#">Exit Group</a></p>
<div style="clear:both"></div>
</div>
I was also thinking, could it be, I need to echo out that the user has left, and clear out the log.html file, within this code instead??
//If user wants to end session
$("#exit").click(function(){
var exit = confirm("Are you sure you want to end the session?");
if(exit==true){window.location = 'main.php?logout=true';}
});