Php remembers the implementation of the next automatic password login

Source: Internet
Author: User

Php remembers the implementation of the next automatic password login

When creating a website, you will often encounter the need to remember the password. The next time you log on automatically, you will not log on within one week, and you will not log on within one month. This function is generally implemented through cookies. This article will briefly explain how to use php to achieve this requirement. Of course, there are more than N methods to achieve this requirement.

The whole process is when you log on, if you choose to remember the password or do not log on within one week, after the user successfully logs on, store the data of a cookie for automatic logon to the user table of the database for verification at next automatic logon. If the verification succeeds, the system automatically logs on. Otherwise, you need to enter the user name and password to log on. The cookie value can be a random code.

The sample code is as follows:

$ Username = trim ($ _ POST ['username']); $ password = md5 (trim ($ _ POST ['Password']); $ ref_url = $ _ GET ['req _ url']; $ remember = $ _ POST ['remember']; // indicates whether to log on automatically. $ err_msg = ''; if ($ username = ''| $ password ='') {$ err_msg = "neither user name nor password can be blank ";} else {$ row = getUserInfo ($ username, $ password); if (empty ($ row) {$ err_msg = "Incorrect username and password ";} else {$ _ SESSION ['user _ info'] = $ row; if (! Empty ($ remember) {// if the user selects this option, the user name and password that have been encrypted are recorded in the logon status and placed in the cookie setcookie ("username", $ username, time () + 3600*24*365); setcookie ("password", $ password, time () + 3600*24*365);} if (strpos ($ ref_url, "login. php ") = false) {header (" location :". $ ref_url) ;}else {header ("location: main_user.php ");}}}

In addition, you must perform the following function checks before accessing each page of the website.

// Check whether the user has logged on to function checklogin () {if (empty ($ _ SESSION ['user _ info']) {// check whether the session is empty if (empty ($ _ COOKIE ['username']) | empty ($ _ COOKIE ['Password']) {// if the session is empty, and the user does not choose to record the login header ("location: login. php? Req_url = ". $ _ SERVER ['request _ URI ']); // go to the login page, record the REQUEST url, and jump to the past after logon. the user experience is good .} Else {// The user selects to remember the logon Status $ user = getUserInfo ($ _ COOKIE ['username'], $ _ COOKIE ['Password']); // retrieve the user's personal data if (empty ($ user) {// if the user name and password are incorrect, the information is not obtained. Go to the login page header ("location: login. php? Req_url = ". $ _ SERVER ['request _ URI ']);} else {$ _ SESSION ['user _ info'] = $ user; // the user name and password are correct, put the user's personal data into the session }}}}
Articles you may be interested in
  • How to Set automatic phpmyadmin login and cancel Automatic Login
  • Php extracts the birthday date from the ID card number and verifies whether it is a minor.
  • PhpMyAdmin does not allow empty password login Solution
  • How to Set automatic login for phpmyadmin
  • Php implements batch file compression, packaging, and downloading
  • How does PHP determine whether a GIF image is a dynamic image (animation)
  • Php obtains the start and end timestamps of today, yesterday, last week, and this month.
  • One of phper's most real work and life experiences is dedicated to PHPER fans.

Related Article

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.