I'm trying to store a cookie after login and it all works fine on a test system but as soon as I put it live it fails? I've tried the standard w3schools code (below) and even this is the same so it can't be my code.
PHP and Apache versions are not identical but within a few releases of each other (Live : PHP 5.5.21 and Apache 2.4.12 vs Test : PHP 5.6.14 and Apache 2.4.10) so before I start upgrading I thought I'd check here as this surely cannot be the reason. Am I missing something? (sorry but I'm a noob at PHP development) why does something so simple fail on a live site? The live site has successfully hosted Drupal which uses a cookie, and I've tried multiple browsers/machines all accepting cookies form other sites so I'm baffled...
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
oh my god.. so I found the answer after stack exchange suggested this following my post - Transferred Hosts and setting cookies doesn't work now
There was a very small amount of whitespace at the top of the code page and so the server did not seem to send it as the first part of the header. test code is now working, I'll go and fix my 'real' code now I know... so - answer is - snuggle the cookie code right up to the top left of your editor!