当我使用正确的用户和密码时,我的php登录系统没有重定向


My php login system is not redirecting when I use the correct user and password

我已经配置了我的数据库,并编写了我的代码,当我测试它时,并使用正确的登录信息,页面不会重定向到我的index.html,而是停留在我的login.php上,并清除我的表单。没有人能弄清楚出了什么问题。感谢所有的帮助!重定向函数在functions.inc.php中编写。

functions.inc.php

    <?php 
//these are the functionss that make my php run
 function redirect($page) { 
 header('Location: ' . $page);
 exit(); 
} 
// This checks if the user is logged in, it will return a boolean value, as to whether they are or not.
function check_login_status() { 
 if (isset($_SESSION['logged_in'])) { 
 return $_SESSION['logged_in']; 
 } 
 return false; 
}
?>

login.inc.php

    <?php 
// Include required MySQL configuration file and functions 
require_once('config.inc.php'); 
require_once('functions.inc.php'); 
// Start session 
session_start(); 
// Check if user is already logged in 
if ($_SESSION['logged_in'] == true) { 
 // If user is already logged in, redirect to main page 
 redirect('index.html'); 
} else { 
 // Make sure that user submitted a username/password and username only consists of alphanumeric chars 
 if ( (!isset($_POST['username'])) || (!isset($_POST['password'])) 
OR 
 (!ctype_alnum($_POST['username'])) ) { 
 redirect('login.php'); 
 } 
 // Connect to database 
 $mysqli = @new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, 
DB_DATABASE); 
 // Check connection 
 if (mysqli_connect_errno()) { 
 printf("Unable to connect to database: %s", 
mysqli_connect_error()); 
 exit();  }
 // Escape any unsafe characters before querying database 
 $username = $mysqli->real_escape_string($_POST['username']); 
 $password = $mysqli->real_escape_string($_POST['password']); 
 // Construct SQL statement for query & execute 
 $sql = "SELECT * FROM users WHERE username = '" . 
$username . "' AND password = '" . md5($password) . "'"; 
 $result = $mysqli->query($sql); 
 // If one row is returned, username and password are valid 
 if (is_object($result) && $result->num_rows == 1) { 
 // Set session variable for login status to true 
 $_SESSION['logged_in'] = true; 
 redirect('index.html'); 
 } else { 
 // If number of rows returned is not one, redirect back to login screen 
 redirect('login.php'); 
 } 
} 
?> 

login.php(这是用户看到的页面)

    <!Doctype html>
<html>
  <head>
 <legend>Login to Web Site</legend>
</center>
 <fieldset>
 <table>
<tr>
 <td> 
 <center>
 <label for="username">
 <input type="text" name="username" id="username" />  </label>
  </center>
 </td>
 </tr>
 <tr>
 <td>
  <center>
 <label for="password">
 <input type="password" name="password" id="password"/> 
 </label>
 </center>
</td>
</tr> 
 </table>
 <label for="submit"> 
 <input type="submit" name="submit" id="submit" value="Login"/> 
 </label> 
 </fieldset> 
 </form>
 </table>

    <script src="../jquery.js"></script>
    <script src="../bootstrap.js"></script>
</body>
</html>

你需要改变这个

function check_login_status() { 
 if (isset($_SESSION['logged_in'])) { 
 return $_SESSION['logged_in']; 
 } 
 return false; 
}

function check_login_status() { 
  return (isset($_SESSION['logged_in']) || $_SESSION['logged_in'] === true);
}

你的情况:

// Check if user is already logged in 
if ($_SESSION['logged_in'] == true) { 
 // If user is already logged in, redirect to main page 
 redirect('index.html'); 
}

:

// Check if user is already logged in 
if (check_login_status()) { 
 // If user is already logged in, redirect to main page 
 redirect('index.html'); 
}

调用redirect()函数后,发出exit, returndie,以便代码不再继续处理。

如果它继续处理和echo的任何文本,头将已经发送,重定向将失败。在这种情况下,即使您没有对任何文本进行echo处理,在重定向之后进行exit处理也是一个好习惯,因为它将在稍后的其他代码中对您产生不利影响。

例如:

redirect('index.html'); 
exit;

基于你的错误,你应该使用check_login_status()函数。变化:

if ($_SESSION['logged_in'] == true) { 
 // If user is already logged in, redirect to main page 
 redirect('index.html'); 

if (check_login_status()) { 
 // If user is already logged in, redirect to main page 
 redirect('index.html'); 
 exit;

这是假定check_login_status()在登录时返回true。