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

用户名

我已经配置了数据库,并在测试数据库时编写了代码,并使用正确的登录信息,该页面不会重定向到我的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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

每当我编译到设备时,Xcode都会询问用户名和密码

当使用正确的帐户和密码进行“ npm login”登录时,为什么仍然获得“用户名或密码错误”?

我收到一个错误“ {错误:无效的登录名:535-5.7.8用户名和密码不被接受。” 当我尝试从Node JS使用模块nodemailer发送邮件时

当我尝试在OSX上缓存我的Github用户名和密码时未收到钥匙串提示

当我添加用户名功能时,php登录表单无法正常工作

用户名和密码为空时,如何在单击“登录”按钮时停止重定向?

Spring MVC-尽管用户名和密码正确,但仍将我重定向到我的登录失败映射

如何使我的Spring Boot应用程序使用给定的用户名和密码登录到keycloak?

我可以使用任何用户名和密码登录到Firebird 3数据库

我没有输入用户名或密码,当我单击登录按钮时,它强制关闭了我的应用程序

我的用户名输入重定向到“关于”页面

我的登录始终成功,无论用户名或密码正确/错误

当我输入错误的用户名或密码时,只会说**请输入用户名和密码**

php mysql会话使用错误的用户名和密码重定向到主页

为什么不管我输入的密码和用户名如何,这段代码都重定向到a.php?

我想向发送OTP消息。但是当我输入用户名和密码时,它显示了错误的用户名和密码

我的登录用户名和密码是什么?

我正在尝试使用bash脚本自动登录npm,但我不知道在请求时ti如何输入用户名和密码。是否可以?

PHP登录“无效的用户名和密码”使用MySQL

正确的用户名密码未登录django

登录系统,输入用户名和密码后登录

仅当使用 jquery 未正确输入用户名或密码时,如何才能显示我的错误消息?

我有一个登录问题 用户名和密码是正确的

输入正确的用户名和密码后无法登录

为什么这个登录程序在我第一次正确输入用户名和密码时可以登录?

我需要验证用户名和密码才能进入仪表板,如果用户未登录,还需要重定向到登录页面

登录问题我可以使用任何用户名和密码登录

需要在 C# 中使用用户名和密码创建登录窗口,但是当我输入不正确的用户名或密码时,出现错误

尝试连接到 somee.com 数据库时是否使用我的登录用户名和密码?