PHP cookie implements method of recording user login information (pictures, text + video)

藏色散人
Release: 2018-10-18 15:55:52
Original
7025 people have browsed it

This article mainly introduces to you How PHP uses cookies to record user login information.

Cookies are data stored on the user's local terminal. So in the previous article [How to record and delete variables in cookies in PHP? ], we have introduced in detail what cookies are and how to use them. Friends in need can refer to it first.

In addition to being needed in our daily project development, the use of cookies is also a common test point in our PHP interview process. It is very necessary for everyone to learn and master it.

Below we will use specific code examples to introduce how cookies can be used to record user login information.

The following method is basically the same as the idea and usage example of recording user login information through session introduced before. Friends who need it can also choose to learn more [How does PHP use session to record user login information? 】This article.

1. Login interface code example:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>登录</title>
    <style type="text/css">
        body {
            background: url(images/bg.png);
        }
 
        .clear {
            clear: both;
        }
 
        .login {
            width: 370px;
            margin: 100px auto 0px;
            text-align: center;
        }
 
        input[type="text"] {
            width: 360px;
            height: 50px;
            border: none;
            background: #fff;
            border-radius: 10px;
            margin: 5px auto;
            padding-left: 10px;
            color: #745A74;
            font-size: 15px;
        }
 
        input[type="checkbox"] {
            float: left;
            margin: 5px 0px 0px;
        }
 
        span {
            float: left;
        }
 
        .botton {
            width: 130px;
            height: 40px;
            background: #745A74;
            border-radius: 10px;
            text-align: center;
            color: #fff;
            margin-top: 30px;
            line-height: 40px;
        }
    </style>
</head>
<body>
<div class="login">
    <form action="check.php" method="post">
        <img  src="images/header.png" alt="PHP cookie implements method of recording user login information (pictures, text + video)" ><br>
        <input type="text" name="username" placeholder="请输入用户名!" value=""><br>
        <input type="text" name="password" placeholder="请输入密码!" value=""><br>
        <input type="submit" class="botton" value="login">
    </form>
    <div class="clear"></div>
</div>
</body>
</html>
Copy after login

2. PHP file code example for connecting to the database:

<?php
$dbName = &#39;demo&#39;;
$host = &#39;127.0.0.1&#39;;
$user = &#39;root&#39;;
$password = &#39;root&#39;;
 
$dsn = "mysql:host=$host;dbname=$dbName";
$pdo = new PDO($dsn, $user, $password);
 
function sql($table, $field = &#39;*&#39;, $where = &#39;&#39;)
{
    global $pdo;
    $sql = &#39;select&#39; . &#39; &#39; . $field . &#39; &#39; . &#39;from&#39; . &#39; &#39; . $table . &#39; where &#39; . $where;
    $data = $pdo->query($sql)->fetch();
    return $data;
}
Copy after login

3. Code example of user login information

<?php
include "db.php";
@$name = $_POST[&#39;username&#39;];
@$pas = $_POST[&#39;password&#39;];

$row = sql(&#39;user&#39;, &#39;*&#39;, "username = &#39;$name&#39;");
if (!$row) {
    echo "<script>
    alert(&#39;用户名不存在!请检查用户名~~&#39;)
</script>";
    header("Refresh:1;url=login.html");
    return;
}

if ($row[&#39;password&#39;] == $pas) {
    setcookie("username", "$name", time() + 36000);
    echo "<script>
    alert(&#39;登录成功!正在跳转...&#39;)
</script>";
    echo "<a href=&#39;index.php&#39;>如果跳转失败请点击跳转~~</a>";
    header("Refresh:1;url=index.php");
    return;
}
echo "密码错误!请检查密码";
header("Refresh:1;url=login.html");
Copy after login

Here we store the user’s successful login information in a cookie, and the retention time is 10 hours.

4. Home page code example:

<?php
echo "<h1>这里是主页</h1>";
@$name = $_COOKIE[&#39;username&#39;];
echo $_COOKIE[&#39;username&#39;];
if ($name) {
    echo "<script>
     alert(\"尊敬的$name ,欢迎回来!!\");
</script>";
}else{
    echo "<script>
    alert(&#39;您还尚未登录!请返回登录~~&#39;)
</script>";
    echo "<a href=&#39;login.html&#39;>如果跳转失败请点击跳转~~</a>";
    header("Refresh:1;url=login.html");
}
Copy after login

Access through the browser, the effect is as shown below:

PHP cookie implements method of recording user login information (pictures, text + video)

Of course we can check whether the user information is successfully stored in the cookie. In index.php, directly write the following code:

<?php
echo $_COOKIE[&#39;username&#39;];
Copy after login

The result is as shown below:

PHP cookie implements method of recording user login information (pictures, text + video)

As shown in the figure, we output the user name information stored in the cookie, which means that the user information has been stored in the cookie.

This article is an introduction to how PHP uses cookies to record user login information. I hope it will be helpful to friends in need!

If you want to know more about PHP, you can follow the PHP Chinese website PHP Video Tutorial, everyone is welcome to refer to and learn!

The above is the detailed content of PHP cookie implements method of recording user login information (pictures, text + video). 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!