4 Replies - 3736 Views - Last Post: 11 July 2011 - 05:14 PM Rate Topic: -----

#1 drayarms  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 177
  • Joined: 18-May 11

How Does One Make a Session Persist?

Posted 11 July 2011 - 02:27 PM

I'm creating a site where members have to log in in order to access the site's pages. I'm using sessions to log the members in. Each member has a unique session id that grants him access to the site. At the top of each of the sit'es pages, I authenticate the user using the following code.
<?php
	//Start session
	session_start();
	
	//Check whether the session variable id is present or not
	if(!isset($_SESSION['id']) || (trim($_SESSION['id']) == '')) {
		header("location: access_denied.php");
		exit();
	}
?>





So this bit of code basically redirects the user to an access denied page, if the user isn't authenticated otherwise, the user can access the page. Right below that bit of code, I have this code that ends the session after 15 minutes of inactivity.
<?php


//address error handling

ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);

//Get the current page url to pass on to the session expired page.
$url=urlencode("http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);


if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 900)) {
    // last request was more than 15 minates ago
    session_destroy();   // destroy session data in storage
    session_unset();     // unset $_SESSION variable for the runtime
    header("location: session_expired.php?url=$url");
}
$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp


?>




What do I need to do to keep the session permanently active until either the 15 minutes is reached or I manually sign out ? Often times the page upon refreshing, gets redirected to the access_denied.php which clearly indicates that my session id is not lingering on and gets destroyed prematurely. What do I do??

Is This A Good Question/Topic? 0
  • +

Replies To: How Does One Make a Session Persist?

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3489
  • View blog
  • Posts: 10,057
  • Joined: 08-June 10

Re: How Does One Make a Session Persist?

Posted 11 July 2011 - 02:35 PM

usually a sessions timeout is determined via the session.gc_maxlifetime and session.cookie_lifetime settings so there is no need for manual timeout control.
Was This Post Helpful? 0
  • +
  • -

#3 drayarms  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 177
  • Joined: 18-May 11

Re: How Does One Make a Session Persist?

Posted 11 July 2011 - 03:43 PM

@So is there anyway the developer can have control over these settings? Sometimes it only takes a few minutes for my sessions to end.
Was This Post Helpful? 0
  • +
  • -

#4 drayarms  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 177
  • Joined: 18-May 11

Re: How Does One Make a Session Persist?

Posted 11 July 2011 - 05:04 PM

@Dormilich So is there anyway the developer can have control over these settings? Sometimes it only takes a few minutes for my sessions to end.
Was This Post Helpful? 0
  • +
  • -

#5 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: How Does One Make a Session Persist?

Posted 11 July 2011 - 05:14 PM

You'll have to change the values in your php.ini file. Read this
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1