Home > Article > Backend Development > How to use cookies to implement login interface in php
How to use cookies to implement the login interface in php: 1. Set the login form; 2. Set the default value of the text box to the content in the cookie; 3. Add an if judgment to the form processing interface.
#The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.
How to use cookies to implement login interface in php? The PHP login interface uses cookies to remember account passwords
cookie principle:
cookie in The server creates it and returns it to the client browser for local storage. The cookie storage time is determined by setting the life cycle.
Implementation process:
<!-- 登录表单 --> <div class='div_login' id='login'> <form name='login_form' method="post" action="/FirstPHPWeb/application/controllers/loginCl.php"> <!-- 在此处设置文本框默认值为 cookie 中的内容 --> 账 号:<input type="text" name="id" value="<?php echo @$_COOKIE['id']; ?>"/><br/> 密 码:<input type="password" name="psw" value="<?php echo @$_COOKIE['psw']; ?>"/><br/> 验证码:<input type="text" name="checkcode" /><br/> <img id='login_checkcode_img' onclick="changeCode('login_checkcode_img')" src="../controllers/createCheckCode.php" ><a href="#" onclick="changeCode('login_checkcode_img')">看不清楚,换一张</a><br/> <input type="checkbox" name="isKeepInfo" value="keep" checked/>在此电脑上保留用户名<br/> <input class='btn' type="image" src='/FirstPHPWeb/public/img/login.png' alt='submit' name='login' value='登陆' /> </form>
// 在表单的处理界面添加判断 if($isKeepInfo){ setcookie("id", "$id", time()+3600*24*7,'/'); setcookie("psw", "$psw", time()+3600*24*7,'/'); }
Details:
It is not complicated to implement this function, but In actual application, a small detail will also be ignored:
Let’s first learn about the setcookie function. The following syntax is given in the development document:
setcookie(name, value, expire, path, domain);
name, value: store cookie data in the form of key-value pairs;
expire: expiration time, timestamp format (optional). If not set, it defaults to a session-level cookie, and the cookie will expire after the browser is closed.
path: Valid path on the server side (optional). The default is the path of the page when the cookie is currently set;
domain: the domain name that the cookie is valid for (optional). Only the specified domain name can get the cookie. By default, all domain names can get it. [Recommended learning: PHP video tutorial]
When the form file and form processing file are not in the same directory, be sure to remember to set the path attribute, otherwise it will be taken in the form interface. Cookie username and password do not exist.
The above is the detailed content of How to use cookies to implement login interface in php. For more information, please follow other related articles on the PHP Chinese website!