Click to See Complete Forum and Search --> : Cookie Issues -- Works on Firefox but not IE


doodler85
10-11-2007, 10:44 PM
So. I wrote a script that sets a cookie, and everything's peachy... except the cookie does not appear to be recognized in IE unless you leave the page and come back to it. This is an issue because it remembers your age (it's for an ESRB thing) and people using IE can just refresh and get the form all over again. This is not the case in Firefox... wherein everything works as I thought it would.

Do you know why this behavior difference is occuring? What would be a solution?

Thanks!

scragar
10-11-2007, 11:04 PM
I can't think of why this would be happening, but if it's too bad you could always use the location header to reload the page:
//you can remove the first line below if you don't have query strings to worry about.
foreach($_GET as $key => $val){$qrystr="$key=".urlencode($val)."&";};
header("location: ?${qrystr}");

doodler85
10-11-2007, 11:59 PM
Thanks for the response!

I don't think reloading the page would be ideal because the entire site is built out in a single Flash unit. Although in this case an age check failure is the only concern, so reloading via the location parameter is an option... but it seems like there should be a better solution. Is there?

MrCoder
10-12-2007, 07:16 AM
Use $_SESSION to store the age.

doodler85
10-12-2007, 01:10 PM
Hey guys,

Thanks again for your thoughts so far. I implemented sessions that I *believe* are working, but I still have the same problem. If you go to the website in IE, fill out the age information and refresh... the data doesn't apply. However if you leave the site and return, the cookie works just fine.

Just in case my code is to blame I've copy-pasted the contents of my setCookie.php file below. I've never used sessions before so if my implementation is **** let me know :) Also if you see anything else that could be done better I'd appreciate any comments. Mind that our client is running a PHP 4.x server.

:confused:

$error = 0;
$lang = "en";

if($_REQUEST["myFunction"] == "GET") $error = _getCookie($_REQUEST["cookie"]);
else if($_REQUEST["myFunction"] == "SET") $error = _setCookie($_REQUEST["cookie"]);

//if($error == 1) $lang = _getLang($_REQUEST["cookie"]);

/* ********* */

header('Content-Type: text/plain');
echo 'error=', $error, '&myLang=', $lang;

/* ********* */

function _getCookie($cookieName){

if(!empty($_SESSION["passFail"]) && !empty($_SESSION["myLang"])) {

session_start();

global $lang;
$lang = $_SESSION["myLang"];

if($_SESSION["passFail"] == "pass"){
return 1;
}
return -1;

} else if(isset($_COOKIE[$cookieName])){

$cookiePieces = explode("_", $_COOKIE[$cookieName]);

global $lang;
$lang = $cookiePieces[1];

if($cookiePieces[0] == "pass"){
return 1;
}
return -1;

} else

return 0;

}

function _setCookie($cookieName){

session_start();

//if(empty($_SESSION["passFail"]) || empty($_SESSION["myLang"])){
$_SESSION["passFail"] = $_REQUEST["passFail"];
$_SESSION["myLang"] = $_REQUEST["myLang"];
//}

if(isset($_COOKIE[$cookieName])){
//setcookie ("cookiename", "", time( ) - 1);
}

$contents = $_REQUEST["passFail"] . "_" . $_REQUEST["myLang"];
setcookie($cookieName, $contents, time()+60*60*24*7);

return 1;

}

scragar
10-12-2007, 01:28 PM
session_start needs to go above right at the top of the document, before you echo anything or even think of checking them

doodler85
10-12-2007, 02:25 PM
It works :( You're a hero :(

Thanks!