Home > Article > Daily Programming > PHP cookie implements method of recording user login information (pictures, text + video)
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>
2. PHP file code example for connecting to the database:
<?php $dbName = 'demo'; $host = '127.0.0.1'; $user = 'root'; $password = 'root'; $dsn = "mysql:host=$host;dbname=$dbName"; $pdo = new PDO($dsn, $user, $password); function sql($table, $field = '*', $where = '') { global $pdo; $sql = 'select' . ' ' . $field . ' ' . 'from' . ' ' . $table . ' where ' . $where; $data = $pdo->query($sql)->fetch(); return $data; }
3. Code example of user login information
<?php include "db.php"; @$name = $_POST['username']; @$pas = $_POST['password']; $row = sql('user', '*', "username = '$name'"); if (!$row) { echo "<script> alert('用户名不存在!请检查用户名~~') </script>"; header("Refresh:1;url=login.html"); return; } if ($row['password'] == $pas) { setcookie("username", "$name", time() + 36000); echo "<script> alert('登录成功!正在跳转...') </script>"; echo "<a href='index.php'>如果跳转失败请点击跳转~~</a>"; header("Refresh:1;url=index.php"); return; } echo "密码错误!请检查密码"; header("Refresh:1;url=login.html");
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['username']; echo $_COOKIE['username']; if ($name) { echo "<script> alert(\"尊敬的$name ,欢迎回来!!\"); </script>"; }else{ echo "<script> alert('您还尚未登录!请返回登录~~') </script>"; echo "<a href='login.html'>如果跳转失败请点击跳转~~</a>"; header("Refresh:1;url=login.html"); }
Access through the browser, the effect is as shown below:
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['username'];
The result is as shown below:
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!