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.
#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<?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>
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('denglu');//里边的字符可以自己设置 if (!$type) { return str_replace('=', '', base64_encode($value ^ $key)); } $value = base64_decode($value); return $value ^ $key; }
<?php require_once('function.php'); session_start();//开启session header("Content-Type: text/html; charset=UTF-8"); /* * 添加一些用户名和密码的判断 * 。。。。。。。。。 */ if(isset($_POST['auto'])){//如果勾选了自动登陆一周 if(!isset($_COOKIE['auto'])){ setcookie('auto',encryption($_POST['name']),7*24*3600+time());//设置cookie过期时间为一周后 } $_SESSION['name'] = $_POST['name']; header('location:success.php'); }else{//没有勾选自动登陆一周 $_SESSION['name'] = $_POST['name']; header('location:success.php'); }
<?php session_start();//开启session require_once('function.php'); header("Content-Type: text/html; charset=UTF-8"); if(isset($_SESSION['name'])){ echo '成功登陆session:用户名为'.$_SESSION['name']; } elseif(isset($_COOKIE['auto'])){ echo '成功登陆cookie:用户名为'.encryption($_COOKIE['auto'],1); }else{//什么都没有的跳转到登录表单 header('location:show.php'); } ?>
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!