I am working on a website where I use cookies to login and to track whether people have watched a certain page (for view counts). After working on it for a while, I decided to launch the website, and put the site on a server. But while testing a saw that I could not login. I first discovered this while using sessions. I would set the session, but the session would not stay around. After looking through the code, I could not find one issue. So I decided to change this to cookies, which added some useful session customization. I thought: perfect. So I test this code on localhost, and it worked perfectly: no problem at all. So I put this live on the server and again, nothing was set. No cookies, nothing. I've googling for the past two days, without any effort, but nothing shows a working solution for me.
Now, he some useful, found info: I use ob_start and ob_end_flush; I use setcookie($key, $value, strtotime(+1 month), '/'); I tried forcing the cookie by using $_COOKIE[$key] = $value before using the previous line; I made a testscript, consisting of:
session_start();
ob_start();
error_reporting(E_ALL);
if (!isset($_COOKIE['test'])){
$_COOKIE['test'] = 'test';
$success = setcookie("test","test", strtotime("+1 month"),"/");
var_dump($success);
echo "a";
}
else{
var_dump($_COOKIE);
echo "b";
}
ob_end_flush();
Result: "bool(false) a". So setcookie returns false. Why? That's my question. It works on localhost, but I also uploaded the script to a website of a friend of mine and it works. His and mine website are hosted by the same company, but probably on a different server. He ran the script on his pc too, went exactly the same as with me, so it isn't one of my pc's settings.
/tl;dr/ setcookie returns false on the server, but works (returns true) on localhost and another website.