PHP user regist...LOGIN

PHP user registration login system login processing page

Login processing page

The flow chart is as follows:

第二版登录.png

The code is as follows:

<?php
session_start();
//登录处理界面 logincheck.php
//判断是否按下提交按钮
    if(isset($_POST["hidden"]) && $_POST["hidden"] == "hidden")  
    {  
    //将用户名和密码存入变量中,供后续使用
        $user = trim($_POST["username"]);//trim()函数移除字符串两侧的空白字符
        $psw = md5(trim($_POST["userpwd"]));//密码使用md5()加密一次,存入数据库
        $code = $_POST["code"];
        if($user == "" || $psw == "")  
        {
        //用户名或者密码其中之一为空,则弹出对话框,确定后返回当前页的上一页  
            echo "<script>alert('请输入用户名或者密码!'); history.go(-1);</script>";  
        }else if($code != $_SESSION[' ver_code']){
            echo "<script>alert('验证码不正确,请重新输入!'); history.go(-1);</script>";
        }  
        else  
        {  //确认用户名密码验证码不为空,则连接数据库
            $conn = mysqli_connect("localhost","root","root");//数据库帐号密码为安装数据库时设置
             if(mysqli_errno($conn)){
                echo mysqli_errno($conn);
                exit;
             }
            mysqli_select_db($conn,"userdb");  
            mysqli_set_charset($conn,'utf8'); 
            $sql = "select username,userpwd from user where username = '$user' and userpwd = '$psw'";  
            $result = mysqli_query($conn,$sql);  
            $num = mysqli_num_rows($result);  
            if($num)  
            {  
                echo "<script>alert('成功登录'); window.location.href='index.php';</script>";  
            }  
            else  
            {  
                echo "<script>alert('用户名或密码不正确!');history.go(-1);</script>";  
            }  
        }  
    }  
    else  
    {  
        echo "<script>alert('提交未成功!');</script>";  
    }  
  
?>

Code explanation:

  • Enter when you click login on the login page Login processing page

  • Determine whether the $_POST["hidden"] passed in the post method exists. If it does not exist, it will prompt that the submission was unsuccessful and return to the login 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 login 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 login page, if equal, continue to execute

  • ## Connect to the database, select the database we created, set the character set, query the database through the user name and password, and determine whether the user name and password are Exists in the database. If it does not exist, it will prompt that the user name or password is incorrect and return to the login page. If it exists, it will prompt successful login and jump to the homepage


<?php session_start(); //登录处理界面 logincheck.php //判断是否按下提交按钮 if(isset($_POST["hidden"]) && $_POST["hidden"] == "hidden") { //将用户名和密码存入变量中,供后续使用 $user = trim($_POST["username"]);//trim()函数移除字符串两侧的空白字符 $psw = md5(trim($_POST["userpwd"]));//密码使用md5()加密一次,存入数据库 $code = $_POST["code"]; if($user == "" || $psw == "") { //用户名或者密码其中之一为空,则弹出对话框,确定后返回当前页的上一页 echo "<script>alert('请输入用户名或者密码!'); history.go(-1);</script>"; }else if($code != $_SESSION[' ver_code']){ echo "<script>alert('验证码不正确,请重新输入!'); history.go(-1);</script>"; } else { //确认用户名密码验证码不为空,则连接数据库 $conn = mysqli_connect("localhost","root","root");//数据库帐号密码为安装数据库时设置 if(mysqli_errno($conn)){ echo mysqli_errno($conn); exit; } mysqli_select_db($conn,"userdb"); mysqli_set_charset($conn,'utf8'); $sql = "select username,userpwd from user where username = '$user' and userpwd = '$psw'"; $result = mysqli_query($conn,$sql); $num = mysqli_num_rows($result); if($num) { echo "<script>alert('成功登录'); window.location.href='index.php';</script>"; } else { echo "<script>alert('用户名或密码不正确!');history.go(-1);</script>"; } } } else { echo "<script>alert('提交未成功!');</script>"; } ?>
submitReset Code
ChapterCourseware