MySQL database user authentication system code written in PHP _ PHP Tutorial

Source: Internet
Author: User
The code of the MySQL database user authentication system written in PHP. I was entrusted by my friends these two days and asked me to help him write a user authentication system using the MySQL database. Of course, I had to spend one night's rest, and wrote a very simple one that was entrusted by my friends over the past two days, I want to help him write a user authentication system that uses the MySQL database. Of course, I had to spend one night off and write a very simple PHP program.
The principle of user authentication is simple: first, you need to enter the user name and password on the page. of course, users who are not registered must first register. Then, you can call the database to search for users. If yes, confirm it. if no, the user is reminded to register first. It is easy to use PHP to complete this process, but it should be noted that if you want to confirm the user identity in future pages, you can only come up with a method to use PHP3. To use the session, you have to wait for the release of the official PHP4 version!
The first step is to create a login page. I will not talk about it here. I just made a simple one, and you can do it well.
Step 2: Design the validation program after logon.
Login. php:

The code is as follows:


Mysql_connect ("localhost", "user", "password ")
/* Connect to the database, and modify the user name and password */
Or die ("unable to connect to the database, please try again ");
Mysql_select_db ("userinfo ")
Or die ("unable to select database, please try again ");
$ Today = date ("Y-m-d H: I: s ");
$ Query ="
Select id
From usertbl
Where name = $ name and password = $ password
/* Search for and log on to user information from the database */
";
$ Result = mysql_query ($ query );
$ Numrows = mysql_num_rows ($ result );
If ($ numrows = 0 ){
/* Verify that users with the same data can be found. if not, they are not registered */
Illegal echo user

Register echo 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, $ name ");
/* Cookies are used to facilitate subsequent page authentication.
However, I have not released this part. If you want to confirm with others */
Echo logon successful

Echo, please!

}
?>


The third step is of course the registration page, and I will not talk much about it.
Step 4: confirm the identity after registration and enter the database.
Register. php:

The code is as follows:


Mysql_connect ("localhost", "user", "password")/* change the user name and password */
Or die ("unable to connect to the database, please try again ");
Mysql_select_db ("userinfo ")
Or die ("unable to select database, please try again ");
$ Query = "select id from usertbl where name = $ name \";
/* Search for user data with the same name from the database */
$ Result = mysql_query ($ query );
$ Numrows = mysql_num_rows ($ result );
If ($ numrows! = 0)/* if it is found, someone first registers the same name */
{Echo has been registered. please select another name !;}
Else
{$ Query = "insert into usertbl values (0, $ name, $ password ,\)";
/* Enter new user information if the same user information cannot be found */
Mysql_query ($ query );
Echo registration successful;
Echo, please log on !;}
?>

The next step is the use of cookies. I originally intended to use cookies so that every page can identify users. However, since other pages are not ready yet, I do not know what information is needed. So there is only one simple application. here we use the php reference:

The code is as follows:


If (! $ Usercookie)
{Header ("invalid user ");
}
?>
Welcome. php:
Require ("cookie. php");/* call cookie. php */
?>
Echo $ usercookie;
?>

This completes a very simple user authentication system. of course, you have to build a database if you want to use it. The following is the structure of my database table. the database name is userinfo.

The code is as follows:


Create table usertbl
(
ID int auto_increment primary key,
Name varchar (30 ),
Password varchar (20 ),
Lastlogin varchar (20)
);

Bytes. Of course, I had to spend a night's rest and wrote a very simple one...

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.