This article mainly introduces the implementation method of using cookie to save the user name of user login in PHP. In the form of an example, it completely analyzes the techniques of cookie to save the user login name. Friends who need it can Refer to the following
The example of this article describes how PHP uses cookies to save the user name of the user's login. Share it with everyone for your reference. The specific implementation method is as follows:
User login file: login.php
The code is as follows:
<html> <head> <title>用户登录</title> </head> <body> <?php function getCookieUsername(){ if(empty($_COOKIE['username'])){ return ""; }else{ return $_COOKIE['username']; } } ?> <form action="admin.php" method="post"> 用户名:<input type="text" name="username" value="<?php echo getCookieUsername(); ?>"><br /> 密码:<input type="password" name="pwd"><br /> 是否保存用户名:<input type="checkbox" name="yes"><br /> <input type="submit" name="sub" value="登录"> </form> </body> </html>
Backend file: admin.php
The code is as follows:
<?php if(!empty($_POST['sub']) && $_POST['username']=="admin"){ echo "欢迎".$_POST['username']." 登录成功"; if(!empty($_POST['yes'])){ setCookie("username",$_POST['username'],time()+3600*24*30); }else{ setCookie("username","",time()-10); } }else{ echo "你的账号错误,请重新输入<br />"; } echo "<a href='login.php'>返回登录页面</a>"; ?>
The above is the detailed content of PHP uses cookies to save user login username example code. For more information, please follow other related articles on the PHP Chinese website!