Build a website: User login authentication. I have already introduced several php functional skills. from now on, I will make full use of these skills to build a powerful website, I will introduce some advanced and basic skills in the future. I have already introduced several php functional skills. from now on, I will make full use of these skills to build a powerful website, in the future, we will introduce some advanced and basic skills on how to apply them to websites.
The user login authentication function is essential for building a good website. here I will only introduce the ideas and practices and will not add Steps to list them step by step.
First, you need a database that can be used to record user data. its content should include at least the name and password. Naturally, you can add corresponding fields in the database as needed.
To make the database have information, you must have a registration program. by registering a user, you can store the information in the database. the registration procedure is very simple, just issuing instructions for storing the information in the database, I will not describe it here. it is suggested that, for the sake of user data security, it is best to encrypt the user's password, and the database should not have the same name, otherwise it will be messy.
The following describes the implementation of user login and logout. the user login function can be completed using session and cookie. here I will introduce how to use cookie to complete this function.
In fact, the entire process of user login is easy to understand. The program compares the name and password entered by the user with the data stored in the database, and the database passes the information of this user, if no, the user is rejected.
Let's take a look at the workflow of the login. php login program:
After you submit the information to login, php, and login. php, the process is as follows:
$ Passwd = md5 ($ passwd );
$ Result = mysql_query ("select * from user where name = '$ name' and passwd =' $ passwd '");
The reason for the password to be processed by md5 is that the database stores the md5-encrypted password and determines whether the user exists. if the user does not exist or the password is incorrect, an alarm is given to the user, if the user's data exists, you can set the user's data to the cookie value as follows:
Setcookie ("cookiename", $ name, time () + 18000 ,"","/");
Setcookie ("cookiepasswd", $ passwd, time () + 18000 ,"","/");
If you are worried about security issues caused by user forgetting to log out, remove the time settings:
Setcookie ("cookiename", $ name ,"","/");
Setcookie ("cookiepasswd", $ passwd ,"","/");
In this way, when the user closes the browser, the cookie setting becomes invalid. that is to say, the user must log on again next time! Although the security function of disabling the browser invalidates the cookie, a user logout Function is required to ensure security. The logout function is to invalidate the cookie function that records user data, to complete this function, you only need to set the time to-1 and leave the cookie variable blank:
Logout. php:
Setcookie ('cookiename', "", time ()-1 ,'/',"");
Setcookie ('cookiepasswd', "", time ()-1 ,'/',"");
$ Cookiename = "";
$ Cookiepasswd = "";
This completes a complete user authentication function.
Php's functional skills have been fully utilized to build a powerful website from now on. in the future, we will introduce some advanced and basic skills...