Use the PHP function 'setcookie' to set cookies

WBOY
Release: 2023-07-25 09:24:02
Original
1610 people have browsed it

Use the PHP function "setcookie" to set cookies

In website development, cookies are a very common technology. It is used to store a small amount of data in the user's browser so that it can be used between different pages. transfer information between. PHP provides a function called "setcookie" for setting cookie values ​​and attributes. In this article, we will learn how to set cookies using the "setcookie" function.

The following is the basic syntax for setting cookies using the "setcookie" function:

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

Parameter description:

  • name: the name of the cookie. Must be set.
  • value: The value of the cookie. Can be a string or other data type.
  • expire: cookie expiration time. The default is 0, which means it will expire when the browser is closed. It can also be set to a UNIX timestamp to specify the specific expiration time point.
  • path: The path of the cookie. Defaults to the current page.
  • domain: The domain name of the cookie. The default is empty, indicating the current domain name.
  • secure: Whether to only send cookies over HTTPS connections. Defaults to false, which means cookies can be sent over HTTP connections.
  • httponly: Whether to only allow access to cookies through the HTTP protocol. The default is false, which means the cookie can be accessed through JavaScript.

The following are some common usage examples:

  1. Set a cookie named "username" with a value of "John" and an expiration time of 1 hour:
setcookie("username", "John", time()+3600);
Copy after login
  1. Set a cookie named "username" with a value of "John", an expiration time of one month, and a scope of the entire domain name:
setcookie("username", "John", time()+2592000, "/");
Copy after login
  1. Set a cookie named "rememberMe" with a value of "true", an expiration time of one week, and a scope of subdomain name:
setcookie("rememberMe", "true", time()+604800, "/", "subdomain.example.com");
Copy after login
  1. Set a cookie named "theme" Cookie with value "dark", expiration time of one year, only sent through HTTPS, only allowed to be accessed through HTTP protocol:
setcookie("theme", "dark", time()+31536000, "/", "", true, true);
Copy after login

can be set and customized according to actual needs, using different parameters cookies. Of course, after setting the cookie, we can also use PHP's "$_COOKIE" super global variable to read the value of the set cookie.

Summary:

By using PHP's "setcookie" function, we can easily set and manage cookies. By specifying different parameters, we can customize cookie values, expiration time, scope and other attributes to meet the needs of actual projects. In actual development, we should set and use cookies reasonably based on security and business needs to provide a better user experience and functional interaction.

The above is the detailed content of Use the PHP function 'setcookie' to set cookies. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!