php session control Cookies in PHP

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.

   
用户名: 密 码:

The welcome.php code that jumps to

   
welcome,

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.

Continuing Learning
||
用户名: 密 码:
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!