运行 SQL 同时创建 session 并通过 php 重定向

Running SQL whilst creating session and redirecting via php

抱歉,这个标题毫无用处。我自己也不确定如何简洁地描述它。

查询

<?php 
// Parse the log in form if the user has filled it out and pressed "Log In"
if (isset($_POST["username"]) && isset($_POST["password"])) {

    $username = $_POST["username"]; // filter everything but numbers and letters
    $password = $_POST["password"]; // filter everything but numbers and letters
    // Connect to the MySQL database  
    include "dbconnect.php";
    $sql = mysql_query("SELECT username, admin FROM logins WHERE username='$username' AND password='$password' LIMIT 1"); // query the person
    // ------- MAKE SURE PERSON EXISTS IN DATABASE ---------
    $existCount = mysql_num_rows($sql); // count the row nums
    if ($existCount == 1) { // evaluate the count
        while($row = mysql_fetch_array($sql)){ 
            $username = $row["username"];
            $admin = $row["admin"];

        }
        if ($admin ==2) {
            $coach = $_POST["username"]; // filter everything but numbers and letters
            $password = $_POST["password"]; // filter everything but numbers and letters
            $sql = mysql_query("SELECT id, activity FROM coaches WHERE username='$coach' AND password='$password' LIMIT 1");
            while($row = mysql_fetch_array($sql)){ 
                $id = $row["id"];
                $activity = $row["activity"];
            }
            $_SESSION["id"] = $id;
            $_SESSION["coach"] = $coach;
            $_SESSION["password"] = $password;
            $_SESSION["activity"] = $activity;
            header("location: coach-home.php");
            exit();
        } 
        else 
            if ($admin ==1) 
                { header("location: player-login.php"); }
        exit();
    } else {
        echo 'That information is incorrect, try again <a href="testlog.php">Click Here $username</a>';
        exit();
    }
}
?>

为了帮助节省时间,一切正常,直到我开始在 (admin ==2) {} 位中输入查询。我试图让它获得已经发送的登录详细信息,然后,一旦注意到他们的管理员级别(在这种情况下,2 = 他们是管理员),它将 运行 另一个查询来创建 session 对于管理员,并将他们带到教练主页。

然而似乎正在发生的事情是 session 没有被创建(duh),因为 coach-home 没有看到有效的 SESSION,因此,重定向我。

我想知道的是 a. 如果我尝试做的事情是可能的,并且失败了 b. 我开始研究 redirect-redirect... 所以在 (admin ==2) 之后它会将此人引导至 coach-login,其中与上面的查询基本相同,然后用于重定向到 coach-home。但是,页面没有这样做,而是显示在 coach-login 并且是空白的(下面的 coach-login.php 代码)

页面底部的表格(如果这是相关的?)

<form id="form1" name="form1" method="post" action="testlog.php">
    User Name:<br />
      <input name="username" type="text" id="username" size="40" />
    <br /><br />
    Password:<br />
   <input name="password" type="password" id="password" size="40" />
   <br />
   <br />
   <br />

     <input type="submit" name="button" id="button" value="Log In" />

  </form>

如有任何帮助,我们将不胜感激。

coach-login.php

<?php 
// Parse the log in form if the user has filled it out and pressed "Log In"
if (isset($_POST["username"]) && isset($_POST["password"])) {

    $coach = $_POST["username"]; // filter everything but numbers and letters
    $password = $_POST["password"]; // filter everything but numbers and letters
    // Connect to the MySQL database  
    include "dbconnect.php";
    $sql = mysql_query("SELECT id, activity FROM coaches WHERE username='$coach' AND password='$password' LIMIT 1"); // query the person
    // ------- MAKE SURE PERSON EXISTS IN DATABASE ---------
    $existCount = mysql_num_rows($sql); // count the row nums
    if ($existCount == 1) { // evaluate the count
         while($row = mysql_fetch_array($sql)){ 
             $id = $row["id"];
             $activity = $row["activity"];

         }
         $_SESSION["id"] = $id;
         $_SESSION["coach"] = $coach;
         $_SESSION["password"] = $password;
         $_SESSION["activity"] = $activity;
         header("location: coach-home.php");
         exit();
    } else {
        echo 'That information is incorrect, try again <a href="index.php">Click Here</a>';
        exit();
    }
}
?>

"yes I have session_start(); in most files, except for coach-login.php"


按照OP的意愿:

session_start(); 也必须在 "coach-login.php" 中,因为那里有会话变量。

正如我之前所说,session_start(); 必须在所有使用会话的文件中。

  • 这就是会话未结转的原因。