2017-07-19 55 views
0

我只是想做一个简单的登录网站。我存储在会话变量电子邮件ID:无法在不同的页面访问会话变量

$_SESSION['user']=$email; 

但是,当我试图在另一个页面访问此会话变量,它显示了一个错误:

Undefined index: user in C:\xampp\htdocs\web\home.php on line 11

的login.php

<?php 
     if($_SERVER["REQUEST_METHOD"]=="POST") 
     { 
      $email=$_POST["email"]; 
      $password=$_POST["password"]; 

      $bool=true; 
      include 'dbs_con.php'; 
      $query=mysqli_query($conn,"SELECT * FROM user_info WHERE email='$email'"); 
      if($query === FALSE) { 
     die(mysqli_error($conn)); // TODO: better error handling 
    } 
      $exists = mysqli_num_rows($query); 
      $table_users = ""; 
      $table_password = ""; 
      if($exists > 0) //IF there are no returning rows or no existing username 
      { 
       while($row = mysqli_fetch_assoc($query)) 
       { 
        $table_users = $row['email']; // the first username row is passed on to $table_users, and so on until the query is finished 
        $table_pass = $row['password']; 
        //echo "current db value".$table_users." & ".$table_pass; 

       } 
       // echo "USER INPUT".$email." & ".$password; 
       // echo "current db value".$table_users." & ".$table_pass; 
        if(($email == $table_users) && ($password==$table_pass)) // checks if there are any matching fields 
        { 
         echo "yes"; 
         if($password==$table_pass) 
         { 
          $_SESSION['user'] = $email; //set the username in a session. This serves as a global variable 
          echo $_SESSION['user']; 
         header("location: home.php"); // redirects the user to the authenticated home page 

         } 

         else 
         { 
         Print '<script>alert("Incorrect Password!");</script>'; // Prompts the user 
         Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php 
         } 
       } 
       else 
       { 
        Print '<script>alert("Incorrect username!");</script>'; // Prompts the user 
        Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php 
       } 
      } 
     } 
    ?> 

home.php

<?php 
session_start(); 
?> 
<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
</head> 
<body> 
<header> 
    <?php print $_SESSION['user']; ?> 
</header> 
</body> 
</html> 

回答

1

你有session_start();在login.php页面上?