Home  >  Article  >  Backend Development  >  How to fix PHP cookie-based login in one move

How to fix PHP cookie-based login in one move

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-03-24 11:06:591833browse

Nowadays, most of us implement the function of automatic password login based on the cookie of the client, and PHP is no exception. Friends in need can refer to it.

How to fix PHP cookie-based login in one move

#php's solution to remembering passwords and automatically logging in is actually the operation of sessions and cookies.

First of all, we need an html template, in which form elements are written, name, password and login button are written, and the file is named login.php. Our login page is mainly performed on this page.

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<title>请登录</title>
</head>
<body>
<form method="post" action="login.php">
    姓名:<input type="text" name="username" />
    密码:<input type="password" name="password" />
    <input type="submit" name="submit" value="登录" />
</form>
</body>
</html>

It’s not enough that we have the html template, we need to pass the value out. Assuming that this website has only one member (or user), we need to match the name with the user’s name, such as No, you will not be able to log in. The code is as follows:

if (isset($_POST[&#39;submit&#39;])) {
    if(isset($_POST[&#39;username&#39;]) && isset($_POST[&#39;password&#39;]) && $_POST[&#39;username&#39;]===&#39;cmcc&#39; && $_POST[&#39;password&#39;]===&#39;123456&#39;){
        if (setcookie(&#39;username&#39;,$_POST[&#39;username&#39;],time()+3600)) {
            header(&#39;Location:lesson9.php?url=lesson8.php&&=登录成功,正在跳转中!&#39;);
        }else{
            echo &#39;cookie设置失败!&#39;;
        }
    }else{
        header(&#39;Location:lesson9.php?url=lesson8.php&&=用户名或密码填写错误,登录失败!&#39;);
    }
}
?>

After logging in and then visiting the website, it should not be displayed that you need to log in. For this time, we need to set up a if function to judge.

<?php
if (isset($_COOKIE[&#39;username&#39;])&&$_COOKIE[&#39;username&#39;]===&#39;cmcc&#39;) {
    exit("您已经登录请不要重复登录");
}

In order to accept this data and log in again due to login errors, we need to create a new file weclome.php and write in it:

<?php
   if (isset($_COOKIE[&#39;username&#39;])&&$_COOKIE[&#39;username&#39;]===&#39;cmcc&#39;) {
       echo "亲爱的{$_COOKIE[&#39;username&#39;]}您好,欢迎回来!";
   }else{
       echo "<a href=&#39;lesson7.php&#39;>请登录</a>";
   }
?>

At this time we have completed all operations on the login page. The complete code of login.php is given below for easy reference.


<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<title>请登录</title>
</head>
<body>
<form method="post" action="login.php">
    姓名:<input type="text" name="username" />
    密码:<input type="password" name="password" />
    <input type="submit" name="submit" value="登录" />
</form>
</body>
</html>

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to fix PHP cookie-based login in one move. 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