Log in

View Full Version : Cookie not working in IE. Please check my coding.



dog
12-18-2006, 11:40 AM
Hello everyone,

I'm having trouble with writing a cookie in IE.

I'm using the code shown below and it's working fine in Firefox but not in IE.

This code is the first thing on the page, placed before the document type and the opening HTML tag

<?php

if ($_REQUEST['send']=="yes")
{
setcookie("return_user", $value);
setcookie("return_user", $value, time()+60*60*24*360); /* expire in 1 year */
setcookie("return_user", $value, time()+60*60*24*360, "/", ".example.com", 0);
}

?>

In case it's not obvious, the idea is that when the user returns to the page after having successfully filled out a form they arrive at form.php?send=yes and a cookie gets written.

Anyone know how I can get this working in IE and Firefox?

djr33
12-18-2006, 11:45 AM
Why are you setting the same cookie three times?

dog
12-18-2006, 11:47 AM
Why are you setting the same cookie three times?

Because I don't know what I'm doing :o

I'll change that then.

djr33
12-18-2006, 11:56 AM
http://www.php.net/manual/en/function.setcookie.php


setcookie('name','value','timeexpires','path','domain','secure')

If secure is TRUE, then it's only available on HTTPS pages.... ignore that for now.
The domain doesn't need to be set... unless you want it only to work with www.yourpage.com or something.yourpage.com, not just *.yourpage.com.
path is like if you want it only in a certain folder (would be good if you had several quizzes, so like /1/ could be set to having taken the quiz, but the folder /2/ wouldn't be... etc.). Ignore this, too, probably.

So... all you need....
setcookie('cookiename','value','expires')

So.... do you even need to set an expiration time?
If so, then use time() and add seconds til you want it to end (like 3600 would be an hour after the cookie is first set. time() outputs the current time).


As for cookiename and value... easy.

'mypage' and 'visited'

Or whatever.

You can later get this (but only on the next page... you can't send and recieve a cookie in one page load, I don't think) in your PHP script like so:
$_COOKIE['name']

So...
to continue with above--
if ($_COOKIE['mypage'] == 'visited') { echo "You have already been here!"; }

dog
12-18-2006, 12:23 PM
Thanks for the tips. That all makes good sense.

I'm still experiencing the problem. I've got a confusing mix of Javascript that I don't full understand and PHP that I've never used before overlaying on the page. So it's hard to know exactly where the problem lies.

I'm going to try to re-write it all with PHP and they I'll post it up if it's still not working.