1

我正在使用来自http://tutorialzine.com/2009/10/cool-login-system-php-jquery/的 PHP 登录系统只是为了给你一个快速的概述,我相信本教程在下面设置了变量方式:

<?php
define('INCLUDE_CHECK',true);
require 'connect.php';
require 'functions.php';
// Those two files can be included only if INCLUDE_CHECK is defined

session_name('tzLogin');
// Starting the session

session_set_cookie_params(1*7*24*60*60);
// Making the cookie live for 1 weeks

session_start();
if($_SESSION['id'] && !isset($_COOKIE['tzRemember']) && !$_SESSION['rememberMe'])

..........

到目前为止一切顺利,除了我无法将会话变量从主登录页面转移到后续页面(包含受限内容)。这是我打算放在每个受限内容页面开头的基本代码

<?php
session_name('tzLogin');
session_set_cookie_params(1*7*24*60*60);
session_start();
    if($_SESSION['id'])  <-- I believe I need more code here (incldue the cookie)
{
//If all is well, I want the script to proceed and display the HTML content below.
}
else 
{
header("Location: MainLogin.html");
or die;
//redirects user to the main login page.
}
?>

正如你所看到的,我是一个完全的新手,但任何帮助将不胜感激。截至目前,即使我正确登录,我的受限内容页面也会不断被重定向到主页。因此我怀疑 SESSION 状态没有被延续。再次感谢!

4

1 回答 1

0

您可能应该确保在调用 session_set_cookie_params 时设置路径和域:

session_set_cookie_params ( 1*7*24*60*60, '/','.yourdomain.com')

请参阅http://php.net/manual/en/function.session-set-cookie-params.php (最好也设置 httpOnly 属性。)

此外,请确保您实际上为会话 id 键分配了一些值(在您的代码示例中并不清楚您所做的):

$_SESSION['id'] = 'some value';

最后,您可能希望在调试时使用 session_status() 来验证您实际上是否正确启动了会话 (http://php.net/manual/en/function.session-status.php)。

于 2013-01-25T04:57:45.457 回答