In this section, we learn about Cookie through an example where a user does not need to re-enter the user name and password when he visits the website for the first time.
First introduce how to set cookies in php.
php provides a function that allows us to set cookies. This function is:
bool setcookie ( string $名字 [, string $值] [, int $过期时间 = 0] [, string $路径] [, string $域名] [, bool $安全 = false] [, bool $http只读 = false] );
Parameter Description
$Name Required. Specifies the name of the cookie.
$value Optional. Specifies the value of the cookie.
$Validity period Optional. Specifies the validity period of the cookie.
$Path Optional. Specifies the server path for cookies.
$Domain name Optional. Specifies the domain name for the cookie.
$Security Optional. Specifies whether cookies are transmitted over a secure HTTPS connection.
$httpAndu Optional. If true, then js cannot read and change the cookie, which increases security.
Generally speaking, we don’t actually use as many parameters as above. For this function, we usually use it like this: setcookie(cookie name, cookie value, cookie validity period);
Yes, Just 3. In this way, we can read the cookie through $_COOKIE['name'] on the server side.
The following is an example:
We name the file: cookie.php.
Let’s simulate the most common example we see on the Internet: enter the user name and password and successfully log in.
Let's build a database login, which has a table user and two fields: username and password.
<?php //第一次登陆的时候,通过用户输入的信息来确认用户 if ( ( $_POST['username'] != null ) && ( $_POST['password'] != null ) ) { $userName = $_POST['username']; $password = $_POST['password']; //从db获取用户信息 //PS:数据库连接信息改成自己的 分别为主机 数据库用户名 密码 $conn = mysqli_connect('localhost','root','root'); mysqli_select_db($conn,'test'); $sql = "select * from user where `username` = '$userName' "; $res = mysqli_query($conn,$sql); $row = mysqli_fetch_assoc($res); if ($row['password'] == $password) { //密码验证通过,设置cookies,把用户名和密码保存在客户端 setcookie('username',$userName,time()+60*60*24*30);//设置时效一个月,一个月后这个cookie失效 setcookie('password',$password,time()+60*60*24*30); //最后跳转到登录后的欢迎页面 header('Location: welcome.php' . "?username=$userName"); } } //再次访问的时候通过cookie来识别用户 if ( ($_COOKIE['username'] != null) && ($_COOKIE['password'] != null) ) { $userName = $_COOKIE['username']; $password = $_COOKIE['password']; //从db获取用户信息 //PS:数据库连接信息改成自己的 分别为主机 数据库用户名 密码 $conn = mysqli_connect('localhost','root','root','test'); $res = mysqli_query($conn,"select * from user where `username` = '$userName' "); $row = mysqli_fetch_assoc($res); if ($row['password'] == $password) { //验证通过后跳转到登录后的欢迎页面 header('Location: welcome.php' . "?username=$userName"); } } ?> <html> <head> </head> <body> <form action="" method="POST"> <div> 用户名:<input type="text" name="username" /> 密 码:<input type="text" name="password" /> <input type="submit" value="登录"> </div> </form> </body> </html>
The welcome.php code that jumps to
<?php $user = $_GET['username']; ?> <html> <head> </head> <body> welcome,<?php echo $user;?> </body> </html>
In this way, when I visit cookie.php for the first time, I need to enter the user name and password. After entering, I jump to welcome. .php. Then I closed the browser and opened cookie.php again. This time I was not asked to enter user information, but jumped directly to welcome.php, because the cookie information we saved before was automatically sent to the server by the browser, and the server did After processing, we jump directly to welcome.php. The server knows us! Knowing that I am the user who logged in before, we use cookie technology to maintain the state of the stateless HTTP protocol.
Do this again, I believe you can use cookies.
only! ! ! only! ! ! only! ! ! I have to say important things three times. We generally do not put usernames and passwords in cookies because it is not safe and can easily leak your own information. Please do not put important information in cookies. Ours is just an example of learning cookies.
Next Section