简体   繁体   English

PHP会话变量未设置

[英]PHP session variable not set

I have cpanel based centos server. 我有基于cpanel的centos服务器。

I am facing issue of session variable not available through out the pages. 我面临无法通过页面访问的会话变量的问题。

I checked all server setting but unable to get idea what i missed out. 我检查了所有服务器设置,但无法知道我错过了什么。

<?php
    session_start();
    // index.php
      echo "session id = " .session_id();
      $_SESSION["username"] = "Niraj";

      echo '<br />Lets see if session available in page 2 ->  <a href="page2.php">page 2</a>';         

      if (!is_writable(session_save_path())) {
          echo '<br><br><br><br>Session path "'.session_save_path().'" is not writable for PHP!'; 
      }
      else
      {
      echo '<br><br><br><br>Session path "'.session_save_path().'" is writable for PHP!'; 
      }

?>

Output of above index.php as under: 以上index.php的输出如下:

session id = 5f59e48f328ef72fda877c8a9f7a07ca
Lets see if session available in page 2 -> page 2

Session path "/var/tmp" is writable for PHP!

If i refresh page, than session id remain same. 如果我刷新页面,则会话ID保持不变。

Code of page2.php as under: page2.php的代码如下:

<?php
session_start();
//page2.php
echo "session id = " .session_id();
echo "<br> Username = " . $_SESSION["username"];
?>

Output of page2.php as under: page2.php的输出如下:

session id =d99088ca0027a483301746e02282662c
Username = 

Problem is Username doesn't output any session value. 问题是用户名不输出任何会话值。 Temporary directory is writable and browser support cookies. 临时目录是可写的,浏览器支持cookie。

I marked that when click on page2.php, it will shows new value in session id, is it okay or session id should remain same across all pages? 我标记为单击page2.php时,它将在会话ID中显示新值,可以吗,或者会话ID在所有页面上都应保持相同?

I tried everything and put lots of effords since last 2 days, same code working fine with other windows server and session id remain same until i close browser. 自从过去两天以来,我尝试了所有事情并投入了大量effords,直到我关闭浏览器之前,相同的代码与其他Windows服务器和会话ID的正常工作都保持不变。

Thanks 谢谢

session_id() must remain the same for you to query data which was set to that session id. session_id()必须保持相同,以便您查询设置为该会话ID的数据。 The session id (dependent on lifetime value) will stay with you until the browser closes. 会话ID(取决于生命周期值)将一直伴随着您,直到浏览器关闭。 I suspect that your browser is blocking session cookies which is causing PHP to regenerate a new ID each time the page loads. 我怀疑您的浏览器阻止了会话cookie,这会导致PHP每次页面加载时重新生成一个新ID。 Download a new browser which you havent used before and test the theory and let me know how you get on. 下载您以前没有使用过的新浏览器,并测试理论,让我知道您的情况。

You can check the global session cookie values and see what the lifetime is set to if you wish but I bet its the browser (0 == Lifetime -> until browser closes). 您可以检查全局会话cookie值,并根据需要查看将生存期设置为什么,但是我敢打赌它是浏览器(0 ==生存期->直到浏览器关闭)。

var_dump(session_get_cookie_params());

http://php.net/manual/en/function.session-get-cookie-params.php http://php.net/manual/zh/function.session-get-cookie-params.php

Also.... 也....

You could just disable any plugins you have especially ones which stop ad's like Adblocker etc... 您可以禁用所有具有特别功能的插件,例如Adblocker等。

Have you seen anything strange occurring to the session files in /var/tmp ? 您是否看到/var/tmp的会话文件发生任何奇怪的情况? Could the server be deleting them? 服务器可以删除它们吗?

MediaTemple Grid servers seem to have issues with sessions when they save to the tmp folder. 将MediaTemple Grid服务器保存到tmp文件夹时,会话似乎出现问题。 I understand you may not be using MediaTemple, but their DV servers run CentOS so it could have something to do with the OS. 我了解您可能没有使用MediaTemple,但是他们的DV服务器运行CentOS,因此可能与OS有关。

https://mediatemple.net/community/products/grid/204643480/why-am-i-experiencing-session-errors https://mediatemple.net/community/products/grid/204643480/why-am-i-experiencing-session-errors

The symptom of interest they list is "General problems with sessions not seeming to be carried across web requests." 他们列出的感兴趣的症状是“会话似乎没有跨Web请求进行的一般性问题”。 Their solution is to get session files out of the tmp folder and store them somewhere else by setting session.save_path in php.ini and restarting apache. 他们的解决方案是将会话文件从tmp文件夹中移出并将它们存储在其他位置,方法是在php.ini中设置session.save_path并重新启动apache。

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

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