drn5375
drn5375
2015-07-14 04:08

标题重定向后,PHP会话索引是否未定义?

I have struggled with this for hours but I can't get it to work. When I do a redirection to another PHP page, all my session variables are null. I am on xampp server.

session.php

<?php
     session_start();
     if(isset($_POST['submitted']))                                                                                         
     {   
        $_SESSION['first_name'] = "MAX";
        var_dump($_SESSION);
        header("Location: http://localhost:8080/secure login/session2.php");     
        die();
     }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
<head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-    1" /> 
    <title>You Logged In</title> 
</head> 
<body> 
    <form action="session.php" method="post">
        <div align="center"><input type="submit" name="submit" value="Login" /></div>
    <input type="hidden" name="submitted" value="TRUE" />
    </form>
</body>
</html>

session2.php

<?php
    session_start();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
<head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
    <title>You Logged In</title> 
</head> 
<body> 
    <div id="main"> 
        <?php 
            echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';
            echo 'You are welcome to session2.php <br></br>'; 
            if (isset($_SESSION['first_name'])) 
            { 
                echo $_SESSION['first_name'] . "<br></br>";
            }
            else
            {
                echo "Your session doesn't exist. I hate php <br></br>";
                echo $_SESSION['first_name'];
            }
        ?>
    </div>
 </body>
 </html>

The session doesn't save, and the output is;

Array
(
)
You are welcome to session2.php
Your session doesn't exist. I hate php
Notice: Undefined index: first_name in C:\xampp\htdocs\secure login\session2.php on line 28

I have tried other things like changing where session variables are saved from xampp/tmp to another directory, but this didn't solve the problem. I have a program that I need to keep a user logged in when I do a redirection but this has blocked me for more than a day.

UPDATE:

The space between the directories wasn't the problem, it temporarily solved the problem but that was because there wasn't cache for the new directory yet. Any way, for a few more days, I debugged and realized that I was running two programs on my localhost. Both were using sessions, and so if one terminates the session, it also terminates the session for the other since localhost is like a domain name and there exists only one session. Particularly, the logout.php of my other program was not destroying the session but was rather jumbling it up were by you have to remove browser cache do unjumble it. I was emptying session array, destroying the session, and destroying the cookie, this was the problem and so I couldn't login again. All I had to do was just destroy the session only;

See -> Killing off Global Session Variable as a logout button

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • doushun1904 doushun1904 6年前

    Seems like you are having problem because you have a space in name secure login

    localhost:8080/secure%20login/session.php 
    

    So please try to change the name with underscore secure_login and also change your code

    <?php
         session_start();
         if(isset($_POST['submitted']))                                                                                         
         {   
            $_SESSION['first_name'] = "MAX";
            var_dump($_SESSION);
            header("Location: http://localhost:8080/secure_login/session2.php");     
            die();
         }
    ?>
    
    点赞 评论 复制链接分享
  • doushi1510 doushi1510 6年前

    Firstly, check if there any white spaces before starting the <?php tag in session.php and session2.php file.

    Than remove var_dump($_SESSION); from session.php.

    and change the folder name to secure_login

    点赞 评论 复制链接分享
  • douye2036 douye2036 6年前

    Change this

    <?php
        session_start();
        if(isset($_POST['submitted']))
        {
            $user = $_SESSION['first_name'] = "MAX";
    
            if(isset($user))
            {
                header("Location: http://localhost:8080/secure login/session2.php");
            }
            else
            {
                header("Location: ");//index page
            }
        }
    ?>
    

    In $user = $_SESSION['first_name'] = "MAX";

    first $_SESSION['first_name'] = "MAX"; will execute, then result will be assign to $user. so in isset($user) it Check whether set

    点赞 评论 复制链接分享
  • doushou8299 doushou8299 6年前

    Most likely due to the die() call, I think that causes it not to write the session.

    Try session_write_close(); prior to it.

    点赞 评论 复制链接分享

为你推荐