PHP Web Fundamentals Reading Cookie Values - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Web Fundamentals Reading Cookie Values - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Tuesday, May 28, 2019

PHP Web Fundamentals Reading Cookie Values

PHP Web Fundamentals



Reading Cookie Values

Problem

You want to read the value of a cookie that you’ve previously set.

Solution

Look in the $_COOKIE superglobal array:

         if (isset($_COOKIE['flavor'])) {
              print "You ate a {$_COOKIE['flavor']} cookie.";
         }

Discussion

A cookie’s value isn’t available in $_COOKIE during the request in which the cookie is set. In other words, calling the setcookie() function doesn’t alter the value of $_COOKIE. On subsequent requests, however, each cookie sent back to the server is stored in $_COOKIE.

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/>";
         }




No comments:

Post a Comment

Post Top Ad