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...