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 note that if you want to confirm the user identity in future pages. Step 1:
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 note that if you want to confirm the user identity in future pages.
The first step is to create a login page.
Step 2: Design the validation program after logon.
Login. php: mysql_connect ("localhost", "user", "password")/* connect to the database, modify the user name and password */or die ("unable to connect to the database, please try again "); mysql_select_db (" userinfo ") or die (" You cannot select a 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) {/* verifies whether users with the same data can be found, no, it is not registered */echo illegal 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 ");/* cookie is used here to facilitate subsequent page authentication. However, I have not released this part. If you are interested, please confirm that */echo is successfully logged on !; }?> |
Step 3: register the page.
Step 4: confirm the identity after registration and enter the database.
Register. php: 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 ("You cannot select a database, please try again"); $ query = "select id from usertbl where name = $ name \"; /* Search for user information 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 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 login !;} ?> |
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:
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.
create table usertbl ( ID int auto_increment primary key, Name varchar(30), Password varchar(20), Lastlogin varchar(20) ); |