php method to implement the login function: 1. Connect and create a database; 2. Use the layui framework to determine whether the username and password input is empty; 3. Create "login.php" to determine the username and password Correctness; 4. Check the login effect.
php login function implementation
It is natural to start with the simplest function. My first task was to perform a login operation, which was actually not as simple as I imagined.
1. The first thing is to connect and create the database.
I wrote this part in model.php
$userName='root'; $passWord=''; $host='localhost'; $dataBase='login'; //创建连接 $conn=mysqli_connect($host,$userName,$passWord,$dataBase);
2. To write the front-end page, in order to become proficient in the front-end framework, I used The layui framework makes the interface, and there is a js code in front of it to determine whether the username and password input is empty
nbsp;html> <script></script> <link> <meta> <title>注册登录</title> <script> function InputCheck() { if (Login.username.value == "") { alert("请输入用户名!"); Login.username.focus(); return (false); } if (Login.password.value == "") { alert("请输入密码!"); Login.password.focus(); return (false); } } </script> <p> </p><p> </p><p> </p>
3.login.php is used to determine the correctness of the username and password. I read about this on the Internet There are many methods, all kinds of. Before I encountered any obstacles, I decided to use a simple form first, which is to use SQL statements to query the result set of user name and password. If the result set is empty, the user does not exist.
<?php //数据库连接 require_once 'model.php'; //从登录页接受来的数据 $name=$_POST['username']; $pwd=$_POST['password']; $sql="select id,username,password from user where username='$name' AND password='$pwd';"; $result=mysqli_query($conn,$sql); $row=mysqli_num_rows($result); if(!$row){ echo "<script>alert('密码错误,请重新输入');location='login.html'</script>"; } else{ echo "<script>alert('登录成功');location='123'</script>"; };
4. File directory
5. Effect
6. Summary
Overall it is not particularly difficult because the function is very simple. The next task is to add the registration function and verification code function.
Recommended: "PHP Tutorial"
The above is the detailed content of How to implement php login function. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
