2

我有一个表单,它接受用户名并在提交后将其放入 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>

额外信息:

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

index.php?page=new_call&user=10106

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

4

3 回答 3

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

于 2013-07-09T07:09:51.813 回答
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'] );
于 2013-07-09T07:22:22.260 回答
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
}
?>
于 2013-07-09T07:39:32.390 回答