简体   繁体   中英

Cookie won't set. (Only on localhost)


I'm fighting with this cookie-thing for a while now and hopefully one of you can help:

setcookie("example", "value", time()+3600, "/", ".website-name.com" , false,  false);

What am I missing? This line is in set_cookie.php, wich is included into all pages (like index.php) with include_once("set_cookie.php");. It works just fine when you go to www.website-name/set_cookie.php but won't work when you include it into index.php or another page. It is included at the very beginning of the pages and I don't think that's the problem, because it worked with the localhost. I checked if the cookies are set or with chrome tools and also with echo() out the values. It's this line of code that something wrong with, I think. I don't know what it could possibly be. Please help :D

Thanks in advance!

Found an answer. Just in case someone experiencing the same problem: one.com (my webspace service) was the problem. They place some code at the very beginning of yout site, that's why I couldn't place the cookie. I had to place the cookie with another .php-file, wich I started by using ajax.

jquery:

 $('.cookie_ok').click(function(){ $('.cookie').hide(200); $.ajax({ type: "POST", url: "tip.php", data:{wert: "ja"}, datatype: "html", success: function(result){ } }) }); 

tip.php:

 <?php if (isset($_COOKIE['hinweis'])) { $hinweis = "nein"; } else { $hinweis = "ja"; setcookie("hinweis", "ja", time()+3600*24*50, "/", false, false); } ?> 

The problem that you mention is often happens when cookies.php file is included in php pages after they did some output (like echo of html tag and title), but as setcookie() works by sending header information, it can not be set, as headers can not be send after any page output.

Edit:

It nice that you found that hosting adds some information. Nice solution with ajax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM