if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) {// last request was more than 30 minutes agosession_unset(); // unset $_SESSION variable for the run-timesession_destroy(); // destroy session data in storage}$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
if (!isset($_SESSION['CREATED'])) {$_SESSION['CREATED'] = time();} else if (time() - $_SESSION['CREATED'] > 1800) {// session started more than 30 minutes agosession_regenerate_id(true); // change session ID for the current session and invalidate old session ID$_SESSION['CREATED'] = time(); // update creation time}
<?phpsession_start();?>
<html><form name="form1" method="post"><table><tr><td>Username</td><td><input type="text" name="text"></td></tr><tr><td>Password</td><td><input type="password" name="pwd"></td></tr><tr><td><input type="submit" value="SignIn" name="submit"></td></tr></table></form></html>
<?phpif (isset($_POST['submit'])) {$v1 = "FirstUser";$v2 = "MyPassword";$v3 = $_POST['text'];$v4 = $_POST['pwd'];if ($v1 == $v3 && $v2 == $v4) {$_SESSION['luser'] = $v1;$_SESSION['start'] = time(); // Taking now logged in time.// Ending a session in 30 minutes from the starting time.$_SESSION['expire'] = $_SESSION['start'] + (30 * 60);header('Location: http://localhost/somefolder/homepage.php');} else {echo "Please enter the username or password again!";}}?>
HomePage.php
<?phpsession_start();
if (!isset($_SESSION['luser'])) {echo "Please Login again";echo "<a href='http://localhost/somefolder/login.php'>Click Here to Login</a>";}else {$now = time(); // Checking the time now when home page starts.
if ($now > $_SESSION['expire']) {session_destroy();echo "Your session has expired! <a href='http://localhost/somefolder/login.php'>Login here</a>";}else { //Starting this else one [else1]?><!-- From here all HTML coding can be done --><html>Welcome<?phpecho $_SESSION['luser'];echo "<a href='http://localhost/somefolder/logout.php'>Log out</a>";?></html><?php}}?>
<?php/**** Starts a session with a specific timeout and a specific GC probability.* @param int $timeout The number of seconds until it should time out.* @param int $probability The probablity, in int percentage, that the garbage* collection routine will be triggered right now.* @param strint $cookie_domain The domain path for the cookie.*/function session_start_timeout($timeout=5, $probability=100, $cookie_domain='/') {// Set the max lifetimeini_set("session.gc_maxlifetime", $timeout);
// Set the session cookie to timoutini_set("session.cookie_lifetime", $timeout);
// Change the save path. Sessions stored in teh same path// all share the same lifetime; the lowest lifetime will be// used for all. Therefore, for this to work, the session// must be stored in a directory where only sessions sharing// it's lifetime are. Best to just dynamically create on.$seperator = strstr(strtoupper(substr(PHP_OS, 0, 3)), "WIN") ? "\\" : "/";$path = ini_get("session.save_path") . $seperator . "session_" . $timeout . "sec";if(!file_exists($path)) {if(!mkdir($path, 600)) {trigger_error("Failed to create session save path directory '$path'. Check permissions.", E_USER_ERROR);}}ini_set("session.save_path", $path);
// Set the chance to trigger the garbage collection.ini_set("session.gc_probability", $probability);ini_set("session.gc_divisor", 100); // Should always be 100
// Start the session!session_start();
// Renew the time left until this session times out.// If you skip this, the session will time out based// on the time when it was created, rather than when// it was last used.if(isset($_COOKIE[session_name()])) {setcookie(session_name(), $_COOKIE[session_name()], time() + $timeout, $cookie_domain);}}
<?php$user = $_POST['user_name'];$pass = $_POST['user_pass'];
require ('db_connection.php');
// Hey, always escape input if necessary!$result = mysql_query(sprintf("SELECT * FROM accounts WHERE user_Name='%s' AND user_Pass='%s'", mysql_real_escape_string($user), mysql_real_escape_string($pass));
if( mysql_num_rows( $result ) > 0){$array = mysql_fetch_assoc($result);
session_start();$_SESSION['user_id'] = $user;$_SESSION['login_time'] = time();header("Location:loggedin.php");}else{header("Location:login.php");}?>
现在,检查时间戳是否在允许的时间窗口内(1800秒为30分钟)
<?phpsession_start();if( !isset( $_SESSION['user_id'] ) || time() - $_SESSION['login_time'] > 1800){header("Location:login.php");}else{// uncomment the next line to refresh the session, so it will expire after thirteen minutes of inactivity, and not thirteen minutes after login//$_SESSION['login_time'] = time();echo ( "this session is ". $_SESSION['user_id'] );//show rest of the page and all other content}?>
class Session{public static function init(){ini_set('session.gc_maxlifetime', 1800) ;session_start();}public static function set($key, $val){$_SESSION[$key] =$val;}public static function get($key){if(isset($_SESSION[$key])){return $_SESSION[$key];} else{return false;}}public static function checkSession(){self::init();if(self::get("adminlogin")==false){self::destroy();header("Location:login.php");}}public static function checkLogin(){self::init();if(self::get("adminlogin")==true){header("Location:index.php");}}public static function destroy(){session_destroy();header("Location:login.php");}}