PHP user registration login system registration processing page
Registration processing page
The flow chart is as follows:
The detailed code is as follows:
<?php session_start(); //注冊(cè)處理界面 regcheck.php if(isset($_POST["hidden"]) && $_POST["hidden"] == "hidden") { $user = trim($_POST["username"]);//trim()函數(shù)移除字符串兩側(cè)的空白字符 $psw = md5(trim($_POST["userpwd"])); $psw_confirm = md5(trim($_POST["confirm"])); $code = $_POST["code"]; if($user == "" || $psw == "" || $psw_confirm == "") { echo "<script>alert('請(qǐng)確認(rèn)信息完整性!'); history.go(-1);</script>"; } else if($code != $_SESSION[' ver_code']){ echo "<script>alert('驗(yàn)證碼不正確,請(qǐng)重新輸入!'); history.go(-1);</script>"; } else { if($psw == $psw_confirm) { $conn = mysqli_connect("localhost","root","root"); //連接數(shù)據(jù)庫(kù),帳號(hào)密碼為自己數(shù)據(jù)庫(kù)的帳號(hào)密碼 if(mysqli_errno($conn)){ echo mysqli_error($conn); exit; } mysqli_select_db($conn,"userdb"); //選擇數(shù)據(jù)庫(kù) mysqli_set_charset($conn,'utf8'); //設(shè)定字符集 $sql = "select username from user where username = '$user'"; //SQL語(yǔ)句 $result = mysqli_query($conn,$sql); //執(zhí)行SQL語(yǔ)句 $num = mysqli_num_rows($result); //統(tǒng)計(jì)執(zhí)行結(jié)果影響的行數(shù) if($num) //如果已經(jīng)存在該用戶 { echo "<script>alert('用戶名已存在'); history.go(-1);</script>"; } else //不存在當(dāng)前注冊(cè)用戶名稱(chēng) { $ip=ip2long($_SERVER['REMOTE_ADDR']); // 把ip地址轉(zhuǎn)換成整型 $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('注冊(cè)成功!');window.location.href='login.php';</script>"; } else { echo "<script>alert('系統(tǒng)繁忙,請(qǐng)稍候!'); 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