验证用户对数据库的登录

时间:2018-02-16 19:38:32

标签: php mysql database forms connect

所以我正在尝试创建一个登录系统。我无法弄清楚如何检查我的MySQL数据库中的信息登录。即使数据库表为空,我也可以登录。我究竟做错了什么?我可以连接好,这只是验证部分,这一切都搞砸了。

表格:

<form name='login' method="POST" action="home.php";;>

Username: <input type="text" name="username" required /><br>
Password: <input type="password" name="password" required /><br>

<input type="submit" name="login" value="Login" />
</form>

这是home.php:

<?php  //Start the Session
session_start();
 require('connect.php');
//3. If the form is submitted or not.
//3.1 If the form is submitted
if (isset($_POST['username']) and isset($_POST['password'])){
//3.1.1 Assigning posted values to variables.
$username = $_POST['username'];
$password = $_POST['password'];
//3.1.2 Checking the values are existing in the database or not
$query = "SELECT * FROM `user` WHERE username='$username' and password='$password'";

$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
$count = mysqli_num_rows($result);
//3.1.2 If the posted values are equal to the database values, then session will be created for the user.
if ($count == 1){
$_SESSION['username'] = $username;
}else{
//3.1.3 If the login credentials doesn't match, he will be shown with an error message.
$fmsg = "Invalid Login Credentials.";
}
}
//3.1.4 if the user is logged in Greets the user with message
if (isset($_SESSION['username'])){
$username = $_SESSION['username'];
echo '<head>
<title>Sheeplets World: Home Page</title>
<link type="text/css" rel="stylesheet" href="index.css" media="screen" />
</head>

<body>
<div id=allcontent>

<h1 id=header>Sheeplets World</h1>

<div id=rcorners>
<h2 id=topic1>Welcome ' . $username . '!</h2>

<p       class="content1">abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
    </div>
    </div>
    </body>';
    echo "<a href='logout.php'>Logout</a>";
    ?>

这是connect.php:

<?php
$connection = mysqli_connect('localhost', 'root', '', 'testing');
if (!$connection){
    die("Database Connection Failed" . mysqli_error($connection));
}
$select_db = mysqli_select_db($connection, 'testing');
if (!$select_db){
    die("Database Selection Failed" . mysqli_error($connection));
}?>

提前致谢!

1 个答案:

答案 0 :(得分:-1)

尝试打印已获取的记录并计数以使用

调查$ count和查询结果的内容
   echo $count;

   $row=mysqli_fetch_assoc($result);
   print_r($row);

登录的示例代码

<?php
session_start();
$errorMsg = "";
$validUser = $_SESSION["login"] === true;
if(isset($_POST["sub"])) {
  $validUser = $_POST["username"] == "admin" && $_POST["password"] == "password";
  if(!$validUser) $errorMsg = "Invalid username or password.";
  else $_SESSION["login"] = true;
}
if($validUser) {
   header("Location: /login-success.php"); die();
}
?>
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  <title>Login</title>
</head>
<body>
  <form name="input" action="" method="post">
    <label for="username">Username:</label><input type="text" value="<?= $_POST["username"] ?>" id="username" name="username" />
    <label for="password">Password:</label><input type="password" value="" id="password" name="password" />
    <div class="error"><?= $errorMsg ?></div>
    <input type="submit" value="Home" name="sub" />
  </form>
</body>
</html>

参考link