PHP uses cookies to save user login username example code

怪我咯
Release: 2023-03-13 10:52:01
Original
1569 people have browsed it

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[&#39;username&#39;])){
  return "";
 }else{
  return $_COOKIE[&#39;username&#39;];
 }
}
?>
<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>
Copy after login

Backend file: admin.php

The code is as follows:

<?php
if(!empty($_POST[&#39;sub&#39;]) && $_POST[&#39;username&#39;]=="admin"){
 echo "欢迎".$_POST[&#39;username&#39;]." 登录成功";
 if(!empty($_POST[&#39;yes&#39;])){
  setCookie("username",$_POST[&#39;username&#39;],time()+3600*24*30);
 }else{
  setCookie("username","",time()-10);
 }
}else{
 echo "你的账号错误,请重新输入<br />";
}
echo "<a href=&#39;login.php&#39;>返回登录页面</a>";
?>
Copy after login

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!

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