注册表单中的会话变量没有传递给登录表单


Session Variables from Registration Form not being passed to Login Form

我一直在尝试解决一个问题,但到目前为止我一直没有运气。

我有一个与用户的姓和名相呼应的个人资料页面。此函数在用户首次注册时起作用。然而,问题是,当用户注销(结束会话)并重新登录并返回他/她的个人资料页面时,名字和姓氏不显示,而是留下空白。

为了更好地阐明这些途径:

1。用户注册->配置文件显示姓和名2.用户登录->配置文件不显示姓和名

这里是有关这个问题的代码(我已经有session_start()在每个页面的顶部我有;此外,我的变量,reg表单和登录表单都在一个PHP外壳下):

变量:

    <?php
    error_reporting(E_ALL ^ E_NOTICE);
     $reg = $_POST['reg'];
     //initializing registration variables to prevent errors
     $fn = ""; //first name
     $ln = ""; //last name
     $em = ""; //email
     $em2 = ""; //email 2
     $pass = ""; //password
     $bday = ""; //birthday
     $sud = ""; //sign up date
     $em_check =""; //check if email exists
     //registration variables + form
     $fn = mysqli_real_escape_string($con, $_POST['first_name']);
     $ln = mysqli_real_escape_string($con, $_POST['last_name']);
     $em = mysqli_real_escape_string($con, $_POST['email']);
     $em2 = mysqli_real_escape_string($con, $_POST['email2']);
     $pass = mysqli_real_escape_string($con, $_POST['password']);
     $bday = date("Y");
     $sud = date("Y-m-d H:i:s"); // Year - Month - Day

登记表:

    if(isset($_POST['reg'])) {
if($em==$em2) {
//check if email already exists
  $emSQLI = "SELECT email FROM `users` WHERE email='$em'";
  $em_check = mysqli_query($con, $emSQLI); //checks whether both entered emails are identical
  $check = mysqli_num_rows($em_check); //count the amount of rows where email = $em
  if ($check == 0) {
    //check if all fields have been filled
    if($fn && $ln && $em && $em2 && $pass && $bday) {
      //check the maximum length of relevant fields
      if(strlen($fn)>90||strlen($ln)>90) {
        echo "Maximum limit for first/last names is 90 characters.";
      }
      else{
        if (strlen($pass)<6||strlen($pass)>99) {
          echo "Password must be between 6 and 99 characters long.";
        }
        else {
          $pass = md5($pass);
          $regSQLI = "INSERT INTO users (id, email, birth_date, first_name, last_name, password, sign_up_date, activated) VALUES ('','$em','$bday','$fn','$ln','$pass','$sud','0')";
          $regQuery = mysqli_query($con, $regSQLI);
       //variables that will be passed over from the register fields to forthcoming sessions
      $_SESSION["email_login"] = $em;
      $_SESSION["first_name"] = $fn;
      $_SESSION["last_name"] = $ln;
      }
      }
      header("location: profile.php");
      exit();
      }
      else {
        echo '<div id="regerrormsg">Please fill in all required fields. </div>';
      }
    }
    else {
      echo '<div id="regerrormsg"> Email is already registered. </div>';
    }
  }
  else {
    echo '<div id="regerrormsg">Entered emails do not match. </div>';
  }
}

登录表单:

    if(isset($_POST['email_login']) && isset($_POST['pass_login'])) {
  $email_login = mysqli_real_escape_string($con, $_POST['email_login']);
  $pass_login = mysqli_real_escape_string($con, $_POST['pass_login']);
  $pass_login = md5($pass_login);
  $logquery = "SELECT id FROM users WHERE email='$email_login' AND password='$pass_login' LIMIT 1";
  $sqli = mysqli_query($con, $logquery);
  $userCount = mysqli_num_rows($sqli); // Count number of rows returned
  // checks the database for respective items
  if ($userCount == 1) { //if the search finds a matching record of the login input form
      while( $row = mysqli_fetch_assoc($sqli)) { // use fetch_assoc
          $id = $row["id"];
          }
      $_SESSION["email_login"] = $email_login;
      $_SESSION["first_name"] = $fn;
      $_SESSION["last_name"] = $ln;

      header("location: home.php");
      exit();
  }
  else {
    echo '<div id="regerrormsg">Login information is invalid. </div>';
  }
    }

最后,显示名字的概要页面:

    <?php
    session_start();
    include ( "./inc/connect.inc.php");
    if(!isset($_SESSION["email_login"])) {
      header("location: index.php");
     exit();
    }
    else {
    }
    ?>
    <?php
    echo "Delighted to have you here, " .$_SESSION["first_name"]."&nbsp".$_SESSION["last_name"].".";
     ?>

我卡住了,希望能帮助解决这个问题,谢谢!

编辑:这里是html代码:

登录表单:

    <form action="index.php" method="POST">
      <input type="email" name="email_login" size="60" placeholder="Email" /><br /><br /><br />
      <input type="password" name="pass_login" size="60" placeholder="Password" /><br /><br /><br />
      <input type="submit" name="login" id="login" value="LOG IN">
    </form>

注册表单:

    <form action="index.php" method="POST">
          <input type="text" name="first_name" size="15" placeholder="First name" /><br /><br /><br />
          <input type="text" name="last_name" size="15" placeholder="Last name" /><br /><br /><br />
          <input type="email" name="email" size="15" placeholder="Email" /><br /><br /><br />
          <input type="email" name="email2" size="25" placeholder="Re-enter email" /><br /><br /><br />
          <input type="password" name="password" size="15" placeholder="New password" /><br /><br /><br />
          <p5>Birthyear</p5><br />
          <div id="date1" class="datefield">
            <input id="birth_year" type="tel" name="birth_year" maxlength="4" placeholder="YYYY" />
          </div>
          <input type="submit" name="reg" value="Sign Up"><br />
        </form>

在您的Log form中,替换此

  $logquery = "SELECT id FROM users WHERE email='$email_login' AND password='$pass_login' LIMIT 1";
  $sqli = mysqli_query($con, $logquery);
  $userCount = mysqli_num_rows($sqli); // Count number of rows returned
  // checks the database for respective items
  if ($userCount == 1) { //if the search finds a matching record of the login input form
      while( $row = mysqli_fetch_assoc($sqli)) { // use fetch_assoc
          $id = $row["id"];
          }
      $_SESSION["email_login"] = $email_login;
      $_SESSION["first_name"] = $fn; // -> $fn not defined
      $_SESSION["last_name"] = $ln; // -> $ln not defined

      header("location: home.php");
      exit();
  }
  else {
    echo '<div id="regerrormsg">Login information is invalid. </div>';
  }

,

  $logquery = "SELECT * FROM users WHERE email='$email_login' AND password='$pass_login' LIMIT 1";
  $sqli = mysqli_query($con, $logquery);
  $userCount = mysqli_num_rows($sqli); // Count number of rows returned
  // checks the database for respective items
  if ($userCount == 1) { //if the search finds a matching record of the login input form
      while( $row = mysqli_fetch_assoc($sqli)) { // use fetch_assoc
          $id = $row["id"];
          $_SESSION["email_login"] = $row["email"];
          $_SESSION["first_name"] = $row["first_name"]; // -> retrieve the data from the result of the query
          $_SESSION["last_name"] = $row["last_name"];
      }
      // EDIT: i moved the $_SESSION part above, my bad !

      header("location: home.php");
      exit();
  }
  else {
    echo '<div id="regerrormsg">Login information is invalid. </div>';
  }