Home > Article > Backend Development > How to implement the 7-day login-free function in php
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 cookie
Information), 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['user']=$user; //此处跳转至登陆成功页面,利用header() } //验证完成后跳转至登陆成功页面,利用header()
//将用户登录后的信息保存到session中 @session_start(); $_SESSION['user']=$user; //7天免登录:登录成功后 if(isset($_POST['rememberMe'])){ //7天免登录 setcookie('user_id',$user['id'],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!