Jump to content

login using sessionid or time

- - - - -

  • Please log in to reply
1 reply to this topic

#1
ravi951

ravi951

    Newbie

  • Members
  • PipPip
  • 18 posts
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.....

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200

Quote

($_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;
}

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users