dougou8639 2015-09-05 02:14
浏览 97

第一次尝试时无法识别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>

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

After initial page load:

POST superglobal:
Array
(
)

SESSION superglobal:
Array
(
)

COOKIE superglobal:
Array
(
)

[Submit]

After clicking Submit one time:

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

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

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

[Submit]

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.

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

After initial page load:

POST superglobal:
Array
(
)

SESSION superglobal:
Array
(
)

COOKIE superglobal:
Array
(
)

[Submit]

After clicking Submit one time:

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

SESSION superglobal:
Array
(
)

COOKIE superglobal:
Array
(
)

[Submit]

After clicking Submit a SECOND time:

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]

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.

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).

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.

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

PHP cookie will not set until the page reloads TWICE. What's going on?

php session variable not stored at first attempt, working fine from second attempt

session variables not set the first time

(none of the solutions there worked for me)

  • 写回答

3条回答 默认 最新

  • dosc9472 2015-09-05 02:46
    关注

    I've run into this issue a few times while developing, although I have never used the Arvixe. I am by no means an expert, but this is the research and experience I've had and the two main reasons why beyond coding errors.

    Cookies not created immediately on request

    Newly created cookies are not immediately available when the page is refreshed. I've had this unexpected behavior before with SESSION_ID and cookies. If for example, you set the cookie in one request, it won't be available until the next request for the server (so the next refresh after you've set the cookie). Accessing $_COOKIE immediately after setcookie()

    There can also be weird logic and the way a browser caches web pages, I think that is likely what is going on here.

    PHP Session Not Setting - via. www or non-www problem

    You said that it occurs on your Arvixe environment and not localhost? You should check if the domain (getting www removed or added) is changing when your click submit. If you do not have correct rules in the htaccess or set via ini_set or other configurations settings in PHP, then you could be getting two session_ids. I can't quite clearly tell from your code, but if that's the case, it would explain why you only get it the second time (since the first submit it loads new domain and set the session variables and cookie variables). Your server may treat www as a new sub-domain and not choose to share the cookie and session_id, but rather create a new one. Try going into Console and executing document.cookie to get information on whether or not it is changing.

    Allow php sessions to carry over to subdomains

    Extraneous session commands or other reasons

    If you have any PHP commands on the page that fiddle with the session_id such as regenerating it or for example:

    session_regenerate_id()
    session_write_close()

    These commands could possibly be affecting your session and causing unexpected behavior. Since it is case-specific, it'd be in your best interest to read further in the documentation on how sessions propagate and are handled. Other than that, it could be cache, browser, headers, placement of session, stale-sessions, previous cookies, and other related issues.

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探