Skip to content

Instantly share code, notes, and snippets.

@craiga
Last active August 29, 2015 14:00
Show Gist options
  • Save craiga/11390574 to your computer and use it in GitHub Desktop.
Save craiga/11390574 to your computer and use it in GitHub Desktop.
Encode a string so it can be safely used with setrawcookie.
<?php
/**
* Encode a string so it can be safely used with {@link http://php.net/setrawcookie setrawcookie}.
*
* @author Craig Anderson <craiga@craiga.id.au>
* @link https://gist.github.com/craiga/11390574
*/
function cookieencode($s) {
$result = $s;
$unsafeCharacters = array(",", ";", " ", "\t", "\r", "\n", "\013", "\014");
foreach($unsafeCharacters as $character) {
$result = str_replace($character, "%" . strtoupper(base_convert(ord($character), 10, 16)), $result);
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment