Home  >  Article  >  Backend Development  >  Use the PHP function 'setcookie' to set cookies

Use the PHP function 'setcookie' to set cookies

WBOY
WBOYOriginal
2023-07-25 09:22:541658browse

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);

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);
  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, "/");
  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");
  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);

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!

Statement:
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