How to implement automatic login in php: 1. Store the automatic login cookie when logging in for the first time as a verification for the user's next automatic login; 2. During the next login initialization, detect whether there is $ in the computer _COOKIE['autoLogin'] data, if available, log in automatically.
The operating environment of this article: windows10 system, php 7, thinkpad t480 computer.
First of all, when the user logs in for the first time, if you check No need to log in for a week, after the login operation is completed, an automatic login cookie will be stored. This cookie can be "$value is The encrypted data is a string composed of the user's "id" plus a "random code". This random code is stored in the user table in the database for verification the next time the user automatically logs in. It can also be more high-end data, and everyone can use it to their heart's content.
setcookie('autoLogin',$value,7*24*3600 time());
Then during initialization the next time you log in , first check whether there is $_COOKIE['autoLogin'] data in the user's computer. If there is, decrypt the data and analyze it to see if it is the real cookie stored in the previous login (to prevent forgery)
Recommended learning: php training
The above is the detailed content of How to implement automatic login in php. For more information, please follow other related articles on the PHP Chinese website!