I was entrusted by my friends these two days and asked me 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.
I was entrusted by my friends these two days and asked me 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 very simple: first, you need to fill in the user name and password on the page, of course, users not registered need to register first. Then, you can call the database to search for users. If yes, the user is prompted to register first. It is very simple to use PHP to complete all this, but it is important to note that if you want to confirm the user identity in the future page, I can only come up with a method to apply PHP3. To apply the session, you have to wait for the official version of PHP4 to announce it!
The first step is to create a login page, which is not mentioned here. I just made a very simple one. you can make it beautiful.
Step 2: Design the validation program after logon.
?? Login. php :? Mysql_connect ('localhost', 'user', 'password')/* connect to the database, correct the user name and password */or die ('unable to connect to the database, please try again '); mysql_select_db ('userinfo') or die ('database cannot be selected, 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 user materials from the database */'; $ result = mysql_query ($ query); $ numrows = mysql_num_rows ($ result); if ($ numrows = 0) {/* verifies whether users with identical materials can be found, if not, the user is not registered */The echo is invalid. Register the echo first. retry the echo operation. ;} 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');/* cookie is applied here to facilitate subsequent page authentication. However, I have not released this part. Hope to see friends with hobbies correct me */echo login successful; echo please come in !; }?>
The third step is of course the registration page.
Step 4: confirm the identity after registration and import it to the database.
?? Register. php: mysql_connect ('localhost', 'user', 'password')/* correct the username and password */or die ('unable to connect to the database, please try again '); mysql_select_db ('userinfo') or die ('database cannot be selected, please try again '); $ query = 'SELECT id from usertbl where name = $ name \'; /* Search for user materials with the same name from the database */$ result = mysql_query ($ query); $ numrows = mysql_num_rows ($ result); if ($ numrows! = 0)/* found, of course, someone first registered the same name */{echo already registered this name, please reselect the name !;} Else {$ query = 'Insert into usertbl values (0, $ name, $ password ,\)'; /* enter new user materials if the same user cannot be found */mysql_query ($ query); echo registration successful; echo please log on !;} ?>
The next step is the cookie application. I originally intended to apply cookies so that each page can recognize the user's identity. However, since other pages are not ready yet, I do not know what materials are needed. So there is only one simple application. here we use the php reference:
If (! $ Usercookie) {header ('invalid user') ;}?> Welcome. php: require ('cookie. php');/* call cookie. php */?> Echo $ usercookie;?>
Here we have completed a simple user authentication system. of course, if you want to apply it, you have to build a database. The following is the structure of my database table. the database name is userinfo.
create table usertbl ( ID int auto_increment primary key, Name varchar(30), Password varchar(20), Lastlogin varchar(20) );