Jump to content

how do you catch a users session id when they close your website.


redarrow

Recommended Posts

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.

 

 

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.

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.

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;

?>

<?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;

?>

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.