Login registration system is the most common daily Internet operation, I set a classification step-by-step Perfect registration system, if where wrong, please forgive me.
Languages used:php
Database:mysql
This implementation function:
1. User Registration
2. User Login
Main documents:
Complete code
1 SQL creates a user table in an existing database, Id,username,password three fields
Copy Code code as follows:
CREATE table user (id int () NOT null auto_increment,username varchar (), password varchar (), primary key (ID));
2 connect.php Database configuration file
<?php
$server = "localhost";//Host
$db _username= ""//Your database username
$db _password= "";//Your database password
$con = Mysql_connect ($server, $db _username, $db _password);//LINK Database
if (! $con) {
die ("Can ' t connect". Mysql_error ()) ;//If link fails output error
}
mysql_select_db (' Test ', $con);//Select Database (my test)
?>
3 signup.html Registration Form
4 signup.php Registration procedure
<?php
Header ("content-type:text/html; Charset=utf8 ");
if (!isset ($_post[' submit ')) {
exit ("Error Execution");
} To determine if there is a submit operation
$name =$_post[' name '];//post get the name
$password =$_post[' password '];//in the form Post gets password
include (' connect.php ') in the form;//linked database
$q = INSERT INTO user (Id,username,password) VALUES (NULL, ' $name ', ' $password ')//SQL $reslut to insert the value of the form into the database
($q, $con);//Execute SQL
if (! $reslut) {
die ( ' Error: '. Mysql_error ())//If SQL execution fails output error
}else{
echo "registered successfully";//Successful output registration succeeded
}
mysql_close ($con);//Close Database
?>
Registration process complete, below is User login
5 login.html Login Form
6 login.php Login Procedure
? PHP header ("content-type:text/html;
Charset=utf8 ");
if (!isset ($_post["Submit")) {exit ("error Execution"); }//Check for submit operation include (' connect.php ');//link Database $name = $_post[' name '];//post get user name form value $passowrd = $_post[' password '];//post get user password Single value if ($name && $passowrd) {//if user name and password are not empty $sql = "SELECT * from user where username = ' $n Ame ' and password= ' $passowrd ';//detect whether the database has corresponding username and password sql $result = mysql_query ($sql);//Execute SQL $rows = Mysql_num_rows ($result);//Returns a value if ($rows) {//0 false 1 True header ("refresh:0;url=welcome.html");//If successful jump
to welcome.html page exit;
}else{echo "username or password error";
echo "<script> settimeout (function () {window.location.href= ' login.html ';},1000);
</script> if the error using JS 1 seconds after the jump to the login page to try again;
}else{//if the username or password is empty echo "form is incomplete"; echo <script> settimeout (function () {window.location.href= ' login.html ';},1000);
</script> ";
If the error using JS 1 seconds after the jump to the login page to try again;
Mysql_close ();//Close Database?>
7 welcome.html Login Successful jump page
At this point a simple complete registration system is complete, the code is very simple did not consider the validation of security robustness, after the improvement.
I hope this article will help you learn about PHP programming.