Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

login using sessionid or time

session login

  • Please log in to reply
1 reply to this topic

#1 ravi951

ravi951

    CC Newcomer

  • Just Joined
  • PipPip
  • 18 posts

Posted 19 September 2011 - 10:54 PM

hi all,
i have written a code in php using sessions.below is the code......

<?php
if(!isset($_SESSION)) 
{
 session_start();
}
$now = time();
$limit = $now;
if(isset($_SESSION['last_activity']) && ($_SESSION['last_activity'] < $limit)) 
{
 $_SESSION = array();
 header('Location:logout.php');
 exit;
} 
else 
{
//the current time
$_SESSION['last_activity'] = $now;
}
?>
.
tell me how to modify in the above code that if the user is entering for first
time then using time() or sessionid it should be stored
or else if he is entering for first time then using current time() or sessionid
it must be stored.....
  • 0

#2 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 20 September 2011 - 03:40 AM

($_SESSION['last_activity'] < $limit)


If limit is the current time, it will always be larger their last activity.

It should be time() - $_SESSION['last_activity'] > $limit, where $limit is the length of the session (such as 3600, as in seconds)

If you do not understand how this works, you can look at this static example:
$last_activity = 100;
$current_time = 120; //imagine this is the current time in seconds

if($current_time - $last_activity > 20) { //the difference is greater than 20, the limit
echo "You've not been active for twenty seconds, logging you off.";
} else {
echo "Welcome back, your session expires next in " . ($current_time + 20) . " seconds.";
$last_activity = $current_time;
}

  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.






Also tagged with one or more of these keywords: session, login

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download