PHP development user login module PHP page
We introduced the main functions of user login earlier. This page implements these functions through PHP code.
Here we use POST to obtain data, such as username and password.
<?php $username = $_POST["username"]; //用戶名 $password = $_POST["password"]; //密碼 $code = $_POST["code"]; //驗(yàn)證碼 ?>
You need to connect to the database and determine whether the connection is successful. We have introduced the creation of database test and table login before. You can connect directly here.
<?php $link = mysqli_connect('localhost','root','root','test'); if (!$link) { die("連接失敗:".mysqli_connect_error()); } $sql = "select * from login"; $result = mysqli_query($link, $sql); $rows = mysqli_fetch_array($result); ?>
It is necessary to verify the user name and password to prevent illegal login access
<?php if($username == "") { //echo "請(qǐng)?zhí)顚?xiě)用戶名<br>"; echo"<script type='text/javascript'>alert('請(qǐng)?zhí)顚?xiě)用戶名');location='login.html'; </script>"; } if($password == "") { //echo "請(qǐng)?zhí)顚?xiě)密碼<br><a href='login.html'>返回</a>"; echo"<script type='text/javascript'>alert('請(qǐng)?zhí)顚?xiě)密碼');location='login.html';</script>"; } ?>
After the user fills in the user name and password, he needs to go to the database to check whether they are correct. Only after verification is correct can you log in normally and jump to the login success page.
<?php if($rows) { //拿著提交過(guò)來(lái)的用戶名和密碼去數(shù)據(jù)庫(kù)查找,看是否存在此用戶名以及其密碼 if ($username == $rows["username"] && $password == $rows["password"]) { //echo "驗(yàn)證成功!<br>"; echo "<script type='text/javascript'>alert('登陸成功');location='success.html';</script>"; } else { //echo "用戶名或者密碼錯(cuò)誤<br>"; echo "<script type='text/javascript'>alert('用戶名或者密碼錯(cuò)誤');location='login.html';</script>"; //echo "<a href='login.html'>返回</a>"; } } ?>
Complete login.php file code:
<?php //開(kāi)啟Session session_start(); header("Content-type:text/html;charset=utf-8"); $link = mysqli_connect('localhost','root','root','test'); if (!$link) { die("連接失敗:".mysqli_connect_error()); } //接受提交過(guò)來(lái)的用戶名及密碼 $username = $_POST["username"];//用戶名 $password = $_POST["password"];//密碼 $code = $_POST["code"]; //驗(yàn)證碼 if($username == "") { //echo "請(qǐng)?zhí)顚?xiě)用戶名<br>"; echo"<script type='text/javascript'>alert('請(qǐng)?zhí)顚?xiě)用戶名');location='login.html'; </script>"; } if($password == "") { //echo "請(qǐng)?zhí)顚?xiě)密碼<br><a href='login.html'>返回</a>"; echo"<script type='text/javascript'>alert('請(qǐng)?zhí)顚?xiě)密碼');location='login.html';</script>"; } if($code != $_SESSION['authcode']) //判斷填寫(xiě)的驗(yàn)證碼是否與驗(yàn)證碼PHP文件生成的信息匹配 { echo "<script type='text/javascript'>alert('驗(yàn)證碼錯(cuò)誤!');location='login.html';</script>"; } $sql = "select * from login"; $result = mysqli_query($link, $sql); $rows = mysqli_fetch_array($result); if($rows) { //拿著提交過(guò)來(lái)的用戶名和密碼去數(shù)據(jù)庫(kù)查找,看是否存在此用戶名以及其密碼 if ($username == $rows["username"] && $password == $rows["password"]) { //echo "驗(yàn)證成功!<br>"; echo "<script type='text/javascript'>alert('登陸成功');location='success.html';</script>"; } else { //echo "用戶名或者密碼錯(cuò)誤<br>"; echo "<script type='text/javascript'>alert('用戶名或者密碼錯(cuò)誤');location='login.html';</script>"; //echo "<a href='login.html'>返回</a>"; } } ?>
Note: This course is just a simple demonstration of user login. Its code is for learning reference only and cannot be directly used in projects.