设计一个数据表 admin
create database study;
use study;
create table admin(
id char ( 10 ) NOT NULL Primary Key ,
username char ( 10 ) NOT NULL ,
password char ( 20 ) NOt NULL ,
quanxian char ( 5 ) NOT NUll
) ;
insert into admin values ( '000001' , 'mllt9920' , '5201314' , 'root' ) ;
PHP连接MySQL数据库 connect.php
<?php
$servername = "localhost" ;
$username = "study" ;
$password = "mllt9920" ;
$database = new mysqli ( $servername , $username , $password ) ;
mysqli_select_db ( $database , 'study' ) ;
? >
Bootstrap框架 login.html
<! doctype html >
< html lang = " en" >
< head>
< meta charset = " utf-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1" >
< link href = " https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel = " stylesheet" integrity = " sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin = " anonymous" >
< title> 登陆</ title>
< style>
.mlmax {
max-width : 500px;
align-content : center;
margin : auto;
margin-top : 30px;
}
</ style>
</ head>
< body>
< script src = " https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity = " sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin = " anonymous" > </ script>
< div class = " mlmax border border-dark" >
< form class = " mb g-3" method = " POST" action = " login.php" enctype = " multipart/form-data" id = " bd" >
< div class = " card-header" align = " center" >
< kbd> 登陆</ kbd>
< small> | </ small>
注册
</ div>
< div class = " card-body" >
< div class = " col-auto" >
< label for = " usr" class = " visually-hidden" > 账号</ label>
< input type = " text" class = " form-control" id = " usr" placeholder = " UserName" name = " username" >
</ div>
< br>
< div class = " col-auto" >
< label for = " pwd" class = " visually-hidden" > 密码</ label>
< input type = " password" class = " form-control" id = " pwd" placeholder = " PassWord" name = " password" >
</ div>
< br>
< div class = " col-auto" align = " center" >
< button type = " submit" class = " btn btn-primary mb-3" id = " bt_dl" > 登陆</ button>
</ div>
</ div>
< div class = " card-footer" align = " center" > < small> 技术支持:萌狼工作室</ small> </ div>
</ form>
</ div>
</ body>
</ html>
判断用户登陆账号密码是否正确 login.php
< ? php
include ( 'connect.php' ) ;
session_start ( ) ;
$name = $_POST [ 'username' ] ;
$pwd = $_POST [ "password" ] ;
if ( empty ( $name ) ) {
echo "<script>alert('请输入用户名');location='login.html'</script>" ;
} else if ( empty ( $pwd ) ) {
echo "
<script>alert('请输入密码');location='login.html'</script>
" ;
} else {
$sql = "select username,password from admin where username='$name ' and password='$pwd '" ;
$result = mysqli_query ( $database , $sql ) ;
$row = mysqli_num_rows ( $result ) ;
if ( ! $row ) {
echo "<script>alert('密码错误,请重新输入');location='login.html'</script>" ;
}
else {
setcookie ( "username" , $name , time ( ) + 3600 * 24 * 365 ) ;
echo "<script>alert('登录成功');location='index.php'</script>" ;
} ;
}
? >