PHP - make session expire after X minutes -


i using following technique...

from login.php form posts page check.php this

<?php     $uzer = $_post['user_name']; $pass = $_post['user_pass'];  require ('db_connection.php');  $result = mysql_query("select * accounts user_name='$uzer' , user_pass='$pass'");  if( mysql_num_rows( $result ) > 0) {     $array = mysql_fetch_assoc($result);          session_start();     $_session['user_id'] = $uzer;     header("location:loggedin.php");             } else {     header("location:login.php"); } ?> 

and on loggedin.php page first thing is

<?php session_start(); if( !isset( $_session['user_id'] ) ) {     header("location:login.php"); } else {     echo ( "this session ". $_session['user_id'] );     //show rest of page , } ?> 

but once logged in when directly type url localhost\myproject\loggedin.php displays page...which makes perfect sense because session has started

what want implement is

  • the direct url \ session works 10 minutes after session terminated\expired\timed out , use must login again , may same session id after 10 minutes use won't able browse same session

what need or learn

store timestamp in session:

<?php     $uzer = $_post['user_name']; $pass = $_post['user_pass'];  require ('db_connection.php');  // hey, escape input if necessary! $result = mysql_query(sprintf("select * accounts user_name='%s' , user_pass='%s'", mysql_real_escape_string($uzer), mysql_real_escape_string($pass));  if( mysql_num_rows( $result ) > 0) {     $array = mysql_fetch_assoc($result);          session_start();     $_session['user_id'] = $uzer;     $_session['login_time'] = time();     header("location:loggedin.php");             } else {     header("location:login.php"); } ?> 

check if timestamp within allowed time window (600 seconds 10 minutes):

<?php session_start(); if( !isset( $_session['user_id'] ) || time() - $_session['login_time'] > 600) {     header("location:login.php"); } else {     // uncomment next line refresh session, expire after ten minutes of inactivity, , not 10 minutes after login     //$_session['login_time'] = time();     echo ( "this session ". $_session['user_id'] );     //show rest of page , } ?> 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -