首次尝试时无法识别 PHP cookie 和会话值 [英] PHP cookie and session values not recognized on first attempt

查看:41
本文介绍了首次尝试时无法识别 PHP cookie 和会话值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是我第二次尝试,用一种新的、更加孤立和简洁的方式来演示这个问题.该问题与登录系统有关,其中会话变量似乎在第一次尝试时没有保存.但这是演示错误行为的精简代码:

OK, so here's my second attempt at this, with a new and much more isolated and concise way of demonstrating the problem. The problem pertains to a login system where session vars do not seem to save on the first attempt. But here is the stripped down code that demonstrates the errant behavior:

<?php

session_start();

echo "POST superglobal: <pre>" . print_r($_POST, true) . "</pre><hr/>";
echo "SESSION superglobal: <pre>" . print_r($_SESSION, true) . "</pre><hr/>";
echo "COOKIE superglobal: <pre>" . print_r($_COOKIE, true) . "</pre><hr/>";

$_SESSION['session_var'] = "SESSION VAR AVAILABLE";
setcookie("cookie_var", "COOKIE VAR AVAILABLE");

?>  
<html>
    <body>
        <form action="" name="frmPost" method="POST">
            <input type="submit" name="cmdSubmitPost" value="Submit" />
        </form>
    </body>
</html>

在本地计算机上的 localhost 上,此代码生成的内容如下:

On my localhost on my local computer, here's what this code produces:

初始页面加载后:

POST superglobal:
Array
(
)

SESSION superglobal:
Array
(
)

COOKIE superglobal:
Array
(
)

[Submit]

点击Submit一次后:

POST superglobal:
Array
(
    [cmdSubmitPost] => Submit
)

SESSION superglobal:
Array
(
    [session_var] => SESSION VAR AVAILABLE
)

COOKIE superglobal:
Array
(
    [cookie_var] => COOKIE VAR AVAILABLE
    [PHPSESSID] => hpkpft4hh1tqlm6e3r496bt5j2
)

[Submit]

这正是我所期望和需要的.我还临时注册了两个独立的免费 php 托管站点来测试这一点,它们都产生了与上述相同的结果.

This is exactly what I expect and need. I also signed up temporarily on two separate free php hosting sites to test this, and they both yielded the same results as above.

但是,在我的主要托管环境 Arvixe,结果有所不同.它是这样的:

However, at Arvixe, my main hosting environment, the results are different. There it goes like this:

初始页面加载后:

POST superglobal:
Array
(
)

SESSION superglobal:
Array
(
)

COOKIE superglobal:
Array
(
)

[Submit]

点击Submit一次后:

POST superglobal:
Array
(
    [cmdSubmitPost] => Submit
)

SESSION superglobal:
Array
(
)

COOKIE superglobal:
Array
(
)

[Submit]

第二次点击Submit后:

POST superglobal:
Array
(
    [cmdSubmitPost] => Submit
)

SESSION superglobal:
Array
(
    [session_var] => SESSION VAR AVAILABLE
)

COOKIE superglobal:
Array
(
    [cookie_var] => COOKIE VAR AVAILABLE
    [PHPSESSID] => 2k6ldfl2icdtj1k7sq5dc9qmj3
    [VC-NoCache] => 1
    [_asomcnc] => 1
)

[Submit]

我希望在 localhost 和其他临时免费 php 托管环境中创建的结果排除了相当简单的代码中的任何根本缺陷.

I hope that the results created in the localhost and other temporary free php hosting environments rules out any fundamental flaw in the fairly straightforward code.

好的,那么问题来了:在 Arvixe 托管环境中,为什么页面在第一次提交后无法识别 cookie 和会话值?我很确定真正的问题是为什么在第一次提交后无法识别 cookie 值?(这是真正的问题,因为这回答了为什么无法识别会话值...这是因为尚未识别 PHPSESSID cookie).

OK, so NOW the question: In the Arvixe hosting environment why does the page not recognize the cookie and session values after the first submit? I'm pretty sure that the REAL question is why is the cookie value not recognized after the first submit? (that's the real question because that answers why the session value isn't being recognized... it's because the PHPSESSID cookie isn't yet recognized).

php.ini 的值基本相同,但如果有特定值可能有助于在此处发布,请告诉我哪些值.

The php.ini values are essentially the same, but if there are particular values that may help to publish here, let me know which ones.

另外,我认为这个问题的答案将有效地解决其他三个 stackoverflow 问题:

ALSO, I assume that the answer to this question would effectively solve these other three stackoverflow questions:

PHP cookie 将在页面重新加载两次之前不会设置.怎么回事?

php 会话变量第一次尝试时未存储,第二次尝试后工作正常

第一次未设置会话变量

(那里的解决方案都不适合我)

(none of the solutions there worked for me)

推荐答案

感谢大家的投入.对于稍后可能会搜索此答案的任何人,事实证明问题出在托管/服务器端.

Thanks to all for your input. For anyone who may be searching for this answer later, it turns out that the problem was hosting/server-side.

在实际切换主机(需要的不仅仅是这个问题)之后,同样的问题跟着我到了新主机.但是在新主机上,他们的服务器设置包括称为清漆缓存"的内容,这与客户端浏览器缓存非常不同.它是服务器端缓存.

After actually switching hosts (needed for more than just this problem) the same problem followed me to the new host. But on the new host their server settings included something called "varnish caching", which is very different from client-side browser caching. It is server-side cache.

据说它可以提高性能……但如果您的应用严重依赖于经常变化的实时 cookie 和会话值,它真的会把事情搞砸!

Supposedly it speeds up performance... but if your app relies heavily on real-time cookie and session values that change often, it REALLY messes things up!

我敢打赌,我在原帖中链接的其他堆栈溢出帖子就是由这个引起的.

I would bet that those other stack overflow posts I linked above in the original post were caused by this.

在新的托管环境中,我刚刚关闭了所有清漆缓存",一切都运行良好.我怀疑旧主机有一些这样的服务器端缓存.

On the new hosting environment I just turned off all "varnish caching" and everything functioned perfectly. I suspect that the old host had some server-side caching like this.

这篇关于首次尝试时无法识别 PHP cookie 和会话值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆