redarrow Posted March 19, 2009 Share Posted March 19, 2009 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/150073-how-to-set-a-session-via-timestamp/ Share on other sites More sharing options...
pkSML Posted March 19, 2009 Share Posted March 19, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/150073-how-to-set-a-session-via-timestamp/#findComment-788160 Share on other sites More sharing options...
redarrow Posted March 19, 2009 Author Share Posted March 19, 2009 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> "; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/150073-how-to-set-a-session-via-timestamp/#findComment-788164 Share on other sites More sharing options...
redarrow Posted March 19, 2009 Author Share Posted March 19, 2009 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> "; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/150073-how-to-set-a-session-via-timestamp/#findComment-788165 Share on other sites More sharing options...
pkSML Posted March 19, 2009 Share Posted March 19, 2009 I'm still trying to understand what you're doing. So you have a session-based login system. Users have one minute to start downloading a podcast, otherwise they'll be logged out??? Quote Link to comment https://forums.phpfreaks.com/topic/150073-how-to-set-a-session-via-timestamp/#findComment-788169 Share on other sites More sharing options...
redarrow Posted March 19, 2009 Author Share Posted March 19, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/150073-how-to-set-a-session-via-timestamp/#findComment-788173 Share on other sites More sharing options...
pkSML Posted March 19, 2009 Share Posted March 19, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/150073-how-to-set-a-session-via-timestamp/#findComment-788180 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.