Home  >  Article  >  Backend Development  >  How to use cookies to implement login interface in php

How to use cookies to implement login interface in php

藏色散人
藏色散人Original
2021-12-30 09:26:352659browse

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.

How to use cookies to implement login interface in php

#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

Interface effect:

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=&#39;div_login&#39; id=&#39;login&#39;>
<form name=&#39;login_form&#39; method="post" action="/FirstPHPWeb/application/controllers/loginCl.php">
<!-- 在此处设置文本框默认值为 cookie 中的内容 -->
账 号:<input type="text" name="id" value="<?php echo @$_COOKIE[&#39;id&#39;]; ?>"/><br/>
密 码:<input type="password" name="psw" value="<?php echo @$_COOKIE[&#39;psw&#39;]; ?>"/><br/>
验证码:<input type="text" name="checkcode" /><br/>
<img  id=&#39;login_checkcode_img&#39; onclick="changeCode(&#39;login_checkcode_img&#39;)" src="../controllers/createCheckCode.php" ><a href="#" onclick="changeCode(&#39;login_checkcode_img&#39;)">看不清楚,换一张</a><br/>
<input type="checkbox" name="isKeepInfo" value="keep" checked/>在此电脑上保留用户名<br/>
<input class=&#39;btn&#39; type="image" src=&#39;/FirstPHPWeb/public/img/login.png&#39; alt=&#39;submit&#39; name=&#39;login&#39; value=&#39;登陆&#39; />
</form>
// 在表单的处理界面添加判断
if($isKeepInfo){
    setcookie("id", "$id", time()+3600*24*7,&#39;/&#39;);
    setcookie("psw", "$psw", time()+3600*24*7,&#39;/&#39;);
}

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!

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