PHP login registration registration
In the previous course, we will go to the registration page and jump to reg.php
Submit the form to the regin.php file
Let’s do it next Take a look at the registration steps
Connect to the database
Get the form information
Judgment Whether the form is empty
Write sql statement and add content to the database
Judge whether the registration is successful
Note: What we need to pay attention to here is the third step. When the database contains the information submitted by the form, the registration should not be successful at this time. To put it more simply, for example, Zhang San has already registered. When you use the username Zhang San, we should give the information that the username has been registered
Let’s explain it in detail below. Let’s take a look at the flow chart first:
Connecting to the database is the same as the registration page. Introduce our conn.php file to
get the form information. We can do some filtering operations, such as removing spaces. trim() function To encrypt the password, use md5()
Code to go to:
$name=trim($_POST['username']);
$ password=$_POST['password'];
trim function, filter spaces, if not, we add a lot of spaces after the user name, submit the form, open the firebug debugging tool, we can see the entered user name There will be a lot of spaces at the end. Using the trim function, we can filter out the spaces in the form.
As shown in the figure below
The trim() function is not used After submission, we can see that there are a lot of spaces.
The effect after use is as follows
After md5() encrypted output You will see a 32-bit cipher text
$sql = "select * from user where username='$name'";
$info = mysql_query($sql);
$res = mysql_num_rows($info);
Then judge $res. If it exists, it will prompt the user to have been registered and jump to the registration page
If not, perform the registration operation
Before doing the registration operation, we also need to judge the form Is the information empty? If it is empty, it will return to the registration page and give a prompt message.
The code is as follows:
if(empty($name)){
echo "<script> ;alert('Username cannot be empty');location.href='reg.php';</script>";
}else if(empty($password)){
?echo "< script>alert('Password cannot be empty');location.href='reg.php';</script>";
}else{
//Registration operation
}
The registration operation code is as follows:
???????? $sql1?="insert into user(username,password) values('".$name."','"?.$password ."')";
$result = mysql_query($sql1);
if($result){
echo "<script>alert('Registration successful')</script>";
} Else {
echo "& lt; script & gt; alert ('register failure') & lt;/script & gt;";
}
<?php require_once("conn.php");//首先鏈接數(shù)據(jù)庫 $name=trim($_POST['username']); //trim函數(shù),過濾空格,如果不加,我們在用戶名后面添加很多空格,提交表單,打開firebug //調(diào)試工具,我們可以到輸入的用戶名后面會有很多空格,使用trim函數(shù),我們可以把表單中空格給過濾掉 $password=$_POST['password']; $sql = "select * from user where username='$name'"; $info = mysql_query($sql); $res = mysql_num_rows($info); if(empty($name)){ echo "<script>alert('用戶名不能為空');location.href='reg.php';</script>"; }else if(empty($password)){ echo "<script>alert('密碼不能為空');location.href='reg.php';</script>"; }else{ if($res){ echo "<script>alert('用戶名已存在');location.href='reg.php';</script>"; }else{ $sql1 ="insert into user(username,password) values('".$name."','" .md5($password)."')"; $result = mysql_query($sql1); if($result){ echo "<script>alert('注冊成功')</script>"; }else{ echo "<script>alert('注冊失敗')</script>"; } } } ?>## In this way, we have completed a simple login and registration. Let's create a few files and copy the code locally to test it