PHP login regis...LOGIN

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

  1. Connect to the database

  2. Get the form information

  3. Judgment Whether the form is empty

  4. Write sql statement and add content to the database

  5. 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:


$ 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


##Username Zhang San, password 123456, click to register


The appearance of such ciphertext will have a certain effect on the security of our account

Next we need to determine whether the user name has been registered

First obtain the form information, and then go Query whether it exists in the database table

The code is as follows

$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:

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>";

//Registration operation


The registration operation code is as follows:

         $sql1 ="insert into user(username,password) values('".$name."','" .$password ."')";
$result = mysql_query($sql1);
echo "<script>alert('Registration successful')</script>";
} Else {
echo "& lt; script & gt; alert ('register failure') & lt;/script & gt;";

## The complete source code is as follows:


       $sql = "select * from user where username='$name'";
        $info = mysql_query($sql);
        $res = mysql_num_rows($info);

        echo "<script>alert('用户名不能为空');location.href='reg.php';</script>";
    }else if(empty($password)){
        echo "<script>alert('密码不能为空');location.href='reg.php';</script>";
            echo "<script>alert('用户名已存在');location.href='reg.php';</script>";
            $sql1 ="insert into user(username,password) values('".$name."','" .md5($password)."')";
            $result = mysql_query($sql1);
                     echo "<script>alert('注册成功')</script>";
                     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

Next Section

<?php require_once("conn.php");//首先链接数据库 $name=trim($_POST['username']); //trim函数,过滤空格,如果不加,我们在用户名后面添加很多空格,提交表单,打开firebug //调试工具,我们可以到输入的用户名后面会有很多空格,使用trim函数,我们可以把表单中空格给过滤掉 $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>"; } } } ?>
submitReset Code