Using PHP's setcookie Function to Create and Manage Cookies

This article explains how PHP's setcookie function works, describes each parameter, and provides several practical code examples for setting cookies with different lifetimes, scopes, and security options, helping developers manage client‑side data effectively.

php Courses
php Courses
php Courses
Using PHP's setcookie Function to Create and Manage Cookies

In website development, a cookie is a widely used technique that stores a small amount of data in the user's browser to transfer information between pages. PHP provides the setcookie function to set a cookie's value and attributes. This article teaches how to use setcookie to create cookies.

The basic syntax is:

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

Parameters:

name : the cookie name (required).

value : the cookie value, which can be a string or other data type.

expire : the expiration time. The default is 0 (expires when the browser closes). It can also be a UNIX timestamp specifying an exact expiration moment.

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

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

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

httponly : whether the cookie is accessible only through the HTTP protocol. The default is false, allowing JavaScript access.

Below are some common usage examples:

1. Set a cookie named "username" with the value "John" that expires in one hour: setcookie("username", "John", time()+3600); 2. Set a cookie named "username" with the value "John" that expires in one month and is available to the entire domain:

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

3. Set a cookie named "rememberMe" with the 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 the value "dark" that expires in one year, is sent only over HTTPS, and is HttpOnly:

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

You can customize cookies according to actual requirements by using different parameters. After setting a cookie, the PHP superglobal $_COOKIE can be used to read its value.

Summary:

By using PHP's setcookie function, developers can easily set and manage cookies, customizing attributes such as value, expiration time, path, domain, and security flags to meet project needs, thereby improving user experience and functional interaction while maintaining appropriate security considerations.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendsecurityPHPcookiessetcookie
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

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.