【问题标题】:SESSION['username'] is not setSESSION['username'] 未设置
【发布时间】:2013-07-06 16:24:59
【问题描述】:

我有一个表单,它接受用户名并在提交后将其放入 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(),否则我的 echo 也不会给我结果。

这是我提交的表格:

<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,而在于重定向不起作用。提交表单后,它应该重定向但现在停留在同一页面上。

【问题讨论】:

  • 你初始化会话了吗?
  • 您需要使用 session_start() 启动每个要使用会话的脚本;
  • 如果我不这样做,那么我的回声不会给我我的结果;)
  • 您必须在每个脚本的开头都有 session_start()。
  • 我不明白的地方:你说你得到“SESSION user is not set10106”,但根据你的代码顺序,它应该是“10106SESSION user is not set”;您说“它执行重定向但无法加载任何内容,因为 SESSION 为空”,但如果您的代码进入 else 路由,则不应执行重定向。您发布的是实际代码吗?

标签: php session redirect header


【解决方案1】:
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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • if(isset($_SESSION['username']) || empty($_SESSION['username'])){ 所以这将始终通过,因为'如果会话用户已设置或未设置.
    • 我的错,我更新了条件来检查 isset 和 !empty
    • 等等……这行得通吗?您可以将纯 HTML 放在带有拆分 php 标签的条件之间吗?每天学些新东西。 :D
    • 是的,这行得通;),虽然我更喜欢只回应 div 等
    【解决方案3】:
    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'] );
    

    【讨论】:

    • 是的,结果是; $_SESSION 用户未设置 10106。 10106 是我想要达到的回声返回的值。
    • 也许在所有这些混乱之后,只需点击 ctrl-u 并复制并粘贴为您提供“$_SESSION 用户未设置 10106”的 HTML 输出,这将是一种不错的形式。这样我们就可以看到 html 内部发生了什么。
    • 好的,我也会发布 HTML。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-02
    • 2019-05-18
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多