
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
Check if a PHP cookie exists and if not set its value
Based on the PHP manual, the existence of a cookie can’t be found.
A reference from the manual: “Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.”
The reason being cookies are response headers to the browser and the browser needs to then send them back along with the next request. This is the reason they are available on the second page load only.
But here is a work around for the same: The $_COOKIE can be set when the setcookie function is called −
if(!isset($_COOKIE['lg'])) { setcookie('lg', 'ro'); $_COOKIE['lg'] = 'ro'; } echo $_COOKIE['lg'];
- Related Articles
- How to check if a cookie is set in Laravel?
- Check if a particular value exists in Java LinkedHashMap
- Check if a particular value exists in Java TreeSet
- How to check if a value exists in an R data frame or not?
- Python - Check if a number and its double exists in an array
- Python - Check if a number and its triple exists in an array
- How Check if object value exists not add a new object to array using JavaScript ?
- Check if a value exists in an array and get the next value JavaScript
- Check if value exists for a field in a MongoDB document?
- How to check if a file exists or not using Python?
- How to check if a file exists or not in Java?
- C# Program to Check if Value exists in Hashtable
- How to check if a table exists in MySQL and create if it does not already exist?
- Check if table exists in MySQL and display the warning if it exists?
- Check if a word exists in a grid or not in Python

Advertisements