I am trying to set a cookie and redirect. Using Debian GNU/Linux 6.0 (64 bit) with PHP 5.3.3-7+squeeze19 with Suhosin-Patch (cli) (built: Feb 17 2014 10:10:23) and Apache/2.2.16 (Debian).
For some reason this works:
<?php
$cookie_name = $_GET['a'];
$cookie_value = $_GET['b'];
setcookie($_GET['a'], $_GET['b'], time() + (86400 * 30), "/"); // 86400 = 1 day
?>
But this does not:
<?php
$cookie_name = $_GET['a'];
$cookie_value = $_GET['b'];
setcookie($_GET['a'], $_GET['b'], time() + (86400 * 30), "/"); // 86400 = 1 day
header("Location: http://www.example.com");
exit;
?>
Even after several page loads. I've tried adding error reporting to the top of my code, but I don't see any errors when I load the page nor in the Apache log (/var/log/apache2/error.log):
error_reporting(E_ALL);ini_set('display_errors','1');
For some reason whenever I redirect, even using javascript as below, a cookie will not add.
<?php
$cookie_name = $_GET['a'];
$cookie_value = $_GET['b'];
setcookie($_GET['a'], $_GET['b'], time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="1;url=http://www.example.com">
<script type="text/javascript">
window.location.href = "http://www.example.com"
</script>
<title>Page Redirection</title>
</head>
<body>
If you are not redirected, follow <a href='http://www.example.com'>this link</a>!
</body>
</html>
Why does the first example work but not the others?