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;
}
如果我手动将会话超时设置在每个页面中,则编码工作正常。但是,如果我尝试将它存储在另一个文件中并将其调用到每个页面,它将被忽略并且我不会得到任何会话超时。
例子
require("database.php");
require("expired.php");
if(empty($_SESSION['user']))
{
header("Location: login.php");
die("Redirecting to login.php");
}
expired.php 包含与我的 //check session timeout 相同的编码,任何帮助将不胜感激。谢谢。
编辑以包含我的 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();