Home > Backend Development > PHP Problem > How to save the username when logging in in php

How to save the username when logging in in php

王林
Release: 2023-02-24 19:02:01
Original
3756 people have browsed it

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>
Copy after login

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 "登录成功";
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template