How to achieve automatic login in seven days in php

藏色散人
Release: 2023-03-14 13:42:01
Original
2810 people have browsed it

php method to achieve seven-day automatic login: 1. Create a new show.php file to implement the login page; 2. Create a function.php file to implement encrypted cookies; 3. Set the user name and password judgment; 4. Open the session and You can automatically log in within a week.

How to achieve automatic login in seven days in php

#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.

php How to realize automatic login within seven days?

PHP enables automatic login within a week:

##1 .Create four new files


How to achieve automatic login in seven days in php

show.php

<?php
?>
<html >

<head>
<meta charset="utf-8">
<title>测试</title>
</head>
<body>
<form action="login.php" method="post">
用户名<input type="text" name="name"/>
密码<input type="text" name="psd"/>
自动登录<input type="radio" name="auto">
    <input type="submit" value="提交"/>

</form>
</body>
</html>
Copy after login

function.php//Encrypted cookie

Here I used the XOR encryption method, because after two XOR times, it will change back to the original value

<?php
function encryption ($value, $type=0) {
$key = md5(&#39;denglu&#39;);//里边的字符可以自己设置
if (!$type) {
return str_replace(&#39;=&#39;, &#39;&#39;, base64_encode($value ^ $key));
}

$value = base64_decode($value);
return $value ^ $key;
}
Copy after login

login.php

<?php
require_once(&#39;function.php&#39;);
session_start();//开启session
header("Content-Type: text/html; charset=UTF-8");

/*
 * 添加一些用户名和密码的判断
 * 。。。。。。。。。
 */

if(isset($_POST[&#39;auto&#39;])){//如果勾选了自动登陆一周
    if(!isset($_COOKIE[&#39;auto&#39;])){
        setcookie(&#39;auto&#39;,encryption($_POST[&#39;name&#39;]),7*24*3600+time());//设置cookie过期时间为一周后
    }
    $_SESSION[&#39;name&#39;] = $_POST[&#39;name&#39;];
    header(&#39;location:success.php&#39;);
}else{//没有勾选自动登陆一周
    $_SESSION[&#39;name&#39;] = $_POST[&#39;name&#39;];
    header(&#39;location:success.php&#39;);
}
Copy after login

success.php

<?php
session_start();//开启session
require_once(&#39;function.php&#39;);
header("Content-Type: text/html; charset=UTF-8");
if(isset($_SESSION[&#39;name&#39;])){
echo &#39;成功登陆session:用户名为&#39;.$_SESSION[&#39;name&#39;];
}
elseif(isset($_COOKIE[&#39;auto&#39;])){
echo &#39;成功登陆cookie:用户名为&#39;.encryption($_COOKIE[&#39;auto&#39;],1);
}else{//什么都没有的跳转到登录表单
header(&#39;location:show.php&#39;);
}

?>
Copy after login

Run it


How to achieve automatic login in seven days in php

Check the cookie

How to achieve automatic login in seven days in php

Close the browser and reopen it. At this time, the cookie is read.


How to achieve automatic login in seven days in php

This is just to provide an idea. , the specific login verification has yet to be considered by the readers themselves.

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to achieve automatic login in seven days in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template