php使用重定向保留跨页面的会话

时间:2016-07-28 12:13:10

标签: php session

我的问题类似于Displaying user name and user id from $_SESSION 但是,我会尝试详细解决这个问题。我正在修改现有代码以添加"登录到期"和"登录页面重定向"。两个修改都在单独的包含文件中。如果我排除登录到期文件,登录页面重定向工作正常。

问题是$_SESSION['url']是在page_one.php中设置的,但只要timeout.php包含有效,它就不会在login.php中保留会话?如果我在请求页面(page_one.php)中注释掉timeout.php,则会话按设计工作。

BTW:session_id在所有页面中都是相同的。

登录到期(timeout.php):

<?php
if(!isset($_SESSION['user_id']) || (time() - $_SESSION['login_time'] > 1200)){
header("Location: {$home_url}logout.php");
}
else{
$_SESSION['last_login_time'] = $_SESSION['login_time'];
// reset timer on page refresh 
$_SESSION['login_time'] = time();
}
?>

页面重定向(redirect_login.php):

<?php
$_SESSION['url'] = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
?>

(login.php中):

// if access level is 'Admin', redirect to admin section
    if($user->access_level=='Admin'){

        if(isset($_SESSION['url'])) {
            $myurl = $_SESSION['url']."?action=login_success"; 
        }
        else{
            $myurl = $home_url . "admin/index.php?action=login_success";
        }
    header("Location:  {$myurl}");
    }

    // else, redirect only to 'Customer' section
    else{
        header("Location: {$home_url}index.php?action=login_success");
    }

(page_one.php):

<?php
// *** this contains the session_start() for each page ***
include_once "php-login-system/config/core.php";

include "php-login-system/redirect_login.php";

include_once "php-login-system/admin/login_checker.php";

$page_title="Page One.1";

include_once 'php-login-system/admin/layout_head.php';

// 20 min timer on logged in user. *** Problem if enabled ***
include_once 'php-login-system/timeout.php';

echo "<div class='col-md-12'>";

// get parameter values, and to prevent undefined index notice
$action = isset($_GET['action']) ? $_GET['action'] : "";

if($action=='already_logged_in'){
    echo "<div class='alert alert-info'>";
        echo "<strong>You</strong> are already logged in.";
    echo "</div>";
}

else if($action=='logged_in_as_admin'){
    echo "<div class='alert alert-info'>";
        echo "<strong>You</strong> are logged in as admin.";
    echo "</div>";
}

echo "<div class='alert alert-success'>";

$logintimestamp = $_SESSION['last_login_time'];
    echo date("H:i:s", $logintimestamp) . "<br>";
$timestamp = time();
    echo date("H:i:s", $timestamp) . "<br>";
    echo ("The session time was just reset from: ". $_SESSION['login_time'] . " to: " . time()); 

echo "</div>";
?>

<div class='alert alert-info'>
login time: <?php echo $_SESSION['login_time']; ?><br>
login id: <?php echo $_SESSION['user_id']; ?><br>
login logged T/F: <?php echo $_SESSION['logged_in']; ?><br>
login access level: <?php echo $_SESSION['access_level']; ?><br>
login last name: <?php echo $_SESSION['firstname']; ?><br>
current URL: <?php echo $_SESSION['url']; ?><br>
session id: <?php echo session_id(); ?>
</div>      

<?php
echo "</div>";
include_once 'php-login-system/admin/layout_foot.php';
?>

0 个答案:

没有答案