Website Construction: user login authentication

Source: Internet
Author: User

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.


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.