How to use the setcookie function to set cookies in PHP

王林
Release: 2023-06-26 12:02:02
Original
1886 people have browsed it

In web development, cookies are a very common technology that allow web applications to store and access data on the client side. In PHP programming, setting cookies is usually implemented using the setcookie function.

The syntax of the setcookie function is as follows:

bool setcookie ( string $name [, string $value [, int $expire [, string $path [, string $domain [, bool $secure [, bool $httponly ]]]]]] )
Copy after login

Among them, the name parameter is required, and other parameters are optional. The meaning of the parameters is as follows:

  • name: The name of the cookie to be set.
  • value: Cookie value to be set.
  • expire: Expiration time in UNIX timestamp format. If not specified, the cookie expires at the end of the browser session.
  • path: The directory where the cookie can be accessed.
  • domain: The domain name that can access the cookie. By default, cookies can only be set under the current domain name.
  • secure: If set to TRUE, when using an SSL connection, the cookie will only be accessed over HTTPS when transmitted.
  • httponly: If set to TRUE, the cookie can only be accessed through the HTTP protocol and not through JavaScript.

The following is a simple example that demonstrates how to set a Cookie named "username":

setcookie("username", "tom");
Copy after login

When the browser visits the page for the first time, the Cookie will auto configuration. It is worth noting that if you need to set multiple cookies, just use multiple setcookie function calls.

The following is a slightly more complex example that demonstrates how to set a cookie named "username" and expire after 1 day:

$expire = time() + 3600 * 24; // 1天后过期
setcookie("username", "tom", $expire);
Copy after login

In the above example, the time function is used to obtain Take the current timestamp and add it to 3600*24 (the number of seconds in a day) to get the expiration time. In practical applications, you can also use PHP's date processing functions (such as strtotime) to calculate the expiration time.

In addition to setting the cookie value and expiration time, you can also control who can access the cookie by setting the path and domain parameters. For example, the following example demonstrates how to set a Cookie named "username", which can only be accessed in the /example directory:

setcookie("username", "tom", time() + 3600 * 24, "/example");
Copy after login

In short, using the setcookie function can easily set Cookies, thereby implementing Web applications Functions within a program to store and access data. Whether you are calling a function once to set one cookie, or setting multiple different cookies, you can use the setcookie function to easily complete it.

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