我已经配置了数据库,并在测试数据库时编写了代码,并使用正确的登录信息,该页面不会重定向到我的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');
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句