开发者

Error when retrieving session_id() after redirecting to another page

Extremely noob question.

I want to develop a login form in PHP using the Post/Redirect/Get pattern. The page containing the login form is something like this:

<?php
  开发者_开发问答if (try_login()) {
    session_regenerate_id();
    session_start();
    header('Location: main.php');
    exit();
  }
?>
<html>
  <head>
    <!-- etc, etc -->
  </head>
  <body>
    <form action="<?php echo htmlentities($_SESSION['PHP_SELF']); ?>" method="post">
      <!-- etc, etc--></head>
    </form>
  </body>
</html>

For debugging purposes, main.php only contains

<?php
  echo htmlentities(session_id());
?>

I expect to be taken to main.php and shown my session_id() after logging in successfully. However, when I test the login form myself, I am redirected to main.php, but my session_id() doesn't appear. What's wrong with my code?


You need to call session_start() in main.php before you can retrieve the session.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜