Log in

View Full Version : Logout PHP ideas



Mike4x4
05-08-2008, 01:25 AM
I have a chat script that i made but i need ideas on removing users who are not on the webpage, right now if they login and close the browser they still stay in the chat, and to logout they need to click the logout link, what is some ideas to make users logout from the server.

Medyman
05-08-2008, 01:30 AM
Take a look at this thread (http://www.dynamicdrive.com/forums/showthread.php?t=25939) and see if it helps you out any.

Jas
05-08-2008, 02:18 AM
I think you can use AJAX to submit a logout form when the window closes. Not sure though, as I haven't actually tried to do that. But based on my knowledge, Javascript can handle the close condition ("onunload"), and AJAX can submit a form, so I would guess you can do that.

djr33
05-08-2008, 04:34 AM
Logout is simple-- destroy the session.
Automatic logout isn't really needed-- the browser will reset the session on its own with enough time between.

phpsales
05-08-2008, 09:38 PM
First Option:

Have the session timeout due to inactivity.

MySQL Example:

-- log user
UPDATE `messagetable` SET `lastactive` = NOW WHERE `user` = 'x';

-- expire inactive users
DELETE FROM `messagetable` where `lastactive` < DATE_SUB(NOW(), INTERVAL 20 MINUTE);

Reference: http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-add

~~~

Second Option:

Use onunload attribute in html body tag to call session destroy event.

Ruff Example:


<body onunload='logout_user()'>
<script>
//JavaScript Function
function logout_user(){
//send logout request via ajax to php file
}
</script>

~~~
Note: PHP default session management will destory session data when all web browser windows are closed, cookie and session data is cleared or when the session_destroy() module is called.

Mike4x4
05-10-2008, 12:00 AM
Ok this is what i have so far, when users log into my chat it creates an insert qry to onlineusers, with the fields, username and lastactive. last active is a timestamp in mysql with something like this: 2008-05-04 16:06:23

I have a logout button where you click to logout when you have finished chatting and that deletes the row of your username so your username isnt displayed in the usersonline list, but i need a way to make it automatically do it so if they close the browser it deletes there row from the database.

How would i do that?

Jas
05-10-2008, 03:38 AM
. . . Again, like I and phpsales said, use AJAX. It can send the existing form when the user closes the browser. In other words, AJAX will essentially "click" the logout button when the user closes the browser (not litterally-- it will send the same data to the server, though in a slightly different way, thus causing the same effect; as far as the server is concerned, the user clicked the button). Check out a simple ajax tutorial here. (http://www.w3schools.com/Ajax/Default.Asp) (Not the best tutorial on the web, but if you already know JavaScript, it should be a breeze.)

An example of AJAX taken from the end of the tutorial:

<html>
<body>

<script type="text/javascript">
function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.myForm.time.value=xmlHttp.responseText;
}
}
xmlHttp.open("GET","time.asp",true);
xmlHttp.send(null);
}
</script>

<form name="myForm">
Name: <input type="text"
onkeyup="ajaxFunction();" name="username" />
Time: <input type="text" name="time" />
</form>

</body>
</html>

Mike4x4
05-10-2008, 04:15 AM
i'd like to avoid ajax and do it another way

phpsales
05-10-2008, 02:07 PM
Since you are not using AJAX we can assume you are using a HTML IFRAME that refreshes. AJAX or non-AJAX still means that the client page has to check in to the server (refresh iframe) for new messages every (x) amount of time. E.g. every 2 seconds.

1. Every 2 seconds the client checks in with the sever
2. The server receives the check in and updates the last active for client
3. The server then directly proceeds to remove all rows that are older than 2 seconds (use MySQL SQL I noted in earlier post).

This method doesn't have to depend on a web browser closing to kill a session. Any time a user (client) fails to check in with in the allowed time they are automatically timed out. E.g. browser close, network connection closes, etc...

Jas
05-10-2008, 11:43 PM
i'd like to avoid ajax and do it another way

Can I ask why? It shouldn't ruin anything existing on your website, though if you don't want to learn AJAX I understand.

If your not going with AJAX, I would go with phpsales (though the user can't possible refresh every 2 seconds, so you'll have to go with >=120 seconds or find another way to go on that bit; since this is a chat, the whole refresh concept might be a little annoying to users). That method is used by many sites for things like Who's Online stats. Not the more effective way to go, but it does work. And it sounds like you are already partially set up for that anyway (although your also set up for AJAX ;)).

djr33
05-11-2008, 01:48 AM
Again, why bother making it log out. The browser will deal with any timeouts, or you can just check the next time they do show up.

Jas
05-12-2008, 03:30 PM
Yes, but how would the server know? And the program isn't using sessions-- the users are in a table.