How to Set Up Cookies in PHP: A Complete Guide with Examples

How to set up cookies in PHP using the setcookie() function with practical examples

Post Date: January 31, 2020
Last Updated:
November 10, 2026

Introduction

Cookies are an essential part of web development because they help websites remember information about visitors between requests. They are small pieces of data stored in a user’s web browser and can be used to maintain preferences, remember user settings, and support certain website functionalities.

In PHP, cookies make it possible to store information on the user’s browser and retrieve it when they visit the website again. For example, a website can use cookies to remember a user’s preferred language, theme, or other non-sensitive preferences.

PHP provides a built-in setcookie() function to create cookies. In this guide, we will explain how to set up cookies in PHP, understand the setcookie() syntax, create cookies with expiration dates, retrieve cookie values, and manage cookie settings with practical examples.

A cookie is a small piece of information stored in the user’s web browser. It allows a website to remember certain information between visits.

For example, cookies can be used for:

  • Remembering user preferences.
  • Storing language and theme settings.
  • Remembering shopping cart information.
  • Maintaining certain user preferences between visits.
  • Supporting website functionality.

Important: Cookies are stored on the user’s device, not directly in the server database. Avoid storing passwords, authentication secrets, or other sensitive information in plain-text cookies.

2. How to Set Up Cookies in PHP

PHP provides the built-in setcookie() function to create cookies.

The setcookie() function sends a cookie to the user’s browser through an HTTP response header. Therefore, it must be called before sending any HTML content, whitespace, or other output to the browser.

PHP setcookie() Syntax

PHP setcookie() function syntax showing cookie name, value, expiration, path, domain, secure, and httponly parameters

The modern PHP version also supports an options array, which makes it easier to configure cookie security attributes.

Parameters of the setcookie() Function

ParameterDescription
nameSpecifies the cookie name.
valueSpecifies the value stored in the cookie.
expiresDefines when the cookie expires using a Unix timestamp.
pathSpecifies the server path where the cookie is available.
domainDefines the domain that can access the cookie.
secureWhen enabled, the cookie is transmitted only over HTTPS.
httponlyPrevents JavaScript from accessing the cookie.

Note: If the expiration time is set to 0 or omitted, the cookie is generally treated as a session cookie and expires when the browser session ends. Manual+1

Let’s create a simple cookie using PHP.

In this example, we will create a cookie named user and store the value michel. The cookie will expire after 30 days.

Example:

PHP setcookie() example showing how to create a cookie with a 30-day expiration time

Explanation:

  • $cookie_name defines the name of the cookie.
  • $cookie_value stores the value of the cookie.
  • time() returns the current Unix timestamp.
  • 86400 represents 24 hours in seconds.
  • time() + (86400 * 30) sets the expiration time to 30 days.
  • / makes the cookie available throughout the domain.

Important: The cookie will not immediately appear in the $_COOKIE variable during the same request. It becomes available to PHP on the next request when the browser sends it back to the server. Manual+1

4. How to Retrieve Cookies in PHP

After creating a cookie, you can retrieve its value using the PHP $_COOKIE superglobal variable.

The $_COOKIE variable contains cookies sent by the browser with the current HTTP request.

Example:

PHP setcookie() example showing how to create a cookie with a 30-day expiration time

Explanation:

  • $_COOKIE retrieves cookie information sent by the browser.
  • isset() checks whether the specified cookie exists.
  • htmlspecialchars() helps prevent HTML injection when displaying cookie values.
  • If the cookie is available, the stored value will be displayed.

Expected Output:

PHP cookie verification output displaying a message that the cookie does not exist

You can control how long a cookie remains available by configuring its expiration time.

PHP uses the time() function to calculate the expiration timestamp.

Here are some common cookie expiration examples:

ExpirationPHP Code
1 hourtime() + 3600
1 daytime() + 86400
7 daystime() + (86400 * 7)
30 daystime() + (86400 * 30)
Session cookie0

For example, to create a cookie that expires after seven days:

PHP setcookie() code example showing how to create a cookie with a username and 7-day expiration time

This cookie will remain available until its expiration time, subject to the browser’s cookie policies and user settings.

Sometimes, you may need to remove a cookie when a user logs out or when stored preferences are no longer required.

PHP allows you to delete a cookie by setting its expiration time to a timestamp in the past.

Example:

PHP setcookie() code example demonstrating how to delete an existing cookie by setting its expiration time in the past

Explanation:

  • The cookie name must match the cookie you want to delete.
  • The value is set to an empty string.
  • time() - 3600 sets the expiration time in the past.
  • The path and domain should match the original cookie settings.

After the browser processes the response, the cookie will be removed. Manual+1

7. How to Set Secure Cookies in PHP

Cookie security is important when developing websites that handle user information.

PHP supports additional cookie attributes such as secure, httponly, and samesite.

The following example demonstrates how to create a cookie with security settings.

Example:

PHP secure cookie example using setcookie() with HTTPS, HttpOnly, SameSite, and 30-day expiration settings

Explanation:

  • expires: Sets the cookie expiration to 30 days.
  • path: Makes the cookie available throughout the domain.
  • secure: Allows the browser to send the cookie only over HTTPS.
  • httponly: Prevents JavaScript from reading the cookie.
  • samesite: Helps control when cookies are sent with cross-site requests.

Note: Use secure => true on HTTPS websites. If you use SameSite=None, the Secure attribute must also be enabled. Manual+2

8. How to Check Cookies in a Browser

You can verify whether a cookie has been created successfully using your browser’s Developer Tools.

Follow these steps:

  1. Open your website in Google Chrome.
  2. Right-click the webpage and select Inspect.
  3. Navigate to the Application tab.
  4. Select Cookies from the Storage section.
  5. Choose your website’s domain.
  6. Find the cookie name and check its value, expiration date, and security attributes.
How to set up cookies in PHP using the setcookie() function

This helps you verify whether your PHP cookie configuration is working correctly.

9. Common Issues When Setting Cookies in PHP

Sometimes cookies may not work as expected. Here are some common issues and their solutions.

ProblemSolution
Cookie is not createdCall setcookie() before sending output.
Cookie value is not displayedReload the page or make another request.
Cookie expires immediatelyCheck the expiration timestamp.
Cookie is not available on another pageSet the path to /.
Secure cookie does not workMake sure the website uses HTTPS.
Cookie is not deletedUse the same path and domain as the original cookie.

Conclusion

PHP cookies are useful for storing small amounts of information in a user’s browser and maintaining information between website visits. By using the PHP setcookie() function, developers can easily create, retrieve, update, and delete cookies.

Understanding cookie expiration, domain settings, and security attributes such as Secure, HttpOnly, and SameSite helps developers implement cookies more effectively.

Whether you are developing a simple PHP website or a dynamic web application, understanding how to manage cookies in PHP is an important web development skill.

For more information, refer to the official PHP setcookie() documentation.

Frequently Asked Questions (FAQs)

You can create a cookie using PHP’s built-in setcookie() function. Specify the cookie name, value, and expiration time.

PHP setcookie() code example showing how to create a username cookie with a one-day expiration time

Use the $_COOKIE superglobal variable to retrieve a cookie value.

A PHP cookie lasts until its configured expiration time. If the expiration time is set to 0, it is generally a session cookie that expires when the browser session ends.

Fixed:Your PHP installation appears to be missing the MySQL extension which is required by WordPress.

Create contact form with php

Need assistance with PHP cookie implementation, setcookie() configuration, cookie expiration settings, secure cookie handling, or troubleshooting PHP errors? Our technical experts can help with PHP development, website functionality, server configuration, and web application maintenance.

Get in touch with our team here.

Explore our Web Development Services to learn more about our solutions.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Comment

Scroll to Top