PHP user regist...LOGIN

PHP user registration login system registration processing page

Registration processing page

The flow chart is as follows:

第二版注册.png

The detailed code is as follows:

<?php  
session_start();
//注册处理界面 regcheck.php
    if(isset($_POST["hidden"]) && $_POST["hidden"] == "hidden")  
    {  
        $user = trim($_POST["username"]);//trim()函数移除字符串两侧的空白字符
        $psw = md5(trim($_POST["userpwd"]));  
        $psw_confirm = md5(trim($_POST["confirm"])); 
        $code = $_POST["code"];
        if($user == "" || $psw == "" || $psw_confirm == "")  
        {  
            echo "<script>alert('请确认信息完整性!'); history.go(-1);</script>";  
        }
        else if($code != $_SESSION[' ver_code']){
            echo "<script>alert('验证码不正确,请重新输入!'); history.go(-1);</script>";
        }  
        else   
        {  
            if($psw == $psw_confirm)  
            {  
                $conn = mysqli_connect("localhost","root","root");   //连接数据库,帐号密码为自己数据库的帐号密码  
                if(mysqli_errno($conn)){
                    echo mysqli_error($conn);
                    exit;
                }
                mysqli_select_db($conn,"userdb");  //选择数据库  
                mysqli_set_charset($conn,'utf8'); //设定字符集  
                $sql = "select username from user where username = '$user'"; //SQL语句
                $result = mysqli_query($conn,$sql);    //执行SQL语句  
                $num = mysqli_num_rows($result); //统计执行结果影响的行数  
                
                if($num)    //如果已经存在该用户  
                {  
                    echo "<script>alert('用户名已存在'); history.go(-1);</script>";  
                }  
                else    //不存在当前注册用户名称  
                {   
                    $ip=ip2long($_SERVER['REMOTE_ADDR']); // 把ip地址转换成整型
                    $time=time();
                    $sql_insert = "insert into `user` (`username`,`userpwd`,`createtime`,`createip`) values('" . $user . "','" . $psw ."','".$time."','".$ip."')"; 
                    $res_insert = mysqli_query($conn,$sql_insert);  
                    if($res_insert)  
                    {  
                        echo "<script>alert('注册成功!');window.location.href='login.php';</script>";  
                    }  
                    else  
                    {  
                        echo "<script>alert('系统繁忙,请稍候!'); history.go(-1);</script>";  
                    }  
                }  
            }  
            else  
            {  
                echo "<script>alert('密码不一致!'); history.go(-1);</script>";  
            }  
        }  
    }  
    else  
    {  
        echo "<script>alert('提交未成功!');</script>";  
    }  
?>

Code explanation:

  • When you click Register on the registration page, you will enter the registration processing page

  • Determine whether the $_POST["hidden"] passed by the post method exists. If it does not exist, it will prompt that the submission has not been completed. If successful, return to the registration interface. If it exists, continue.

  • Get the passed value (use the trim() function to filter blank characters, and use the md5() function to encrypt the password), and then determine whether it is empty. If it is empty, a prompt will pop up. Return to the registration interface. If it is not empty, continue

  • Determine whether the verification code value passed by the post method is equal to the verification code value that previously existed in the session. If they are not equal, it will prompt that the verification code is incorrect. , return to the registration page, if they are equal, continue to execute

  • Determine whether the passed password and the confirmed password are equal. If they are not equal, it will prompt that the passwords are inconsistent. If they are equal, continue

  • Connect to the database, select the database we created, set the character set, query the database through the user name, if there is a result, it will prompt that the user name exists, return to the registration page, if there is no result, execute the database insertion statement, insert data ( The fields inserted into the database add the registration time and the IP number used for registration)

  • Use the return value of the insert statement to determine whether the insertion is successful. If it is not successful, return to the registration page to re-register. If it is successful, It will prompt that the registration is successful and jump to the login page


Next Section
<?php session_start(); //注册处理界面 regcheck.php if(isset($_POST["hidden"]) && $_POST["hidden"] == "hidden") { $user = trim($_POST["username"]);//trim()函数移除字符串两侧的空白字符 $psw = md5(trim($_POST["userpwd"])); $psw_confirm = md5(trim($_POST["confirm"])); $code = $_POST["code"]; if($user == "" || $psw == "" || $psw_confirm == "") { echo "<script>alert('请确认信息完整性!'); history.go(-1);</script>"; } else if($code != $_SESSION[' ver_code']){ echo "<script>alert('验证码不正确,请重新输入!'); history.go(-1);</script>"; } else { if($psw == $psw_confirm) { $conn = mysqli_connect("localhost","root","root"); //连接数据库,帐号密码为自己数据库的帐号密码 if(mysqli_errno($conn)){ echo mysqli_error($conn); exit; } mysqli_select_db($conn,"userdb"); //选择数据库 mysqli_set_charset($conn,'utf8'); //设定字符集 $sql = "select username from user where username = '$user'"; //SQL语句 $result = mysqli_query($conn,$sql); //执行SQL语句 $num = mysqli_num_rows($result); //统计执行结果影响的行数 if($num) //如果已经存在该用户 { echo "<script>alert('用户名已存在'); history.go(-1);</script>"; } else //不存在当前注册用户名称 { $ip=ip2long($_SERVER['REMOTE_ADDR']); // 把ip地址转换成整型 $time=time(); $sql_insert = "insert into `user` (`username`,`userpwd`,`createtime`,`createip`) values('" . $user . "','" . $psw ."','".$time."','".$ip."')"; $res_insert = mysqli_query($conn,$sql_insert); if($res_insert) { echo "<script>alert('注册成功!');window.location.href='login.php';</script>"; } else { echo "<script>alert('系统繁忙,请稍候!'); history.go(-1);</script>"; } } } else { echo "<script>alert('密码不一致!'); history.go(-1);</script>"; } } } else { echo "<script>alert('提交未成功!');</script>"; } ?>
submitReset Code
ChapterCourseware