PHP Session data not being saved

2019-01-02 18:09发布

I have one of those "I swear I didn't touch the server" situations. I honestly didn't touch any of the php scripts. The problem I am having is that php data is not being saved across different pages or page refreshes. I know a new session is being created correctly because I can set a session variable (e.g. $_SESSION['foo'] = "foo" and print it back out on the same page just fine. But when I try to use that same variable on another page it is not set! Is there any php functions or information I can use on my hosts server to see what is going on?

Here is an example script that does not work on my hosts' server as of right now:

<?php
session_start();
if(isset($_SESSION['views']))
    $_SESSION['views'] = $_SESSION['views']+ 1;
else
    $_SESSION['views'] = 1;

echo "views = ". $_SESSION['views'];
echo '<p><a href="page1.php">Refresh</a></p>';
?>

The 'views' variable never gets incremented after doing a page refresh. I'm thinking this is a problem on their side, but I wanted to make sure I'm not a complete idiot first.

Here is the phpinfo() for my hosts' server (PHP Version 4.4.7): alt text

标签: php session
22条回答
无与为乐者.
2楼-- · 2019-01-02 18:55

Check if you are using session_write_close(); anywhere, I was using this right after another session and then trying to write to the session again and it wasn't working.. so just comment that sh*t out

查看更多
伤终究还是伤i
3楼-- · 2019-01-02 18:56

Check to make sure you are not mixing https:// with http://. Session variables do not flow between secure and insecure sessions.

查看更多
人气声优
4楼-- · 2019-01-02 18:56

Well, we can eliminate code error because I tested the code on my own server (PHP 5).

Here's what to check for:

  1. Are you calling session_unset() or session_destroy() anywhere? These functions will delete the session data immediately. If I put these at the end of my script, it begins behaving exactly like you describe.

  2. Does it act the same in all browsers? If it works on one browser and not another, you may have a configuration problem on the nonfunctioning browser (i.e. you turned off cookies and forgot to turn them on, or are blocking cookies by mistake).

  3. Is the session folder writable? You can't test this with is_writable(), so you'll need to go to the folder (from phpinfo() it looks like /var/php_sessions) and make sure sessions are actually getting created.

查看更多
冷夜・残月
5楼-- · 2019-01-02 18:58

Had same problem - what happened to me is our server admin changed the session.cookie_secure boolean to On, which means that cookies will only be sent over a secure connection. Since the cookie was not being found, php was creating a new session every time, thus session variables were not being seen.

查看更多
登录 后发表回答