使用我的PHP登录页面进行mySQL数据库验证时遇到问题

时间:2018-12-16 09:18:03

标签: php mysql

我正在尝试使用PHP为我的网站创建一个登录页面,但似乎无法使登录验证正常工作。我正在尝试验证数据库中存在的用户名和密码,但似乎无法使其正常工作。有人看到我可能错过的东西吗?

<?php // login.php
// This page lets people log in to the site.
define('TITLE', 'Clermont County Disc Golf Club');
require('templates/header.html');
require_once('discgolf_fns.php');
// Print some introductory text:
print '<h2>Login Form</h2>
    <p align=center>Login to view member only secret meetups!</p>';
?><!DOCTYPE HTML>
<html lang="en">
<div id="wrapper">
<?php

$conn = db_connect();
$username = $_POST['username'];
$passwd = $_POST['passwd'];

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    if(!empty($_POST['username']) && (!empty($_POST['passwd']))){

        if (mysqli_connect_errno()) {
        echo "Error: Could not connect to database.  Please try again later.";
        exit;
        }
    $conn = db_connect();
    $query = "select * from user where username = '".$username."' and passwd = sha1('".$passwd."')";

    $result = $conn->query($query);
    if ($result->num_rows > 0)
        {
         session_start();
        $_SESSION['username'] = $username;   
        $_SESSION['loggedin'] = time(); 
        print "User found";
        $conn->close();

         ob_end_clean(); 
         header('Location: welcome.php');
         exit();

        }else{
            print '<p>The submitted username and password do not match. Go back and try again</p>';

        }


    }else{

        print '<p>Please make sure you enter both email and password.  Go back and try again</p>';
}


    }else{
  print '<form action="login.php" method="post" class="loginform">
     <p align=center><label for="username"> User Name: </label><input type="text" name="username" size"20"></p>
     <p align=center><label for="passwd"> Password: </label><input type="password" name="passwd" size"20"></p>
     <p align=center><input type="submit" name="submit" value="Log In!" class ="button--pill"></p>
    </form>';

}
?>
</div>
</html>
<?php include('templates/footer.html'); ?>

0 个答案:

没有答案