未设置SESSION ['username']

马塞诺

我有一个使用用户名并将其提交后提交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

编辑:好的,所以问题不在于未设置的会话,而是无法正常工作的重定向。提交表单后,它应该重定向,但现在保持在同一页面上。

韦拉斯蒂格(Jm Verastigue)

我认为您需要将代码构造为以下形式:

<?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
}
?>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章