require("database.php");
if(empty($_SESSION['user']))
{
header("Location: login.php");
die("Redirecting to login.php");
}
//check session timeout
$now = time();
$limit = $now - 60 * 1;
if (isset ($_SESSION['last_activity']) && $_SESSION['last_activity'] < $limit)
{
$_SESSION = array();
header('Location: login.php');
exit;
}
else {
$_SESSION['last_activity'] = $now;
}
The coding works fine if I put the session timeout in each and every page manually. But if I try to store it in another file and call it to each page,it will be ignored and i wont get any session time out.
Example
require("database.php");
require("expired.php");
if(empty($_SESSION['user']))
{
header("Location: login.php");
die("Redirecting to login.php");
}
expired.php contains the same coding as my //check session timeout, Any help will be appreciated. Thanks.
Edited to include my database.php
$username = "";
$password = "";
$host = "";
$dbname = "";
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
try
{
$db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
}
catch(PDOException $ex)
{
die("Failed to connect to the database: " . $ex->getMessage());
}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
{
function undo_magic_quotes_gpc(&$array)
{
foreach($array as &$value)
{
if(is_array($value))
{
undo_magic_quotes_gpc($value);
}
else
{
$value = stripslashes($value);
}
}
}
undo_magic_quotes_gpc($_POST);
undo_magic_quotes_gpc($_GET);
undo_magic_quotes_gpc($_COOKIE);
}
header('Content-Type: text/html; charset=utf-8');
if(!isset($_SESSION)){
session_start();
}
//session_start();