views:

29

answers:

1

Hello,

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';}
    });  

Thank you in advance.

+1  A: 

If I may ask, why were you clearing the file: $fp = fopen("log.html", 'w'); after writing to it a few lines before?

I think a better solution would be:

$fp = fopen("log.html", 'w');
fwrite($fp, "<div class='msgln'><i>User ". $_SESSION['name'] ." has left the chat session.</i><br></div>");
fclose($fp);

Also, I think that you might have to write something to the file for the truncate to go into effect (per say). So, if you wanted to keep the current code, add a fwrite($fp, ""); after the $fp = fopen("log.html", 'w');

Hope that helps!

James Hartig
Hmm, that makes sense James, but still the same problem after I followed your steps. Could it be, 'logout' is not even set? I'm a beginner at this, so I do make silly mistake. Thank you anyway.
Newbie_25
By still not working, you mean the file is not being truncated and the "{user} has left the chat session." is being appended to the end of the file?
James Hartig
I just don't see how it isn't working. I mean you must have permissions to write to the file or nothing would work. The w truncates the file, and you are writing something to the file. The file has more than just that last line (the only saying the user logged out), correct?
James Hartig
Yes, correct, it has more lines such as chat messages that are written to the file. But for some awkward reason, the file itself won't truncate nor would that last message be written to the file when the user logs out.
Newbie_25
Can you check to see if there were any PHP warnings or errors? set display_error to On and turn reporting to E_ALL
James Hartig