_php example of MySQL database user authentication system code written in PHP

Source: Internet
Author: User
These two days by a friend entrusted, want me to help him write a user authentication system using MySQL database. I certainly not to shirk, had to spend a night of rest time, wrote a very simple PHP program.
The principle of user authentication is very simple: first, users need to fill in the page with user name and password, of course, unregistered users need to register first. Then call the database search for a corresponding user. If there is confirmation, do not remind the user to register first. Using PHP to do all of this is simple, but it is important to note that if you want to be able to identify the user in a future page, using PHP3 I can only think of a way to use cookies. To use the session, you can only wait for the PHP4 official version of the release!
The first step is to do a login page, here is not much to say. I've only made a very simple one and we can do it beautifully.
The second step begins the design of the confirmation program after login.
Login.php:
Copy Code code as follows:

mysql_connect ("localhost", "User", "password")
/* Connect to the database, username and password to modify their own * *
Or Die ("Unable to connect to the database, retry");
mysql_select_db ("UserInfo")
Or Die ("Cannot select Database, retry");
$today =date ("y-m-d h:i:s");
$query = "
Select ID
From Usertbl
where Name= $name and password= $password
* * Search and log in from the database to the user's corresponding information * *
";
$result =mysql_query ($query);
$numrows =mysql_num_rows ($result);
if ($numrows ==0) {
/* Verify that the same information can be found users, can not be unregistered * *
echo Illegal user

echo Please sign up first

Echo Retry

}
else{
$row =mysql_fetch_array ($result);
$id = $row [0];
$query = "
Update Usertbl
Set lastlogin= $today
where id= $id ";
$result =mysql_query ($query);
Setcookie ("Usercookie", "Welcome you, $name");
* * Here cookies are used to facilitate the next page certification.
But I have not developed this piece. I hope the interested friend will correct me.
Echo Login Succeeded

echo, come in!

}
?>

The third step of course is to do the registration of the page, also do not say more.
The fourth step is to register the identity confirmation and input database.
register.php:
Copy Code code as follows:

mysql_connect ("localhost", "User", "password")  /* modify your username and password */   
Or die ("Cannot connect to the database, retry");  
mysql_select_db ("userinfo")   
Or die (" Cannot select the database, please retry ");  
$query =" select id from usertbl where name= $name \ ";  
 /* searches the database for the same name user data */  
$result =mysql_query ($query);  
$numrows =mysql _num_rows ($result);  
if ($numrows!=0)  /* found out, of course, someone registered the same name first */  
{echo  Someone has registered this name, please select the name again!;}   
else  
{$query = "insert into usertbl values (0, $name, $password, \)";  
 /* cannot find the same input new user information */  
mysql_query ($query);  
echo  registered successfully;  & nbsp
echo  Please login!;}   
?> 
The next step is the use of cookies, I intended to use cookies to make each page identify the user, but because the other page is not ready, I do not know what information to use. So there's only one very simple use, and here's a reference to PHP:
Copy Code code as follows:

if (! $usercookie)
{Header ("illegal user");
}
?>
welcome.php:
Require ("cookie.php"); /* Call cookie.php*/
?>
Echo $usercookie;
?>
Here we have completed a very simple user authentication system, of course, if you want to use it to build a database. Here is the structure of my database table, the name of the library is userinfo.
Copy Code code as follows:

create table usertbl  
(  
ID  int auto_increment primary key,  
Name varchar (,  )
Password varchar (,  )
Lastlogin varchar   
); 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.