Backend Development 5 min read

How to Set Cookies in PHP Using the setcookie Function

This article explains the PHP setcookie function, detailing its syntax, parameter meanings, and provides multiple practical examples for creating cookies with various lifetimes, paths, domains, and security flags, helping developers effectively manage client-side data in web applications.

php中文网 Courses
php中文网 Courses
php中文网 Courses
How to Set Cookies in PHP Using the setcookie Function

In website development, cookies are a very common technology used to store a small amount of data in the user's browser to transfer information between pages. PHP provides a function called setcookie to set the value and attributes of a cookie. This article explains how to use the setcookie function to set cookies.

The basic syntax for using the setcookie function to set a cookie is:

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

Parameter description:

name : the name of the cookie (required).

value : the value of the cookie; can be a string or other data type.

expire : the expiration time of the cookie. Default is 0 (expires when the browser closes). It can also be a UNIX timestamp for a specific expiration point.

path : the path on the server in which the cookie will be available. Default is the current page.

domain : the domain that the cookie is available to. Default is empty, meaning the current domain.

secure : whether the cookie should be sent only over HTTPS connections. Default is false , allowing HTTP transmission.

httponly : whether the cookie is accessible only via the HTTP protocol. Default is false , allowing JavaScript access.

Below are some common usage examples:

1. Set a cookie named "username" with value "John" that expires in 1 hour:

setcookie("username", "John", time()+3600);

2. Set a cookie named "username" with value "John" that expires in one month and is valid for the entire domain:

setcookie("username", "John", time()+2592000, "/");

3. Set a cookie named "rememberMe" with value "true" that expires in one week and is scoped to a subdomain:

setcookie("rememberMe", "true", time()+604800, "/", "subdomain.example.com");

4. Set a cookie named "theme" with value "dark" that expires in one year, is sent only over HTTPS, and is HttpOnly:

setcookie("theme", "dark", time()+31536000, "/", "", true, true);

You can choose different parameters according to actual needs to set and customize cookies. After setting a cookie, you can also read its value using PHP's $_COOKIE superglobal.

Summary:

By using PHP's setcookie function, developers can easily create and manage cookies. Specifying appropriate parameters allows customization of the cookie's value, expiration time, scope, and security attributes, which helps meet project requirements, improve user experience, and ensure safe data handling.

backend developmentPHPWeb Securitycookiessetcookie
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.