PHP session variables not carrying over to my logged in page, but session ID is

后端 未结 4 1796
太阳男子
太阳男子 2020-12-17 03:15

I\'m using PHP 4.3.9, Apache/2.0.52

I\'m trying to get a login system working that registers DB values in a session where they\'re available once lo

相关标签:
4条回答
  • 2020-12-17 03:43

    ...may I add to the other answers, that session_start() sometimes fails or weird stuff occurs if not placed at the very first beginning of the script. In your header script, try:

    Instead of

    <?php
    ob_start();
    session_start();
    

    Put

    <?php
    session_start();
    ob_start();
    
    0 讨论(0)
  • 2020-12-17 03:47

    Try doing a

    session_regenerate_id(true); 
    

    before the

    session_write_close();
    

    Also. The best way IMO to do a login script is this:

    Let the login logic be handled within the mainpage the user is trying to access.

    1. If the user is not authenticated, he is thrown back to the login page
    2. If the user is authenticated, he gets an $_SESSION["auth"] or something
    3. Then when the user is browsing the main page or other pages that need auth, they just check if the $_SESSION["auth"] is set.

    Then you wont have the trouble of session not saving just before a redirect

    0 讨论(0)
  • 2020-12-17 03:52

    I don't see a session_start() in your login script. If you aren't starting the session I don't think php will save any data you place in the $_SESSION array. Also to be safe I'd explicitly place variables into the $_SESSION array instead of just overwriting the whole thing with $_SESSION = mysql_fetch_array($result);.

    0 讨论(0)
  • 2020-12-17 03:52

    I was having a similar problem when I discovered this:

    http://www.w3schools.com/php/php_sessions.asp

    You HAVE TO put the session_start(); before ANY html tags

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