Home  >  Article  >  Backend Development  >  How to implement the 7-day login-free function in php

How to implement the 7-day login-free function in php

autoload
autoloadOriginal
2021-03-17 11:47:103522browse

The so-called 7-day login-free means that when the user chooses to remember the user information when logging in, the background will automatically set a valid time of 7 days, and then the user will log out abnormally within 7 days (not clearing cookieInformation), you do not need to log in to access the backend (the backend helps you log in). No need to log in is achieved by using cookie to save data to the browser and using this cookie.

Part of the code of the login page

//表单内容
<form name="form1" method="post" action="index.php">
   <label>用户名</label>
   <input name="u_username" type="text"     value=""/>
   <label>密码</label>
   <input name="u_password" type="password"  value=""/>

   <label><input type="checkbox" name="rememberMe"/> 7天内自动登录</label>
      <input  type="submit"   name="button" value="登录" />
</form>

During the user login verification, it is determined whether the user has chosen to avoid login. If so, after the user successfully logs in, the user's ID information is stored in the browser. Previous: Save time 7 days

//index.php
//用户信息登录验证添加下方内容
if($user){   //$user为从数据库中获取的用户信息数组
     //判断用户是否存在
     $_SESSION[&#39;user&#39;]=$user;
    //此处跳转至登陆成功页面,利用header()
    }
//验证完成后跳转至登陆成功页面,利用header()
//将用户登录后的信息保存到session中
    @session_start();   
    $_SESSION[&#39;user&#39;]=$user;
//7天免登录:登录成功后
if(isset($_POST[&#39;rememberMe&#39;])){
    //7天免登录
    setcookie(&#39;user_id&#39;,$user[&#39;id&#39;],time() + 7 * 24 * 3600);
}

Recommended: php video tutorial

The above is the detailed content of How to implement the 7-day login-free function in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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