Logo
  • Welcome to CodeIgniter4
    • Welcome to CodeIgniter4
    • Server Requirements
    • Credits
    • PSR Compliance
    • License Agreement
  • Installation
    • Composer Installation
    • Manual Installation
    • Running Your App
    • Troubleshooting
    • Deployment
    • Change Logs
    • Upgrading From a Previous Version
    • CodeIgniter Repositories
  • Build Your First Application
    • Static Pages
    • News Section
    • Create News Items
    • Conclusion
  • CodeIgniter4 Overview
    • Application Structure
    • Models, Views, and Controllers
    • Autoloading Files
    • Services
    • Factories
    • Working with HTTP Requests
    • Security Guidelines
    • Design and Architectural Goals
  • General Topics
    • Configuration
    • CodeIgniter URLs
    • Helper Functions
    • Global Functions and Constants
    • Logging Information
    • Error Handling
    • Web Page Caching
    • AJAX Requests
    • Code Modules
    • Managing your Applications
    • Handling Multiple Environments
  • Controllers and Routing
    • URI Routing
    • Controllers
    • Controller Filters
    • Auto Routing (Improved)
    • HTTP Messages
    • Request Class
    • IncomingRequest Class
    • Content Negotiation
    • HTTP Method Spoofing
    • RESTful Resource Handling
  • Building Responses
    • Views
    • View Renderer
    • View Layouts
    • View Cells
    • View Parser
    • View Decorators
    • HTML Table Class
    • HTTP Responses
    • API Response Trait
    • Content Security Policy
    • Localization
    • Alternate PHP Syntax for View Files
  • Working with Databases
    • Quick Start: Usage Examples
    • Database Configuration
    • Connecting to a Database
    • Running Queries
    • Generating Query Results
    • Query Helper Methods
    • Query Builder Class
    • Transactions
    • Getting Metadata
    • Custom Function Calls
    • Database Events
    • Database Utilities
  • Modeling Data
    • Using CodeIgniter's Model
    • Using Entity Classes
  • Managing Databases
    • Database Forge
    • Database Migrations
    • Database Seeding
    • Database Commands
  • Library Reference
    • Caching Driver
    • Cookies
    • Cross-Origin Resource Sharing (CORS)
    • CURLRequest Class
    • Email Class
    • Encryption Service
    • Working with Files
    • File Collections
    • Honeypot Class
    • Image Manipulation Class
    • Pagination
    • Publisher
    • Security
    • Session Library
    • Throttler
    • Times and Dates
    • Typography
    • Working with Uploaded Files
    • Working with URIs
    • User Agent Class
    • Validation
  • Helpers
    • Array Helper
    • Cookie Helper
    • Date Helper
    • Filesystem Helper
    • Form Helper
    • HTML Helper
    • Inflector Helper
    • Number Helper
    • Security Helper
    • Test Helper
    • Text Helper
    • URL Helper
    • XML Helper
  • Testing
    • Getting Started
    • Database
    • Generating Data
    • Controller Testing
    • HTTP Testing
    • Testing Responses
    • Testing CLI Commands
    • Mocking
    • Benchmarking
    • Debugging Your Application
  • Command Line Usage
    • CLI Overview
    • Running Controllers via CLI
    • Spark Commands
    • Creating Spark Commands
    • CLI Generators
    • CLI Library
    • CLIRequest Class
  • Extending CodeIgniter
    • Creating Core System Classes
    • Replacing Common Functions
    • Events
    • Extending the Controller
    • Authentication
    • Creating Composer Packages
    • Contributing to CodeIgniter
  • Official Packages
CodeIgniter
  • Helpers
  • Cookie Helper

Cookie Helper

The Cookie Helper file contains functions that assist in working with cookies.

  • Loading this Helper

  • Available Functions

Loading this Helper

This helper is loaded using the following code:

<?php

helper('cookie');

Available Functions

The following functions are available:

set_cookie($name[, $value = ''[, $expire = 0[, $domain = ''[, $path = '/'[, $prefix = ''[, $secure = false[, $httpOnly = false[, $sameSite = '']]]]]]]])
Parameters:
  • $name (array|Cookie|string) – Cookie name or associative array of all of the parameters available to this function or an instance of CodeIgniter\Cookie\Cookie

  • $value (string) – Cookie value

  • $expire (int) – Number of seconds until expiration. If set to 0 the cookie will only last as long as the browser is open

  • $domain (string) – Cookie domain (usually: .yourdomain.com)

  • $path (string) – Cookie path

  • $prefix (string) – Cookie name prefix. If '', the default from app/Config/Cookie.php is used

  • $secure (bool) – Whether to only send the cookie through HTTPS. If null, the default from app/Config/Cookie.php is used

  • $httpOnly (bool) – Whether to hide the cookie from JavaScript. If null, the default from app/Config/Cookie.php is used

  • $sameSite (string) – The value for the SameSite cookie parameter. If null, the default from app/Config/Cookie.php is used

Return type:

void

Note

Prior to v4.2.7, the default values of $secure and $httpOnly were false due to a bug, and these values from app/Config/Cookie.php were never used.

This helper function gives you friendlier syntax to set browser cookies. Refer to the Response Library for a description of its use, as this function is an alias for CodeIgniter\HTTP\Response::setCookie().

Note

This helper function just sets browser cookies to the global response instance that Services::response() returns. So, if you create and return another response instance (e.g., if you call redirect()), the cookies set here will not be sent automatically.

get_cookie($index[, $xssClean = false[, $prefix = '']])
Parameters:
  • $index (string) – Cookie name

  • $xssClean (bool) – Whether to apply XSS filtering to the returned value

  • $prefix (string|null) – Cookie name prefix. If set to '', the default value from app/Config/Cookie.php will be used. If set to null, no prefix

Returns:

The cookie value or null if not found

Return type:

mixed

Note

Since v4.2.1, the third parameter $prefix has been introduced and the behavior has been changed a bit due to a bug fix. See Upgrading for details.

This helper function gives you friendlier syntax to get browser cookies. Refer to the IncomingRequest Library for detailed description of its use, as this function acts very similarly to CodeIgniter\HTTP\IncomingRequest::getCookie(), except it will also prepend the Config\Cookie::$prefix that you might’ve set in your app/Config/Cookie.php file.

Warning

Using XSS filtering is a bad practice. It does not prevent XSS attacks perfectly. Using esc() with the correct $context in the views is recommended.

delete_cookie($name[, $domain = ''[, $path = '/'[, $prefix = '']]])
Parameters:
  • $name (string) – Cookie name

  • $domain (string) – Cookie domain (usually: .yourdomain.com)

  • $path (string) – Cookie path

  • $prefix (string) – Cookie name prefix

Return type:

void

Lets you delete a cookie. Unless you’ve set a custom path or other values, only the name of the cookie is needed.

<?php

delete_cookie('name');

This function is otherwise identical to set_cookie(), except that it does not have the value and expire parameters.

This also just sets browser cookies for deleting the cookies to the global response instance that Services::response() returns.

Note

When you use set_cookie(), if the value is set to empty string and the expire is set to 0, the cookie will be deleted. If the value is set to non-empty string and the expire is set to 0, the cookie will only last as long as the browser is open.

You can submit an array of values in the first parameter or you can set discrete parameters.

<?php

delete_cookie($name, $domain, $path, $prefix);
has_cookie(string $name[, ?string $value = null[, string $prefix = '']])
Parameters:
  • $name (string) – Cookie name

  • $value (string|null) – Cookie value

  • $prefix (string) – Cookie prefix

Return type:

bool

Checks if a cookie exists by name in the global response instance that Services::response() returns. This is an alias of CodeIgniter\HTTP\Response::hasCookie().

Previous Next

© Copyright 2019-2025 CodeIgniter Foundation. Last updated on May 02, 2025.

Built with Sphinx using a theme provided by Read the Docs.