会话变量在多个页面中看不见

时间:2016-12-29 15:59:12

标签: php session

我不理解php中的会话。它表示在你开始一个会话后,会话变量被存储起来并且可以在多个页面中看到。但是在我看不到的页面中。例如,我有我的index.php页面,我在其中启动session_start()。然后我点击一个按钮登录,如果一切正常,它应该重定向到profile.php页面,我打印会话电子邮件。但它不能识别我的会话变量。我的代码:

if($_POST['actiune'] == 'login'){

$email = $_POST['email'];
$_SESSION['username'] = $email;
$password = $_POST['password'];
$pass = getPassword($email);
$verify = password_verify($password, $pass);
    if ($verify) {
        header("Location: index.php?page=profile");
    }
    else {
        header("Location: index.php?page=login&msg=PleaseRegister");
    }
}

profile.php

echo $_SESSION['username'] ; die();

有任何帮助吗?

更新:

profile.php

 <?php
 session_start();
 echo $_SESSION['username'] ; 

 ?>
 <div id="profile">

<p id="welcome">Welcome :<?php echo $_SESSION['username']; ?></p>
<?php
    if ($_SESSION['avatar'] == ""){
        ?>
        <img src = "http://placehold.it/400x200/0000ff/&text=Upload a picture" alt =""/> 
        <?php
    }
    else if ($_SESSION['avatar'] != ""){
        ?>
        <img src="avatars/<?php echo $user['file'];?>">
        <?php   
    }
?>

<p id="modifyPf"><a href="index.php?page=update"> Modify</a></p>
<p id="reset"><a href="index.php?page=resetPassword"> Reset password</a></p>
<p id="articlePf"><a href="index.php?page=allArticles"> Article page</a></p>

<form action="action.scripts.php" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="actiune" value="avatar">
    <input type="hidden" name="id" value="<?php echo $user['id'];?>">
    <p><label for="avatar">Upload an avatar:</label></p>
    <p><input type="file" name="avatar" id="fileToUpload"></p>
    <p><input id ="button" class="btn btn-primary" type="submit" name="button" value="Send"/></p>

</form>

2 个答案:

答案 0 :(得分:0)

每个 页面请求顶部需要session_start() - 每个请求只需一次。它不仅仅是创建会话;您希望使用会话的每个请求都需要它。来自the docs

  

session_start()根据通过GET或POST请求传递的会话标识符创建会话或恢复当前的,或通过cookie传递。

答案 1 :(得分:0)

您还需要在session_start();中致电profile.php

  

session_start()创建会话或恢复当前会话   通过GET或POST请求传递的会话标识符,或通过   cookie中。

更新

<?php
    if(!isset($_SESSION)) 
    { 
       session_start(); 
    } 

    if (isset($_SESSION['username'])){
?>
        <div id="profile">
            <p id="welcome">Welcome :<?php echo $_SESSION['username']; ?></p>
            <?php if ($_SESSION['avatar'] == ""){ ?>
                <img src = "http://placehold.it/400x200/0000ff/&text=Upload a picture" alt =""/> 
            <?php
                }
                else if ($_SESSION['avatar'] != ""){
            ?>
                <img src="avatars/<?php echo $user['file'];?>">
            <?php   
                }
            ?>
            <p id="modifyPf"><a href="index.php?page=update"> Modify</a></p>
            <p id="reset"><a href="index.php?page=resetPassword"> Reset password</a></p>
            <p id="articlePf"><a href="index.php?page=allArticles"> Article page</a></p>

            <form action="action.scripts.php" method="POST" enctype="multipart/form-data">
                <input type="hidden" name="actiune" value="avatar">
                <input type="hidden" name="id" value="<?php echo $user['id'];?>">
                <p><label for="avatar">Upload an avatar:</label></p>
                <p><input type="file" name="avatar" id="fileToUpload"></p>
                <p><input id ="button" class="btn btn-primary" type="submit" name="button" value="Send"/></p>
            </form>
        </div>
<?php
    }
    else {
        echo '<pre>';
        var_dump($_SESSION);
        echo '</pre>';
    }
?>

<强>的index.php

   <?php
        session_start();

        if($_POST['actiune'] == 'login'){
            $email = $_POST['email'];
            $_SESSION['username'] = $email;
            $password = $_POST['password'];
            $pass = getPassword($email);
            $verify = password_verify($password, $pass);
            if ($verify) {
                header("Location: index.php?page=profile");
            }
            else {
                header("Location: index.php?page=login&msg=PleaseRegister");
            }
        }
        else {
            echo 'POST actiune is not login';
        }
    ?>