redarrow Posted June 22, 2007 Share Posted June 22, 2007 advance thank you please can some one exsplain how to catch a session when a user close the browser. For example if i want to update the database with the current users id that closed there browser how can i do that, I am baffled becouse if they close the browser the session gets destroed, so were the id comming from to update the database cheers. code examples please tried everythink. Quote Link to comment Share on other sites More sharing options...
king arthur Posted June 22, 2007 Share Posted June 22, 2007 You will need the javascript "onclose" event for that. There is no way on the face of it that the server on its own can tell what the user does with his browser. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 22, 2007 Share Posted June 22, 2007 and then you need the onclose to be ajax Quote Link to comment Share on other sites More sharing options...
redarrow Posted June 22, 2007 Author Share Posted June 22, 2007 ok then how can i set the user's session will last 1 hour then before the hour update the database then cheers. Quote Link to comment Share on other sites More sharing options...
king arthur Posted June 22, 2007 Share Posted June 22, 2007 and then you need the onclose to be ajax Well not necessarily, you could possibly pass the session ID to the javascript which then loads in a dummy image script with the session ID as a parameter, the image script performs the necessary functionality then creates a 1x1 pixel image which never gets used. Quote Link to comment Share on other sites More sharing options...
king arthur Posted June 22, 2007 Share Posted June 22, 2007 ok then how can i set the user's session will last 1 hour then before the hour update the database then cheers. What is it that you're actually wanting to do? Quote Link to comment Share on other sites More sharing options...
redarrow Posted June 22, 2007 Author Share Posted June 22, 2007 i got a online and off line code working fine, But i need it to update the database if the session is not there in overwords the user closed there browser. i thort but dont no how? set a session to the time i thort the user would use the website then just before the session run out update the database with that session. Quote Link to comment Share on other sites More sharing options...
redarrow Posted June 22, 2007 Author Share Posted June 22, 2007 code example ok. Know how to add the part where if the user is not online. ok i got to add another query to update all the database for on_of to a 0. <?php session_start(); if(isset($_SESION['id'])){ $query="SELECT FROM `online` WHERE `id`='".$_SESSION['id']."'"; $result=mysql_query($query)or die("mysql_error"); if(mysql_num_rows($result)=="0"){ $id=$_SESSION['id']; $ip=$_SERVER['REMOTE_ADDR']; $date=time(); $on_of=0; $id=addslashes($_POST['id']); $ip=addslashes($_POST['ip']); $date=addslashes($_POST['date']); $on_of=addslashes($_POST['on_of']); $query2="INSERT INTO `online`(id,date,ip,on_of)values('$id','$date','$ip','$on_of')"; $result2=mysql_query($query2); }else{ $query3="UPDATE `online` set on_of=1 where id='$id'"; $result2=mysql_query($query2); } } $query4="SELECT FROM ONLINE where on_of=1 and id='$id'"; $result=mysql_query($query4); while($rec=mysql_fetch_assoc($result4)){ if($rec['on_of']=="1"){ $online=" ".$rec['name']." online "; } } echo $online; ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted June 22, 2007 Author Share Posted June 22, 2007 <?php session_start(); if(isset($_SESSION['id'])){ $query="SELECT FROM `online` WHERE `id`='".$_SESSION['id']."'"; $result=mysql_query($query)or die("mysql_error()"); if(mysql_num_rows($result) == 0){ $id=$_SESSION['id']; $ip=$_SERVER['REMOTE_ADDR']; $date=time(); $on_of=0; $id=addslashes($_POST['id']); $ip=addslashes($_POST['ip']); $date=addslashes($_POST['date']); $on_of=addslashes($_POST['on_of']); $query2="INSERT INTO `online`(id,date,ip,on_of)values('$id','$date','$ip','$on_of')"; $result2=mysql_query($query2); }else{ $query3="UPDATE `online` set on_of=1 where id='$id'"; $result2=mysql_query($query2); } } $query4="SELECT FROM ONLINE where on_of=1"; $result=mysql_query($query4); while($rec=mysql_fetch_assoc($result4)){ if($rec['on_of']=="1"){ $online=" ".$rec['name']." online "; }else{ $online=" ".$rec['name']." offline "; } } echo $online; ?> Quote Link to comment Share on other sites More sharing options...
SycoSyco Posted June 22, 2007 Share Posted June 22, 2007 Or log the sessionid in a file with a timestamp when a page loads and as users move around the site check the table for inactive sessions. You can check the where people are in your site by logging the urls too. Quote Link to comment Share on other sites More sharing options...
tcollie Posted June 22, 2007 Share Posted June 22, 2007 or do what i do, simply run a cron job every 5 minutes (or however long you want the interval to be) to delete records from your "online" table where the last_active time is greater than X minutes from the current time. Here's how mine works. My cron job runs every five minutes. If there are any records where the last active time is greater than the current time - 10 minutes, then I delete them from the "online" table. That help any? Quote Link to comment Share on other sites More sharing options...
redarrow Posted June 22, 2007 Author Share Posted June 22, 2007 code example please good help cheers. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.