The IETF recommends using base64 encoding for binary cookie values: https://datatracker.ietf.org/doc/html/draft-ietf-httpstate-cookie-07
So I use setrawcookie(..) but I don't know what variable to use to get the cookie back because $_COOKIE[..] still uses the URL decoding that matches setcookie(..). This replaces "+" with " " in the output.
<?php
var_dump($_COOKIE['TEST']);
$binary_string = "";
for($index = 0; $index < 256; $index++){
$binary_string .= chr($index);
}
$encoded_data = base64_encode($binary_string);
var_dump($encoded_data);
$cookie_set = setrawcookie('TEST', $encoded_data, time() + 3600);
?>
You should use normal setcookie instead setrawcookie, this way you can avoid situation when trying to send cookie with not allowed characters (cookie will not be set) and it's value in $_COOKIE array will be usable.
If you insist on raw method, you can find cookies send by browser in $_SERVER['HTTP_COOKIE']. This will be string formatted like key1=value1; key2=value2. One way to get it in array is to do simple exploding:
foreach(explode('; ',$_SERVER['HTTP_COOKIE']) as $rawcookie)
{
list($k,$v) = explode('=',$rawcookie, 2);
$_RAWCOOKIE[$k] = $v;
}
var_dump($_RAWCOOKIE);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With