SESSION ['用户名']未设置

时间:2013-07-09 06:54:58

标签: php session redirect header

我有一个表单,它接受用户名并在提交后将其放入SESSION。 现在,当我检查SESSION用户名是否存在并回显它时,它会完美地显示结果,但是当我想重定向到另一个页面时,如果相同的SESSION不为空,它会告诉我SESSION不存在。

我不明白?!

if(isset($_POST['submit'])){
    $user = $_POST['username']; 
    $_SESSION['username'] = $user;

    echo $_SESSION['username']; // This shows me the result perfectly

    if(isset($_SESSION['username'])){
    header('location:index.php?page=dashboard&user='.$_SESSION['username'].'');
    }
    else{
    echo "SESSION user isn't set."; // This is the result I get from the isset()
    }
}

值10106是我想要在这部分得到的值,我的页面打印出来:

SESSION user isn't set10106

它执行重定向,但无法加载任何内容,因为SESSION为空,但是当我点击我的按钮注销时,它确实显示了URL中的值:

action=logout&user=10106

这意味着会话已设置。但isset()仍然给我一个错误的结果。 我没有忘记session_start(),否则我的回声也不会给我结果。

这是我提交的表格:

<form action='index.php?page=login' method='post'>
    <table id='login'>
       <tr>
          <td colspan=2><br></td>
       </tr>
       <tr>
          <td>User:</td>
          <td><input type='username' name='username'></td>
       </tr>
       <tr>
          <td>Password:</td>
          <td><input type='password' name='password'></td>
       </tr>
       <tr>
          <td colspan=2><input type='submit' name='submit'></td>
       </tr>
    </table>
</form>

额外信息:

当我点击提交时,它会停留在同一页面上,网址没有改变,但我的菜单却没有。设置SESSION用户名时菜单会更改,因此当我单击菜单中的另一个URL时,它会向我显示URL中的SESSION。

index.php?page=new_call&user=10106

编辑:好的,所以问题不在于没有设置的SESSION,它是不起作用的重定向。提交表单后,它应该重定向,但现在保持在同一页面上。

3 个答案:

答案 0 :(得分:2)

session_start();
$user = "admin";

if(!isset($_SESSION['user_name'])){
    if($_POST){
        if($_POST['username'] == $user){
            $_SESSION['user_name'] = $user;
        } else {
            echo "SESSION user isn't set.<br>";
        }

    }
    echo "input your session username ";
} else {
    echo  "Youve got Session " . $_SESSION['user_name'];
}

试一试:D

答案 1 :(得分:1)

if(isset($_POST['submit']))
    {// I assume this tests to see if it was passed by a form
    $_SESSION['username'] = $_POST['username']; 
    //Sets $_SESSION to the post username in 1 step.

    echo $_SESSION['username']; // Prints the contents of $_SESSION['username']

    if(isset($_SESSION['username']))
        {//if the above echoes, then this must be true...
        header("Location: index.php?page=dashboard");
        }
    else
        {
        echo '$_SESSION["user"] isn't set.'; // This is the result I get from the isset()
        }
    }

如果您真的开始将$ _SESSION ['username']值设置在URL中,那么请执行此操作。

header("Location: index.php?page=dashboard&user=" . $_SESSION['username'] );

答案 2 :(得分:1)

我认为您需要将代码结构化为:

<?php
session_start();
if(isset($_GET['page'])){
    $page=$_GET['page'];
}
else{
    $page="";
}

if(isset($_POST['submit'])){ //log in submit
    $user = $_POST['username']; 
    $_SESSION['username'] = $user;

    echo $_SESSION['username']; // This shows me the result perfectly

    if(isset($_SESSION['username']) && !empty($_SESSION['username'])){
    header('location:index.php?page=dashboard&user='.$_SESSION['username'].'');
    }
    else{
    echo "SESSION user isn't set."; // This is the result I get from the isset()
    }
}
if($page=="dashboard"){ //dashboard page

?>
Dashboard page<br>
Your session: <?php echo($_SESSION['username']); ?>
<?php
}
else{ //login page
?>
<form action='' method='post'>
    <table id='login'>
       <tr>
          <td colspan=2><br></td>
       </tr>
       <tr>
          <td>User:</td>
          <td><input type='username' name='username'></td>
       </tr>
       <tr>
          <td>Password:</td>
          <td><input type='password' name='password'></td>
       </tr>
       <tr>
          <td colspan=2><input type='submit' name='submit'></td>
       </tr>
    </table>
</form>
<?php
}
?>