How to use PHP to write MySQL database user authentication system _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
You can use PHP to write a MySQL database user authentication system. 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 off, but I wrote a simple one. I was entrusted by my friends over the past two days and asked him to write a user authentication system using 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, the user is prompted to register first. It is easy to use PHP to accomplish this, but it should be noted that if you want to confirm the user identity in the future pages, I can only come up with a method to use cookies when using 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 :?
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. Hope you can correct your interests */
Echo logon successful
;
Echo, please!
;
}

Bytes. Of course, I had to spend one night's rest, but it was easy to write...

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.