Home > Backend Development > PHP Tutorial > How to create cookies in php

How to create cookies in php

伊谢尔伦
Release: 2023-03-07 16:02:01
Original
5109 people have browsed it

Cookie creation is very simple. As long as the user's browser supports the Cookie function, you can use PHP's built-in function to create a new Cookie. Create a cookie in PHP through the setcookie() function. What you must understand before creating cookies is that cookies are part of HTTP headers, and headers must be sent before other content on the page, so it must be output first. So even if it is a space or a blank line, do not output it before calling the setcookie() function. If an HTML tag, echo statement, or even a blank line is output before the setcookie() function, a program error will occur.

The syntax is in the following format:

 setcookie(name,value,expire,path,domain,secure)
Copy after login

The setcookie() function defines a Cookie that is sent together with the rest of the HTTP header. All its parameters are attributes corresponding to the HTTP header Cookie data. . Although the setcookie() function seems to have a lot of imported parameters, except for the parameter name, other parameters are not necessary, and the only three parameters we often use are $name, $value and $expire.

The parameter description of the setcookie() function is as follows:

1) name

Cookie variable name

can be called through $_COOKIE["cookiename"] The variable name is cookiename cookie

2)value

The value of the Cookie variable. This value is saved on the client and cannot be used to save sensitive data

It can be passed through $_COOKIE[ "values"] Get the value named values

3) expire

Cookie expiration time, expire is a standard UNIX time stamp, you can use the time() function Or mktime() function to obtain, the unit is seconds

If the cookie expiration time is not set, the cookie will be valid forever unless it is deleted manually

4) path

Cookie's valid path on the server side

If this parameter is set to "/", it is valid within the entire domain. If it is set to "/11", it is valid in /11 under the domain. Valid within directories and subdirectories. The default is the current directory

5) domain

Cookie valid domain name

If you want the cookie to be valid in all subdomains under the abc.com domain name, it should be set to abc .com

6) secure

Indicates whether the cookie passes secure HTTPS, the value is 0 or 1

If the value is 1, the cookie can only be valid on HTTPS connections ; If the value is the default value of 0, the cookie is valid on both HTTP and HTTPS connections.

If there is only one parameter called $name, the original cookie option with this name will be deleted. You can also use an empty string to omit this parameter. The parameters $expire and $secure are an integer and can be omitted using 0 instead of using the empty string. But the parameter $expire is a regular UNIX time integer, returned by the time() or mktime() function. The parameter $secure indicates that this cookie will only be sent over a secure HTTPS connection.

Use all parameter settings of the setcookie() function. The example code is displayed as follows

<?php
  setcookie("username","sky",time()+60*60,"/test",".php.cn",1);
?>
Copy after login

Description: The above example indicates the establishment of a Cookie with the identification name "username", and its content value is characters The string "sky" is stored on the client for 1 hour. The parameter "/test" indicates that the cookie is only valid in this subdirectory or subdirectories of the old weapon. The parameter ".php.cn" enables the cookie to be valid in all subdomains under the domain name php.cn. Although the "." is not required, adding it will make it compatible with more browsers. When the last parameter is set to 1, the cookie can only be set on a secure connection.

The value given using setcookie() can only be a number or a string, not other complex structures.

The above is the detailed content of How to create cookies in php. 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