Home > Backend Development > PHP Problem > How to set the scope of cookies in php

How to set the scope of cookies in php

王林
Release: 2023-03-05 13:12:01
Original
2493 people have browsed it

How to set the scope of cookie in php: You can set it through the setcookie() function. The setcookie() function can send an HTTP cookie to the client. This function has a parameter domain, which is used to specify the domain name and scope of the cookie.

How to set the scope of cookies in php

# The setcookie() function sends an HTTP cookie to the client. If successful, the function returns TRUE. Returns FALSE on failure.

(Recommended tutorial: php video tutorial)

Grammar:

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

Parameter introduction:

name Required. Specifies the name of the cookie.

value Required. Specifies the cookie value.

expire Optional. Specifies the cookie expiration time.

time() 3600*24*30 will set the cookie expiration time to 30 days. If this parameter is not set, the cookie will automatically expire after the session ends (that is, when the browser is closed).

path Optional. Specifies the server path for cookies.

(Related recommendations: php training)

If the path is set to "/", then the cookie will be valid within the entire domain name. If the path is set to "/test/ ", then the cookie will be valid in the test directory and all its subdirectories. The default path value is the current directory where the cookie is located.

domain Optional. Specifies the domain name for the cookie.

In order for the cookie to be valid in all subdomains of example.com, you need to set the domain name of the cookie to ".example.com". When you set the domain name of the cookie to www.example.com, the cookie is only valid in the www subdomain.

secure Optional. Specifies whether cookies need to be transmitted over a secure HTTPS connection. Set to TRUE if the cookie needs to be transmitted over a secure HTTPS connection. Default is FALSE.

The above is the detailed content of How to set the scope of 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