Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP cookies problem, works in Firefox not in other browser

I've a problem with setting cookies in php. I've to say that I'm not very experienced with php, so maybe is a very stupid problem.

I've an ajax rating system that should check a cookie to see if the the photo has already been voted.

The page called with ajax check for the cookie, add the id of the photo you are voting to it and call this function:

   setcookie("Name", $cookie, time()+(60*24*365), "/",  $_SERVER['HTTP_HOST'], 0); 

The page that display the photo also call the cookie

   $cookie = $_COOKIE['Name'];

and check to see if you have already voted.

A problem may be the fact that the ajax page is in a different directory than the page that display the photo.

The page that display the photo is in the root directory, the page that cast the vote is in /ajax/vote.php

The voting system works, before I was checking the IPs, but know i need to check the cookies.

It work in Firefox without any problem, but when I've started testing on IE and Safari it seem they don't see the cookie.

I've checked with IECookieViewer and when I cast a vote the cookie is created allright, but when I go back to the page, it look like the page don't find the cookie. Also if I cast another vote the cookie is replaced with a new one.

Sorry for the bad english, I hope the problem is understandable

P.S. Forgot to point something that might be related to the problem. The page is inside an iframe.

like image 743
Kesty Avatar asked Jul 31 '09 10:07

Kesty


1 Answers

Check the cookie settings of the other browsers and if they're set to block all or empty on exit.

If the cookies work in one browser, but not another, you will need to make sure that the other browser is letting you set cookies in the first place.

Sometimes it will look like you can create the cookie, but then it will disappear or be deleted with each page reload.

Cookies from an iframe

It's also possible that because you're setting the cookies in an iframe, that the browsers may view it as a third-party cookie and reject it unless explicitly set out in the browser preferences to allow third-party cookies.

In that case you would need a compact privacy policy (or a compact P3P header) on the pages from where you're trying to set the cookies from.

For PHP, you would add this as your header for the page setting the cookie:

header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"'); 
like image 60
random Avatar answered Oct 27 '22 00:10

random