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['username'])?$_COOKIE['username']:''; ?> <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['username']; if($_POST['rem']){ setcookie("username",$user,time()+3600*3600*24); }else{ setcookie("username",'',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!