HTML+PHP+MySQL 制作最基礎的登錄頁面及驗證


第一步

進入mysql操作終端

新建一個數據庫web1:create database web1;

然后在這個數據庫里面新建一張表test:create table test(user varchar(15),password varchar(18));

表的字段為user和password,這個表用於裝入用戶登錄的用戶名和密碼;

將自己想要的用戶名和密碼插入到test表中,這里我插了兩條,方便后面驗證。

第二步

編寫登錄頁面的index.html文件,用於表單獲取數據,在瀏覽器上顯示並且登錄跳轉到數據處理的login.php頁面。

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8">
        <title>一個沒有過濾的登錄</title>
    </head>
    <body style="background-color:#0066CC">
        <h2 align="center" style="color: white">管理后台登錄</h2>
        <form align="center" action="login.php" method="post" enctype="multipart/form-data">
<!-- 此表單獲取的內容將提交給login.php處理,且請求為post -->
<p style="width:100%;height:30px;display:block;line-height:200px;text-align:center;color:white;font-size:16px;">賬號:<input type="text" name="user" value="" max="10"></p> <p style="width:100%;height:30px;display:block;line-height:200px;text-align:center;color:white;font-size:16px;">密碼:<input type="password" name="pass" value="" min="6" max="16"></p> <p style="width:100%;height:30px;display:block;line-height:200px;text-align:center;"><input type="submit" name="submit" value="登錄"></p> </form> </body> </html>

第三步

編寫后台數據處理的login.php文件,用於連接數據庫並且獲取用戶在index.html上的表單提交的數據,然后在數據庫里面查詢是否含有此用戶和密碼,只有同時正確才能返回登錄成功。

<?php
$con=mysqli_connect("127.0.0.1","root","root","web1"); //連接數據庫,且定位到數據庫web1
if(!$con){die("error:".mysqli_connect_error());} //如果連接失敗就報錯並且中斷程序
$user=$_POST['user'];
$pass=$_POST['pass'];
if($user==null||$pass==null){
    echo "<script>alert('你不是管理員吧!')</script>";
    die("賬號和密碼不能為空!");
}
function check_param($value=null) { //過濾注入所用的字符,防止sql注入
$str = 'select|insert|and|or|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile';
if(eregi($str,$value)){
exit('參數非法!');
}
return true;
}
if(check_param($user)==true&&check_param($pass)==true){
  $sql='select * from test where user='."'{$user}'and password="."'$pass';";
  $res=mysqli_query($con,$sql);
  $row=$res->num_rows; //將獲取到的用戶名和密碼拿到數據庫里面去查找匹配
  if($row!=0)
  {
      echo "<h1>登錄成功,歡迎您&nbsp{$user}!</h1>";
  }
  else
  {
      echo "用戶名或密碼錯誤";
  }
}
?>

第四步

驗證

 

 

sql注入驗證

 

 

 

 

 參考鏈接:https://blog.csdn.net/cwh0908/article/details/83244954

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM