Reading Cookie Values in PHP
By: David Sklar
You want to read the value of a cookie that's been previously set.
Look in the $_COOKIE superglobal array:
if (isset($_COOKIE['flavor'])) { print "You ate a $_COOKIE[flavor] cookie."; }
A cookie's value isn't available in $_COOKIE during the request in which the cookie is set. In other words, the setcookie( ) function doesn't alter the value of $_COOKIE. On subsequent requests, however, each cookie is stored in $_COOKIE. If register_globals is on, cookie values are also assigned to global variables.
When a browser sends a cookie back to the server, it sends only the value. You can't access the cookie's domain, path, expiration time, or secure status through $_COOKIE because the browser doesn't send that to the server.
To print the names and values of all cookies sent in a particular request, loop through the $_COOKIE array:
foreach ($_COOKIE as $cookie_name => $cookie_value) { print "$cookie_name = $cookie_value<br>"; }
Archived Comments
1. Brettkip
View Tutorial By: Brettkip at 2017-06-13 08:46:18
2. good
View Tutorial By: dock at 2013-06-07 15:19:30
3. good
View Tutorial By: dock at 2013-06-07 15:18:49
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
PHP code to write to a CSV file for Microsoft Applications
PHP code to write to a CSV file from MySQL query
PHP code to import from CSV file to MySQL
Password must include both numeric and alphabetic characters - Magento
Error: Length parameter must be greater than 0
PHP file upload prompts authentication for anonymous users
PHP file upload with IIS on windows XP/2000 etc
Multiple File Upload in PHP using IFRAME
Resume or Pause File Uploads in PHP
Exception in module wampmanager.exe at 000F15A0 in Windows 8