Session variables not working php

后端 未结 10 1329
别跟我提以往
别跟我提以往 2020-11-22 03:12

Here are the code of my login page where the login script checks for the authenticity of the user and then redirects to inbox page using header function.

<         


        
相关标签:
10条回答
  • 2020-11-22 03:48

    The other important reason sessions can not work is playing with the session cookie settings, eg. setting session cookie lifetime to 0 or other low values because of simple mistake or by other developer for a reason.

    session_set_cookie_params(0)
    
    0 讨论(0)
  • 2020-11-22 03:49

    If you use a connection script, dont forget to use session_start(); at the connection too, had some trouble before noticing that issue.

    0 讨论(0)
  • 2020-11-22 03:50

    Maybe it helps others, myself I had

    session_regenerate_id(false);
    

    I removed it and all ok!

    after login was ok... ouch!

    0 讨论(0)
  • 2020-11-22 03:53

    Just talked to the hosting service, it was an issue at their end. he said " your account session.save_path was not set as a result issue arise. I set it for you now."

    And it works fine after that :)

    0 讨论(0)
  • 2020-11-22 03:58
    1. Make sure session_start(); is called before any sessions are being called. So a safe bet would be to put it at the beginning of your page, immediately after the opening <?php tag before anything else. Also ensure there are no whitespaces/tabs before the opening <?php tag.
    2. After the header redirect, end the current script using exit(); (Others have also suggested session_write_close(); and session_regenerate_id(true), you can try those as well, but I'd use exit();).
    3. Make sure cookies are enabled in the browser you are using to test it on.
    4. Ensure register_globals is off, you can check this on the php.ini file and also using phpinfo(). Refer to this as to how to turn it off.
    5. Make sure you didn't delete or empty the session.
    6. Make sure the key in your $_SESSION superglobal array is not overwritten anywhere.
    7. Make sure you redirect to the same domain. So redirecting from a www.yourdomain.com to yourdomain.com doesn't carry the session forward.
    8. Make sure your file extension is .php (it happens!).

    PHP session lost after redirect

    0 讨论(0)
  • 2020-11-22 04:01

    I had the same issue for a while and had a very hard time figuring it out. My problem was that I had the site working for a while with the sessions working right, and then all of the sudden everything broke.

    Apparently, your session_save_path(), for me it was /var/lib/php5/, needs to have correct permissions (the user running php, eg www-data needs write access to the directory). I accidentally changed it, breaking sessions completely.

    Run sudo chmod -R 700 /var/lib/php5/ and then sudo chown -R www-data /var/lib/php5/ so that the php user has access to the folder.

    0 讨论(0)
提交回复
热议问题