Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created May 31, 2023 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/2aea1b92fb0e0e159212cdfd3e46e672 to your computer and use it in GitHub Desktop.
Save code-boxx/2aea1b92fb0e0e159212cdfd3e46e672 to your computer and use it in GitHub Desktop.
PHP Cookies

PHP COOKIES

https://code-boxx.com/cookies-in-php/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?php
// (A) SET A "COLOR" COOKIE WITH VALUE "RED"
setcookie("Color", "Red");
<?php
// (A) COOKIES ARE AUTOMATICALLY PARSED INTO $_COOKIE SUPERGLOBAL
print_r($_COOKIE);
// (B) $_COOKIE IS AN ARRAY
echo $_COOKIE["Color"];
<?php
// (A) TO UPDATE A COOKIE, SIMPLY SET COOKIE AGAIN
setcookie("Color", "Blue");
// (B) OR CREATE MORE COOKIES
setcookie("Hello", "World");
<?php
// (A) COOKIES CANNOT ACCEPT ARRAYS
// (A1) SERIALIZE THE ARRAY
setcookie("ARRAYA", serialize(["Foo", "Bar"]));
// (A2) OR JSON ENCODE
setcookie("ARRAYB", json_encode(["Hello", "World"]));
<?php
// (B) TO RETRIEVE THE ARRAY
// (B1) UNSERIALIZE THE ARRAY
$arrA = unserialize($_COOKIE["ARRAYA"]);
print_r($arrA);
// (B2) JSON DECODE
$arrB = json_decode($_COOKIE["ARRAYB"]);
print_r($arrB);
<?php
// (A) SIMPLY SET A PAST TIME TO DELETE COOKIE
setcookie("Color", null, -1);
// (B) $_COOKIE WILL NOT REFLECT THE CHANGE IMMEDIATELY!
print_r($_COOKIE); // $_cookie["color"] still exists
// (C) MANUALLY UNSET TO REMOVE IMMEDIATELY
unset($_COOKIE["Color"]);
print_r($_COOKIE); // $_cookie["color"] gone
<?php
set_cookie("KEY", "VALUE", [
"expires" => time() + 3600, // EXPIRES 1 HOUR (3600 SECS) FROM NOW
"domain" => ".site.com", // THIS COOKIE IS FOR *.SITE.COM
"path" => "/", // APPLICABLE TO ALL PATHS
// "path" => "/products", // APPLICABLE TO SITE.COM/PRODUCTS ONLY
"secure" => true, // APPLICABLE ON HTTPS ONLY
"httponly" => true, // JAVASCRIPT CANNOT ACCESS THIS COOKIE
"samesite" => "None" // FOR CORS - NONE, LAX, OR STRICT
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment