Skip to content

Instantly share code, notes, and snippets.

@davebarnwell
Last active November 3, 2015 08:18
Show Gist options
  • Select an option

  • Save davebarnwell/17fe8bd50c40619b80ac to your computer and use it in GitHub Desktop.

Select an option

Save davebarnwell/17fe8bd50c40619b80ac to your computer and use it in GitHub Desktop.
Test cookie set on 302 redirect
<?php
/**
* Test for cookie setting when doing a 302 redirect,
* has been known to cause issues in certain browser versions most notible Safari.
*
* Copied from here http://blog.dubbelboer.com/2012/11/25/302-cookie.html
*/
if (isset($_GET['show'])) {
// Delete the cookie (note: it will still be set in $_COOKIE).
setcookie('test');
echo 'Cookie was: ';
if (isset($_COOKIE['test']) && ($_COOKIE['test'] == $_GET['show'])) {
echo 'SET';
} else {
echo 'NOT SET';
}
// Make sure this is a new url. This is needed for 301 responses which are otherwise cached.
echo '<br><a href="?a=' , microtime(true) , '">try again</a>';
} else {
$id = microtime(true);
setcookie('test', $id, time() + 4);
header('Location: ?show=' . $id, true, 302); // Could also use 301.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment