PHP CookieLOGIN

PHP Cookie

Cookies are often used to identify users.

What are Cookies?

Cookie is generated by the server and sent to User-Agent (usually a browser). The browser will save the key/value of the Cookie to a text file in a certain directory. Next time This cookie is sent to the server when the same website is requested (provided that the browser is set to enable cookies). Cookie names and values ​​can be defined by the server-side developer, so that the server can know whether the user is a legitimate user and whether he needs to log in again, etc. The server can set or read the information contained in Cookies to maintain the status of the user's session with the server. .

Notes:

1. Different computers cannot share Cookies

2. Different browsers on the same computer cannot share Cookies

3. Cookies cannot be shared under the same browser and different domain names

4. Even under the same browser and the same domain name, cookies with different paths cannot be shared

How to create cookies?

Create a cookie through the setcookie() function, returning TRUE if successful, otherwise returning FALSE.

Note: The setcookie() function must be located before the <html> tag.

Syntax

setcookie(name, value, expire, path, domain);

Parameter description:

Parameters Description

name Cookie name

value Optional, cookie value

expire Optional, expiration time, timestamp format

path Optional, server-side valid path, / indicates that the entire domain name is valid, the default is the path of the page when the cookie is currently set

domain Optional, Valid domain name of this cookie

Example 1

In the following example, we will create a cookie named "user" and assign it the value "php". We also specify that this cookie expires after one hour:

<?php
   setcookie("user", "php", time()+3600);
?>

                                                                       . When using a cookie, the cookie value will be automatically URL-encoded and automatically decoded when retrieved.
Example 2

You can also set the cookie expiration time in another way. This may be simpler than using seconds.

<?php
  $expire=time()+60*60*24*30;
  setcookie("user", "php", $expire);
?>

                                                                                                                                                                                                      . * 30 days).

How to retrieve the value of Cookie?


PHP’s $_COOKIE variable is used to retrieve the value of the cookie.

In the following example, we retrieve the value of the cookie named "user" and display it on the page:

<?php

// Output cookie value
echo $_COOKIE["user"];

// View all cookies
print_r($ _COOKIE);

?>  

In the following example, we use the isset() function to confirm whether the cookie has been set:

<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<?php
if (isset($_COOKIE["user"]))
   echo "欢迎 " . $_COOKIE["user"] . "!<br>";
else
   echo "普通访客!<br>";
?>
</body>
</html>

How to delete cookies?

You can delete a cookie by setting the cookie expiration time to a previous time point:

Deleted example:

<?php
// 设置 cookie 过期时间为过去 1 小时
  setcookie("user", "", time()-3600);
?>

Tips:

Due to protocol restrictions, no content can be output to the browser before the cookie is set.

The cookie will not take effect on the current page set. To access the set cookie, another page must be visited before it expires

Since cookie information is stored in the user's computer, it is possible to forge cookies and cause cookie fraud. Generally, the cookie value can be encrypted to prevent fraud. When reading cookies, just decrypt the cookies

What should I do if the browser does not support cookies?

If your application needs to deal with browsers that do not support cookies, then you will have to use other methods to pass information between pages in your application. One way is to pass data through a form (forms and user input are covered in the previous chapters of this tutorial).

The following form submits user input to "welcome.php" when the user clicks the "Submit" button:

<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<form action="welcome.php" method="post">
名字: <input type="text" name="name">
年龄: <input type="text" name="age">
<input type="submit">
</form>
</body>
</html>

Retrieve the value in the "welcome.php" file, as follows Shown:

<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
欢迎 <?php echo $_POST["name"]; ?>.<br>
你 <?php echo $_POST["age"]; ?> 岁了。
</body>
</html>

Cookie restriction issue

Many browsers have limits on the number of cookies. Most browsers stipulate the number of cookies that a website can set. It cannot exceed 50, and some browsers even limit it to 30

Browsers also have restrictions on the size of Cookies, which generally cannot exceed 4K in size

Cookie Security Issues

If you do not shut down your computer after surfing the Internet in an Internet cafe, other people who use your computer can view the history of all the websites you visit and the cookie content saved by the website. If the important data (user name , password, card number, mobile phone number, ID number...) saving in cookies is a very dangerous behavior.

Therefore, important data cannot be stored in Cookies. If it must be saved, it must rely on the server


Next Section
<?php setcookie("user", "php", time()+3600); ?>
submitReset Code
ChapterCourseware