首页 > 解决方案 > 使用我的 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'); ?>

标签: phpmysql

解决方案


推荐阅读