PHP session variables not writing to files

僤鯓⒐⒋嵵緔 提交于 2021-02-11 14:25:05

问题


My PHP sessions are being created but $_SESSION variables are not being written to the session file.

I have three test PHP pages here that I'm using to diagnose this problem:

test_a.html:

<html>
<body>
    <form action='test_b.php' method='post'>
        Put something here: 
        <input type='text' name='saveme' /> 
        <input type='submit' value='Go!' />
    </form>
</body>
</html>

test_b.php:

<?php
    session_start();
    $_SESSION['saveme']=$_POST['saveme'];
    echo "Here's what you wrote:<br>".$_SESSION['saveme']."<br>".
        "<a href='test_c.php'>Take me to the final check</a><br>";
    echo "And here's your session_id: ".session_id();
    session_write_close();
?>

test_c.php:

<?php
    session_start();
    echo "Here's what you wrote (maybe?):<br>".$_SESSION['saveme']."<br>".
        "You should also see saveme below:<br>";
    foreach ($_SESSION as $key=>$val)
        echo $key." ".$val."<br>";
    echo "And here's your session_id: ".session_id();
?>

When opening test_a.html and typing in anything to the textbox and hitting Go!, it will show up correctly on test_b.php (when it is set and recalled from memory) but it is not shown on test_c.php.

The session_id is set and shows to be the same. The cookie is stored correctly. The session file is created correctly in the filesystem, but is not written to and remains a zero-byte file.

Sessions are now not being written on any pages of my site (despite working for well over a year up to this point) so typos in these code snippets (if any) are likely irrelevant.

Things I've checked:

  • The session storage directory is writeable and the disk has plenty of storage
  • session_start is always successful (and there is nothing before it)
  • The session_id is created correctly and is the same on both pages
  • The session cookie is created with the correct session_id
  • session.use_cookies and session.use_only_cookies are on
  • Register globals is off
  • Cookies are enabled
  • Cleared the /tmp directory to reduce possibility of filesystem issues
  • Tried storing the session data in memory (didn't work)
  • Tried changing the session file location (didn't work)
  • Tested on Chrome & Firefox

Ideas?


回答1:


Your session storage directory may be writable but the file is not. Try setting the rights to 0666 on the session file and check if this fixes the problem. It's something like one process is creating the file and the very next is not able to write it. What's the owner of the file at creation time? Check if it's www-data or whatever runs your php.




回答2:


PHP session keys are separated by the pipe character | so | in key name will break you session. Maybe this is the problem.

Check this:

enter link description here



来源:https://stackoverflow.com/questions/20891879/php-session-variables-not-writing-to-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!