简体   繁体   中英

PHP session variable not set

I have cpanel based centos server.

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:

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.

Code of page2.php as under:

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

Output of page2.php as under:

session id =d99088ca0027a483301746e02282662c
Username = 

Problem is Username doesn't output any session value. Temporary directory is writable and browser support cookies.

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?

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.

Thanks

session_id() must remain the same for you to query data which was set to that session id. The session id (dependent on lifetime value) will stay with you until the browser closes. I suspect that your browser is blocking session cookies which is causing PHP to regenerate a new ID each time the page loads. 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).

var_dump(session_get_cookie_params());

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

Also....

You could just disable any plugins you have especially ones which stop ad's like Adblocker etc...

Have you seen anything strange occurring to the session files in /var/tmp ? Could the server be deleting them?

MediaTemple Grid servers seem to have issues with sessions when they save to the tmp folder. I understand you may not be using MediaTemple, but their DV servers run CentOS so it could have something to do with the OS.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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