简体   繁体   English

Session 变量在页面加载之间不持久

[英]Session variables are not persisting between page loads

Can someone tell me why the session vars are not passing between pages?有人能告诉我为什么 session 变量不在页面之间传递吗? They were working up to 2 days ago.他们工作到 2 天前。 Now its not?现在不是吗? There is a third party system that logs users in based on the third party system.有一个第三方系统,基于第三方系统登录用户。 I direct users to the login page with the return url.我将用户引导到登录页面,返回 url。 The third party system logs a user in and passes their id and a token generated on their end and returns them to my site with the id and the token in the url.第三方系统将用户登录并传递他们的 id 和最终生成的令牌,并将其返回到我的站点,其中 id 和令牌在 url 中。

If sessions are not set i try and grab the id and the token from the url and set the sessions.如果未设置会话,我尝试从 url 中获取 id 和令牌并设置会话。 (working) I then generate my own token to validate against the token passed from the third party system (working) when i go to click to another page the sessions i set are not empty (????) (工作)然后我生成自己的令牌以验证从第三方系统传递的令牌(工作),当我 go 单击另一个页面时,我设置的会话不为空(????)

Here is my code:这是我的代码:

    <?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());
    }
    ?>

ok this is messed up.好的,这搞砸了。 It has to be a problem on the hosting providers PHP setup i think because i created two pages.我认为托管服务提供商 PHP 设置一定是个问题,因为我创建了两个页面。 one called info with this code:使用此代码调用信息:

<?
session_start();

$_SESSION['ID'] = "112233";
$_SESSION['token'] = "mytoken";

print $_SESSION['ID'];
print $_SESSION['token'];
?>
<a href="info2.php">info 2</a>

and one called info2 with this code:一个叫 info2 的代码:

<?
session_start();

print $_SESSION['ID'];
print $_SESSION['token'];
?>
<a href="info.php">info</a>

info created and printed the session ok.信息创建并打印了 session 好的。 when i click the link to go to info2 the sessions dont print.当我单击 go 的链接到 info2 时,会话不打印。 Is this a hosting config problem?这是托管配置问题吗?

As already mentioned, ensure you're calling session_start() on each page.如前所述,确保您在每个页面上都调用session_start()

Additionally, are the scripts on different subdomains??此外,脚本是否在不同的子域上? If they are you should set the INI value session.cookie_domain to .DOMAIN.EXT .如果是,则应将 INI 值session.cookie_domain设置为.DOMAIN.EXT

To further debug this whole situation, do some simple cookie watching.为了进一步调试整个情况,做一些简单的 cookie 观察。 See if PHPSESSID is present as a cookie on both page requests, if it's not then this is your problem.查看PHPSESSID是否在两个页面请求中都作为 cookie 存在,如果不是,那么这就是您的问题。 You can't store cookies cross-domain unless you reconstruct them.除非您重建它们,否则您不能跨域存储 cookies。


In response to your update, try doing this underneath your call to session_start() :为了响应您的更新,请尝试在调用session_start()下执行此操作:

echo session_id();

Confirm that it's the same on both pages.确认两个页面上的内容相同。 If not, check the value of session.cookie_domain like this:如果不是,请检查session.cookie_domain的值,如下所示:

echo ini_get('session.cookie_domain');

Is that set to anything?那是什么设置吗? By default it should be blank, if it's set, especially not to your domain, this is the problem.默认情况下它应该是空白的,如果它被设置,特别是不是你的域,这就是问题所在。

You can also try debugging the cookie value of PHPSESSID like I first suggested.您也可以尝试像我最初建议的那样调试PHPSESSID的 cookie 值。

Check List检查清单
1. Make sure that you have used session_start(); 1. 确保你已经使用了 session_start(); in the next page.在下一页。

2. Are you using .htaccess file? 2.您使用的是.htaccess文件吗?
if so remove the .htaccess file and check the same.如果是这样删除 .htaccess 文件并检查相同。
some time rewrite rules cause session probs...一些时间重写规则导致 session 问题...

3. If session is working fine and you have trouble only with token, then check the token sent in url is url_encoded. 3. 如果 session 工作正常而您只有令牌有问题,则检查 url 中发送的令牌是否为 url_encoded。

it's not the hosting server issue...这不是托管服务器的问题...

check your URLs检查您的网址

if a user is login under "example.com" session will be stored for "example.com" and not "WWW.example.com" so if a link goes to www.example.com it will not have that session.如果用户在“example.com”下登录 session 将存储为“example.com”而不是“WWW.example.com”,因此如果链接转到 www.example.com 它将没有 Z21D6F40CFB5119082E74E74

you can use htaccess to always set the url to "WWW.example.com" use below code for it您可以使用 htaccess 始终将 url 设置为“WWW.example.com”,使用下面的代码

RewriteEngine On重写引擎开启

RewriteCond %{HTTP_HOST} ^hemantjadhav.com$ [NC] RewriteCond %{HTTP_HOST} ^hemantjadhav.com$ [NC]

RewriteRule ^(.*)$ http://www.hemantjadhav.com/ $1 [L,R=301] RewriteRule ^(.*)$ http://www.hemantjadhav.com/ $1 [L,R=301]

(replace hemantjadhav with your domain name) (将 hemantjadhav 替换为您的域名)

Check the size of the session file: (code taken from this post )检查 session 文件的大小:(代码取自这篇文章

$sessionfile = ini_get('session.save_path') . '/' . 'sess_'.session_id();  
echo 'session file: ', $sessionfile, ' ';  
echo 'size: ', filesize($sessionfile), "\n";

If your session file has zero size, make sure there is still disk space available on your server.如果您的 session 文件大小为零,请确保您的服务器上仍有可用磁盘空间。 That was the problem I had.这就是我遇到的问题。

Check disk space with df -h on a linux server.在 linux 服务器上使用df -h检查磁盘空间。

The only answer for this problem is to use session_start();这个问题的唯一答案是使用session_start(); on the top of every page.在每一页的顶部。 It will work fine.它会正常工作。 Else you might need to contact your hosting provider about this problem.否则,您可能需要就此问题联系您的托管服务提供商。

The answer to this is it was a hosting configuration error.答案是它是一个主机配置错误。 Hosting company changed something and it has worked ever since.托管公司改变了一些东西,并且从那以后一直有效。

In my case the solution was to have different parameter names in $_GET and $_SESSION.就我而言,解决方案是在 $_GET 和 $_SESSION 中使用不同的参数名称。

$_SESSION["businessid"] = $_GET["businessid"]; // Leads to problems with session. $_SESSION["business_id"] = $_GET["businessid"]; //Works perfectly.

It sounds strange but that's my experience.这听起来很奇怪,但这是我的经验。

I would add that I got caught up with the same problem, except that in my case page was behind Varnish caching proxy and I missed out that configuration had a line where cookies were allowed only on specific paths, otherwise they would get removed with the following directive:我要补充一点,我遇到了同样的问题,除了在我的情况下,页面位于Varnish缓存代理后面,我错过了配置有一行 cookies 只允许在特定路径上使用,否则它们会被以下内容删除指示:

unset req.http.cookie;

Dont forget to also check your proxy settings .不要忘记检查您的代理设置

I had session.cookie_samesite = "Strict" in my runtime file and was trying to bounce my user from Oauth2.0 back to my site and the PHP session ID was getting erased when the redirects hit.我的运行时文件中有session.cookie_samesite = "Strict"并试图将我的用户从 Oauth2.0 反弹回我的站点,并且 PHP Z21D6F40CFB511982E4424 IDE0 被删除时被重定向。 I removed this from my runtime file and it works fine now.我从我的运行时文件中删除了它,现在它工作正常。

For anyone else searching this in frustration - another thing to check is the cookie_secure setting in php.ini.对于其他沮丧地搜索此内容的人 - 要检查的另一件事是 php.ini 中的 cookie_secure 设置。

If cookie_secure=1 , cookies will only be sent and persist on secure connections.如果cookie_secure=1 , cookies 将仅在安全连接上发送并持续存在。 In our case, the site was deployed to an environment that did not have an ssl setup yet.在我们的例子中,该站点被部署到一个还没有 ssl 设置的环境中。

Set cookie_secure back to its default (0) - or get the site secured.将 cookie_secure 设置回其默认值 (0) - 或确保站点安全。

Make sure both pages are on the same domain.确保两个页面都在同一个域中。 Even www.site.com is different than site.com甚至 www.site.com 也不同于 site.com

If the above solutions do not work I suggest you do the following right before you set the new session variables:如果上述解决方案不起作用,我建议您在设置新的 session 变量之前执行以下操作:

    session_destroy();
    unset($_SESSION);
    session_start();

and THEN save the new session variables that were not persisting before然后保存之前没有持续存在的新 session 变量

You did not call session_write_close ()你没有调用session_write_close ()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM