(B) to achieve a white registering operation, as shown in the following code (implemented on a log)
Second, to achieve registration
1. Layout:
lable {display: inline-flex; width: 30px; height: 30px;} // achieve automatic alignment of the input box, otherwise uneven.
2. Ideas
a) the authentication data reasonableness:
1. When the form is submitted (onsubmit attribute), by JS of document.getElementById ( "id"). Value to obtain account passwords
2. str.trim () == '' confirmation is not empty, str.indexOf ( "") determines whether the user name without the spaces
Analyzing two pwd1 == pwd2 same password, pwd.length <8 determines the length of the password is not less than 8
3. above are not met then the alert, and returns false, denying the author
b) verify that the user name is repeated:
By the name attribute in a form to post background regist_judge), using $ _POST [ 'name'] to obtain the value of submitting the form, by $ mysqli_result = db-> query ( "sql statement Find") information is stored in database tables db, in while in = $ mysqli_result (fetch_array (MYSQL_ASSOC)) acquiring a first message (3-> 2-> 1) by $ row, with $ row [ 'user'] == $ username determines whether the user name already exists in the database
i) Repeat:
header () points to the login screen, and by $ _SESSION [ 'flag'] registration failure message is transmitted, the registration interface to alert (user login failure information);
ii) will not be repeated:
if ($ judge = $ db-> query ($ sql insert statement) === true) // echo generating a tag inserted successfully used to jump to the login screen
c) repeating process Username:
After the background returns a registration interface, using if (isset ($ _ SESSION [ 'flag'])) determines whether there In Flag, exists then the user name repeated, echo JS in Alert (); and using unset ($ _ SESSISON [ 'flag ']) destroy it
Code
regist.php // registration interface
<?php session_start(); if(isset($_SESSION['flag']) ){ echo "<script type='text/javascript'>alert('用户名已存在')</script>"; unset($_SESSION['flag']); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>注册</title> <style type="text/css"> label{width:80px;display:inline-flex;height:30px;} .registall{border:10x solid pink;width:600px;height:300px;margin:200px auto;border:2px solid pink;box-shadow:0 0 5px 0 #aaa; border-radius:30px;} .regist .submit{position:relative;left:115px;top:10px;width:75px;height:27px;} .regist {width:300px;margin:20px auto;} .registall .title{font-size:23px;color:pink;text-align:center;margin-bottom:20px;margin-top:38px;} </style> <script type="text/javascript"> function judge(){ var username = document.getElementById("username").value; var password1 = document.getElementById ( "password1"). value; var password2 = document.getElementById ( "password2"). value; / * Analyzing data is valid * / IF . (username == '' || username TRIM () = = '') alert ( "user name can not be empty" ); the else IF (username.indexOf ( "") = - 1) Alert (! "user name can not contain spaces" ); the else IF (password1 == '' | | password2 == '' || password1. the TRIM () == '' || password2. the TRIM () == '' ) Alert ( "password can not be blank" ); else if(password1 !=password2) Alert ( "two passwords must match" ); the else IF (password1.length <. 8) Alert ( "not less than the length of the password. 8" ); the else return to true ; return to false ; } </ Script> </ head> <body> <div class = "registall"> <div class = "title"> registration interface </ div> <form class = "REGIST" Action = "regist_judge.php" Method = "POST" the onsubmit = "return Judge ( ); "> <label> username: </ label> <input id = "username" name = "username" type = "text" placeholder = " Enter your user name" /> </ br> <label> Password: </ label> <INPUT ID = "password1" name = "password1" type = "password" placeholder = "password length exceeds. 6" /> </ br> <label> Confirm Password: </ label > <the iNPUT the above mentioned id = "password2" name = "password2" of the type = "password" placeholder = "Please enter your password again" /> </ br> <the iNPUT of the type = "the Submit" value = "registered" class = "the Submit" / > </ form> </ div> </ body> </ HTML>
regist_judge.php // login interface judgment
<?php session_start(); include("connect.php"); $username = $_POST["username"]; $password1 = $_POST["password1"]; $password2 = $_POST["password2"]; $sql_judge = "select username from user_msg where username='{$username}'"; $mysqli_result = $db->query($sql_judge); while($row = $mysqli_result->fetch_array (MYSQL_ASSOC)) {
/ * is determined whether there is the user name * / IF ( $ Row [ 'username'] == $ username ) { $ _SESSION [ 'In Flag'] =. 1 ;
header ( "LOCATION: regist.php" ) ; Exit (); } } $ SQL = "iNSERT user_msg (username, password) values ( '{ $ username }', '{ $ password1 }')" ;
// into the database IF ( $ Judge = $ DB -> Query ( $ SQL ) === to true ) { echo "<br/><div style='text-align:center;'>注册成功<br/><br/> <strong> <a href='login.php' style='text-decoration:none;font-size:20px;color:pink;'> 登录</a></strong></div>"; } ?>