我如何获得 PHP 登录脚本来正确登录用户?

How can i get PHP login script to log users in correctly?

我是 PHP 的新手,我正在尝试开发一个简单的登录系统,它会回显成功消息并重定向到安全页面,当详细信息有误时,它会回显错误消息并重新加载登录表单。

我已经尝试了一段时间但无法弄清楚,尽管我有一些功能可以将其定向到正确的页面。

我在 PhpMyAdmin 上的数据库配置正确。此外,我们将不胜感激对会议的任何帮助。

PHP 代码:

<?php 

$servername = "localhost";
$username = "root";
$password = "cornwall";

$con=mysqli_connect('localhost','root','cornwall','ibill');
// This code creates a connection to the MySQL database in PHPMyAdmin named 'ibill':

$username =  $_POST['username'];
$password =  $_POST['password'];
//These are the different PHP variables that store my posted data.

$login="SELECT * FROM users WHERE username='$username' AND password='$password'";
$result=mysqli_query($con, $login);
$count=mysqli_num_rows($result);
//This is the query that will be sent to the MySQL server.

if($count==1)
{
  header('Location: http://localhost/projects/ibill_v3/html/main.html#home');
  exit();
}
//This checks the 'user_details' database for correct user registration details and if successful, directs to home page.
else {
   header('Location: http://localhost/projects/ibill_v3/html/loginform.html');
   echo "Wrong details";
  exit();
}
//If login details are incorrect

/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
?>

HMTL 代码

<!DOCTYPE html>
<html lang="en">

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1; minimum-scale=1;">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
  <link href="/projects/ibill_v3/css/mainstyles.css" rel="StyleSheet"/>
  <link href="/projects/ibill_v3/css/loginform.css" rel="StyleSheet"/>
  <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
  <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
  <script src="script.js"></script>
  <script type='text/javascript' src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
  <script type='text/javascript'>
            $(document).on('pageinit', function(){
                $('.loginform').validate({ // initialize the plugin
                    // rules & options
                });
            });  
  </script>
</head>

<body>
<!--********************************LOGIN FORM PAGE**********************************************-->
<!--****************************************************************************************-->

<!--********************************HEADER**********************************************-->
<div data-role="page" id="loginform">
  <div data-role="header" data-id="foo1" data-position="fixed">
    <h1>Register</h1>
  </div>
<!--********************************HEADER**********************************************-->

<!--********************************MAIN**********************************************-->
  <div data-role="main" class="ui-content">
    <img class="mainlogo" src="/projects/ibill_v3/img/ibill logo.png" alt="iBill Logo" width="250" height="190">
    <h2>Sign in</h2>
    <section class="loginform">
      <form data-ajax="false" method="POST" action="loginform.php" > 
        <ul>
          <li>
            <label for="username">Username</label>
            <input type="text" name="username" id="username" class="required" minlength="5" placeholder="enter username (min-5 characters)">
          </li>
          <li>
            <label for="password">Password</label>
            <input type="password" name="password" placeholder="enter password"  minlength="6">
          </li>

          <div id="loginformbutton">
            <button class='active' type='submit' value='submit'>Sign in</button>
          </div>
            <p>Don't have an account? Sign up!</p>
          <div id="registerbutton">
            <a href="/projects/ibill_v3/html/register.html" data-role="button">Register</a>
          </div>
        </ul>
      </form>
    </section>

  </div>
<!--********************************MAIN**********************************************-->

<!--********************************FOOTER**********************************************-->
  <div data-role="footer">
    <footer class="footer">
        <p>awilliams&copy;</p>
    </footer>
  </div>
</div>
<!--********************************END OF LOGIN FORM PAGE**********************************************-->
<!--****************************************************************************************-->
</body>

我认为这是因为您重定向到没有 post 的同一页面。我没有查看所有代码,这只是我的第一次尝试

这不会出现在 loginform.html:

echo "Wrong details";

使用类似的东西:

$_SESSION['errorMessage'] = "Wrong details";
header('Location: http://localhost/projects/ibill_v3/html/loginform.html');
exit();

然后在 loginform.html 上添加此代码以显示错误消息:

if(isset( $_SESSION['errorMessage'])) echo $_SESSION['errorMessage'];
...
else 
{
    header('Location:    http://localhost/projects/ibill_v3/html/loginform.html');
   echo "Wrong details";
   exit();
}

以上将在您的 echo 语句到达之前重定向,因此不会显示任何内容。

其次,下面一行:
如果您使用 echo 语句,<form data-ajax="false" method="POST" action="loginform.php" > 不会将任何数据发送回包含表单的文件。它将重定向到 loginform.php 并且如果您没有使用您的表单明确重定向回页面,它将留在那里。

改为使用:
<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> 作为表单的操作。然后在 HTML.
中的表格之前的某处包含您的 loginform.php 这会将数据发送回表单文件并将特殊字符替换为 HTML 实体(出于安全考虑),它还允许您将 echo 或变量用于 return 消息用户。

loginform.php 将需要检查是否发布了特定输入:

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    if($_POST['username'] && $_POST['password'])
    {
        //do work
    }
} 

这是一个基本的 php 表单教程,可以帮助您入门:php tutorial