search
HomeBackend DevelopmentPHP ProblemHow to save the username when logging in in php

How to save the username when logging in in php

Sep 24, 2019 pm 05:54 PM
phpkeepusernameLog in

How to save the username when logging in in php

What is a cookie

The server saves user information on the client, such as login name, password, etc. These data are like cookies Likewise, the amount of data is not large. The server can read it from the client when needed and save it in the client's browser cache directory.

① When the browser accesses cookie.php, the server will also send an http response with Set-Cookie:name=zxf;expire=Wed,21-Sep-2017 20:14 GMT. When the browser obtains After receiving this message, the cookie information will be saved to the local disk.

② If we do not have time (the third parameter) the cookie will not be saved to the client, and when the browser session ends, the cookie will expire.

③ The cookie saves string information

④ The client can save multiple keys=>val

⑤ During the saving process of the cookie, Chinese will be urlencoded Encoding

Cookies can have multiple key=>val, and different validity times can be set for different key values.

Instance:

Submit form page:

<?php
$user = isset($_COOKIE[&#39;username&#39;])?$_COOKIE[&#39;username&#39;]:&#39;&#39;;
?>
<form action="file.php" method="post">
用户名:<input type="text" name="username" value="<?php echo $user; ?>" /><br />
密码:<input type="password" name="pwd" /><br />
记住用户名:<input type="checkbox" name="rem" value="1"><br />
<input type="submit" name="sub" value="提交">
</form>

Form information receiving page, set at the same time Cookie:

<?php
$user = $_POST[&#39;username&#39;];
if($_POST[&#39;rem&#39;]){
 setcookie("username",$user,time()+3600*3600*24);
}else{
 setcookie("username",&#39;&#39;,time()-1);
}
echo "登录成功";
?>

Recommended tutorial: PHP video tutorial

The above is the detailed content of How to save the username when logging in 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function