This article mainly introduces the use of PHP to realize automatic login storage mechanism (cookie, session, localStorage) within a week, which can achieve free login within a week. Interested friends can refer to it.
Cookie, session, and localStorage should be the three most troublesome for programmers. I will use the username and password of the simple login interface to explain.
1. Cookies are used to store user-related data. The storage location is local to the user:
The first is the login interface definition:
Everyone knows the above code, it is a form. Then it is the action server.php server processing,
$_SERVER["REMOTE_ADDR"], "loginTime" => time())); se($myArr); setcookies($_POST["username"], $_POST["password"]); setStroage($_POST["username"], $_POST["password"]); echo ""; } } else { echo ""; } //通过session保存到服务器 function se(array $arr) { session_start(); $_SESSION["username"] = $arr["username"]; $_SESSION["password"] = $arr["password"]; $_SESSION["loginIP"] = $arr["loginIP"]; $_SESSION["loginTime"] = $arr["loginTime"]; } //通过cookie保存到本地 function setCookies($username, $password) { setcookie("username", $username, time() + 120); setcookie("password", $password, time() + 120); } //echo ""; function setStroage($username, $password){ echo ""; }
is loaded with a data.php actually contains a piece of data,
$info=array("username"=>"admin","password"=>"admin");
This data is mainly used to check whether the entered user name and password are correct.
First of all, in the server, judge whether it is a post request. If not, you will be prompted to log in again and return to the login interface. After confirming that it is a post, you will then judge whether the information is entered correctly. If it is correct, set the cookie. The following code Let me explain one by one
$myArr = $_POST;//把post传过来的数据放在myarr;里面 $myArr = array_merge($myArr, array("loginIP" => $_SERVER["REMOTE_ADDR"], "loginTime" => time()));//这是穿的另外两个参数,一个是用户登录地址,另一个是cookie存活时间(会在寻获时间以为消失) se($myArr); setcookies($_POST["username"], $_POST["password"]);//这个是设置cookie,把传过来的两个值设置成cookie,会有一个getCookie进行接收 setStroage($_POST["username"], $_POST["password"]); echo ""; }
created with php. The above is the entire content of this article. I hope it will be helpful to everyone’s study. .
Related recommendations:
How to delete files in the directory using PHP unlink and rmdir
PHP Detailed explanation of commonly used methods for mysqli transaction operations
PHP uses asterisks to replace some characters in the user name, mobile phone and email address
The above is the detailed content of Use PHP to realize automatic login storage mechanism within a week. For more information, please follow other related articles on the PHP Chinese website!