I've got a pretty decent like system for my website. As soon as a user clicks the ❤️ my script.js
sends an Ajax containing the blog post's pathname to my PHP server which checks whether the user has already liked it (if so, there's a respective cookie). If not, it reads the content of my like.json
and looks for the count of the post which will then be increased by 1. After that, the server gives the user a cookie saying he's already liked this post.
Now I tell you: Everything works absolutely fine on my localhost. But when I upload everything to the production server, it fails to set the "already liked" cookie.
First, let's see all the code files:
script.js
sends the Ajax:
var url = window.location.pathname;
$.ajax({
type: "POST",
url: 'like.php',
data: { "liked": url },
success: function(response){
}
});
Here's the PHP file:
<?php
setcookie('why-doesnt-work-anything', 'no-idea', time() + (86400 * 30), NULL, NULL, TRUE, NULL);
$url = $_POST['liked'];
$filename = "like.json";
$fd = fopen ($filename, "r");
$contents = fread ($fd, filesize($filename));
fclose ($fd);
$originalcontents = $contents;
$contents = json_decode($contents);
$contents->$url = $contents->$url + 1;
setcookie('nothing-works', 'no-clue', time() + (86400 * 30), NULL, NULL, TRUE, NULL);
echo $contents->$url;
setcookie('doesnt-work', 'i-dont-know-why', time() + (86400 * 30), NULL, NULL, TRUE, NULL);
$contents = json_encode($contents);
$urls = $url;
if($_COOKIE['like'] != undefined && $_COOKIE['like'] != ''){
$urls = $url . ',' . $_COOKIE['like'];
}
if(in_array($url,explode(',',$_COOKIE['like']))){
$urls = $_COOKIE['like'];
$contents = $originalcontents;
}
setcookie('like', $urls, time() + (86400 * 30), NULL, NULL, TRUE, NULL); // the actual liked cookie
$fp = fopen ($filename, "w");
fwrite ($fp,$contents);
fclose ($fp);
?>
Here's the "exciting" part: It's not about cookies in general. As you see, I created 3 test cookies. Remember: On my localhost, everything works fine. But on the production server, all attempts to set a cookie fails after the echo
line. The first 2 cookies work but not the 3rd one.
I don't think it's a bug at my hosting provider (1&1). Is it due to different PHP versions? Or what's the f***ing problem?
Thanks for your help!
As soon as you echo something, you can no more set a cookie unless output buffering is enabled by default in your environment; what might be the case on your localhost.
The documentation says:
Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including and tags as well as any whitespace.
and also:
Note: You can use output buffering to send output prior to the call of this function, with the overhead of all of your output to the browser being buffered in the server until you send it. You can do this by calling ob_start() and ob_end_flush() in your script, or setting the output_buffering configuration directive on in your php.ini or server configuration files.
PS: You must also consider the comment of Niet the Dark Absol about using cookie in such a situation.