这是我的问题,我可以说2页第1页和第2页,我需要的是打开会话并在第1页设置cookie,它将保持1小时,并将在每次页面刷新时重置。现在第2页的故事情节略有不同,首先,第2页不应该打开任何会话,而是应该检查第1页上打开的任何会话是否仍然有效,如果是,那么继续进行任何请求,但是,如果访问者在会话cookie过期后的任何时间访问第2页,或者访问第2页,则访问者应该重定向到第1页。
这是我到目前为止在第1页上所做的工作
<?php
function startSession($time = 3600, $ses = 'MYSES') {
session_set_cookie_params($time);
session_name($ses);
session_start();
// Reset the expiration time upon page load
if (isset($_COOKIE[$ses]))
setcookie($ses, $_COOKIE[$ses], time() + $time, "/");
}
?>
问题是,我不知道该怎么做以及如何做其余的事情以及我应该在第2页上做些什么?
这是我在第2页上尝试的内容,但它没有用。
<?php
if (!isset($_SESSION));{
$a = session_id();
if(empty($a) and $_SERVER['HTTP_REFERER']);{
header('location: page1.html');}}
?>
请帮帮我们。
答案 0 :(得分:1)
除了语法问题,看起来你根本不使用$ _SESSION,使用$ _SESSION你必须在任何输出之前声明session_start()。因此,在您的情况下,可能只使用Cookie。
第1页(page1.php):
<?php
function extendCookie($time = 3600) {
setcookie('MYSES', 'dummy var', time() + $time, "/");
}
extendCookie(); //extend cookie by 3600 seconds (default)
?>
You are on page 1.<br />
<a href="page2.php">Click to proceed to page 2</a>
第2页(第2.2页):
<?php
if (!isset($_COOKIE['MYSES'])){
header('location: page1.php');
}
?>
You are on page 2.
如果你想使用会话,那就是这样:
第1页(page1.php):
<?php
session_start();
function extendSession($time = 3600) {
$_SESSION['expire_time'] = time() + $time;
}
extendSession(7200); //extend session by 7200 seconds
?>
You are on page 1.<br />
<a href="page2.php">Click to proceed to page 2</a>
第2页(第2.2页):
<?php
session_start();
if (!isset($_SESSION) || $_SESSION['expire_time'] < time()){
session_destroy(); //Optional, destroy the expired session.
header('location: page1.php');
}
?>
You are on page 2.