会话变量在页面加载之间不保留
有人可以告诉我为什么会话变量不在页面之间传递吗?他们一直在工作到2天前。现在不是吗?有一个基于第三方系统让用户登录的第三方系统。我将用户定向到带有返回网址的登录页面。第三方系统将用户登录并传递其ID和在其端生成的令牌,并将其与URL中的ID和令牌一起返回到我的站点。
如果未设置会话,我会尝试从URL中获取ID和令牌并设置会话。(工作)然后,当我转到单击我设置的会话不为空的另一个页面时,我生成自己的令牌以验证从第三方系统传递的令牌(工作)(???? 工作)
这是我的代码:
<?php
session_start();
// FUNCTION TO PASS THE URL THE USER IS ON SO THEY COME
// BACk TO THIS PAGE AFTER THE LOG IN. IF APPLICABLE
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
// DESTROY SESSION INFO IF TIMED OUT
if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) {
session_destroy(); // destroy session data in storage
session_unset(); // unset $_SESSION variable for the runtime
}
// SET THE SESSIONS WITH INFO PASSED FROM
// LOGIN PAGE SENT AS A GET
if(isset($_SESSION['ID']) && isset($_SESSION['token'])) {}else{
$_SESSION['ID'] = $_GET['ID'];
$_SESSION['token'] = $_GET['token'];
}
// GENERATE MY TOKEN TO MATCH THE LOGIN SYSTEM TOKEN
$userIP = $_SERVER['REMOTE_ADDR'];
$secretkey = 'A Unique Key For The Logged In User Matching the Login System Passed From mydomain.com/login.php';
$algorithm = 'md5';
$mm = date('m');
$dd = date('d');
$mmdd = $mm.$dd;
$mytoken = strtoupper(hash($algorithm, $secretkey.$_SESSION['ID'].$userIP.$mmdd));
$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
// THIS IS WHERE THINGS ARE GOING WRONG
// SESSION token IS NO LONG SET AFTER I Go To another page
// and my token isnt the same any more either because session ID
// is no longer set???
if($_SESSION['token']==$mytoken){}else{
header("location: https://mydomain.com/login.php?returnURL=".curPageURL());
}
?>
好吧,这搞砸了。这必须是托管服务提供商PHP设置上的问题,我认为因为我创建了两个页面。一个调用信息与此代码:
<?
session_start();
$_SESSION['ID'] = "112233";
$_SESSION['token'] = "mytoken";
print $_SESSION['ID'];
print $_SESSION['token'];
?>
<a href="info2.php">info 2</a>
和一个名为 info2 的代码:
<?
session_start();
print $_SESSION['ID'];
print $_SESSION['token'];
?>
<a href="info.php">info</a>
信息已创建并打印会话正常。当我点击链接转到info2时,会话不会打印。这是托管配置问题吗?