PHP Tutorial PHP Advanced PHP References

PHP $_COOKIE Variable



The PHP $_COOKIE is a built-in super global variable which is always available in all scopes. The $_COOKIE is an associative array which contains the variables passed to the current script via HTTP Cookies.

Example: $_COOKIE example

The example below demonstrates the usage of $_COOKIE variable.

<!DOCTYPE html>
<?php
//expire at the end of the session
setcookie("name", "John Smith");
?>

<html>
<body>

  <?php 
    if(!isset($_COOKIE["name"])) {
      echo "This cookie is not set.";
    } else {
      echo "Hello ".$_COOKIE['name']."!";
    }
  ?>

</body>  
</html>

Assuming the "name" cookie has been set.

The output of the above script will be similar to:

Hello John Smith!

❮ PHP - Predefined Variables