Jump to content

how to set a session via timestamp


redarrow

Recommended Posts

How do you set a session via a time stamp.

 

please help and explain your code so i get the drift please.

 

i am trying to create a online offline plug in.

 

The code does not work, as i don't no how to set the session,

in time stamp, and delete after a set time.

 

example


<?php

// if usernmame via session.
if($_SESSION['username']){

//The user's current login time plus 1 minute.
$_SESSION['timestamp']=+60;


// if the session is set.
if(isset($_SESSION['timestamp'])){

// user is online.
echo "user is on line!";

}else{

// if session less then 60 user offline.
if($_SESSION['timestamp']-60){

	//unset the session.
	unset($_SESSION['timestamp']);

	// show user is online.
echo"user not online!";
}
}

}else{
//if session username is not there. 
header("location: http://www.google.com");
exit;
}
?>

Link to comment
Share on other sites

Your logic doesn't make sense to me.

 

Assuming user A is inquiring about user B: User A would not have User B anywhere in his session.

Every time someone inquires of their own session, they are definitely online.

 

Here's the logic that makes sense:

You'll need a MySQL database that records that timestamp of each user's last action (there would need to be a login system). Then your script would inquire about a user's online/offline status through the database. Session data would only serve to keep the user's username stored and available to write an updated timestamp to the database.

Link to comment
Share on other sites

look at this tell me where i am going wrong please.

 

i need the session to expire after 1 min so i can see online and offline users.

 

the code when done will be added to the header off the project.

 

 

if(isset($_SESSION['online_users']==time())){ << do i add this?

<?php

$_SESSION['podcast_user_id']=24;


$sql4="SELECT * FROM podcast_users WHERE podcast_user_id='{$_SESSION['podcast_user_id']}' AND podcast_activated='yes'";

$res4=mysql_query($sql4)or die("Select users error\n".mysql_error());


if(mysql_num_rows($res4)){
   
  $timestamp=time();
   
   $users_ip=$_SERVER['REMOTE_ADDR'];
   
   $sql5="INSERT INTO online_offline(users_id,online_timestamp,users_ip)VALUES('".$_SESSION['podcast_user_id']."','".mysql_real_escape_string($timestamp)."','".mysql_real_escape_string($users_ip)."')";
   

   
$res5=mysql_query($sql5)or die("Update online offline error\n".mysql_error());
}

$sql6="SELECT * FROM online_offline";

$res6=mysql_query($sql6)or die("SELECT online offline error\n".mysql_error());

while($online_data=mysql_fetch_assoc($res6)){

   $_SESSION['online_offline_id']=$online_data['online_offline_id'];
   
   $_SESSION['online_users']=$online_data['online_timestamp'];
   
   $_SESSION['online_users']+60;
   
   
   if(isset($_SESSION['online_users'])){
      
     echo "user with the id of:\n {$_SESSION['online_offline_id']} is online! <br>";
      
      }else{
      
      	unset($_SESSION['online_user']);
      	
      	echo "user with the id of:\n {$_SESSION['online_offline_id']} is NOT online!<br> ";
   }
   
}

?>

Link to comment
Share on other sites

this code sort off works you tell me if it correct please.

 


<?php session_start();

$_SESSION['podcast_user_id']=24;


$sql4="SELECT * FROM podcast_users WHERE podcast_user_id='{$_SESSION['podcast_user_id']}' AND podcast_activated='yes'";

$res4=mysql_query($sql4)or die("Select users error\n".mysql_error());


if(mysql_num_rows($res4)){
   
  $timestamp=time();
   
   $users_ip=$_SERVER['REMOTE_ADDR'];
   
   $sql5="INSERT INTO online_offline(users_id,online_timestamp,users_ip)VALUES('".$_SESSION['podcast_user_id']."','".mysql_real_escape_string($timestamp)."','".mysql_real_escape_string($users_ip)."')";
   

   
$res5=mysql_query($sql5)or die("Update online offline error\n".mysql_error());
}

$sql6="SELECT * FROM online_offline";

$res6=mysql_query($sql6)or die("SELECT online offline error\n".mysql_error());

while($online_data=mysql_fetch_assoc($res6)){

   $_SESSION['online_offline_id']=$online_data['online_offline_id'];
   
   $_SESSION['online_users']=$online_data['online_timestamp'];
   
   $_SESSION['online_users']+60;
   
   $time_now=time();
   
   if(($_SESSION['online_users'])==($time_now)){
      
     echo "user with the id of:\n {$_SESSION['online_offline_id']} is online! <br>";
      
      }else{
      
      	unset($_SESSION['online_user']);
      	
      	echo "user with the id of:\n {$_SESSION['online_offline_id']} is NOT online!<br> ";
   }
   
}

?>

Link to comment
Share on other sites

no i set it to 1 min for example only.

 

i want a set session, so a user will be logged out after 30 minutes

 

if they want to continue they log in again.

 

i need to find a way if the user continues to use the web site the session keeps but if they log out there offline.

 

like this web site ...

if a user close the browser then there still online so i fort sending them to the log in page is best after a set time.

Link to comment
Share on other sites

You can set a timeout in the session itself.

 

Continually update the session on every page with an expiry X amount of seconds into the future.

The way you do this is with session_set_cookie_params

session_set_cookie_params must be set before you run session_start

 

 

On the server side, set the session.gc_maxlifetime specifies the number of seconds after which data will be seen as 'garbage' and cleaned up. This can be found in your PHP.ini file.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.