Page 1 of 1

setcookie() doesn't work at all! (SOLVED)

Posted: Tue Jul 19, 2005 8:21 am
by Phoenixheart
Hello, I have a problem that needs your help!!!

Code: Select all

$code = $_REQUEST['code'];
$qty = $_REQUEST['txtNo'];
setcookie("e;cart[$code]"e;,$qty,time()+3600,"e;/"e;);
As you see, above is a simple piece of code to set cookie. And I'M SURE THAT IT DID WORK WELL, at least on my personal computer (both local and internet). But today when I surf my site (http://www.saigonartframe.com/products.php - you can check it) on 2 other PCs it doesn't work at all. When I check the Cookies folder (Documents and Settings), no cookie from saigonartframe exists (others, like devnetwork's do). I tried refreshing my browser thousands of times and checking if(setcookie("check","checkvalue")) => return TRUE. No sense.
Anyone can help me?

Posted: Tue Jul 19, 2005 8:40 am
by Burrito
are you sure you're accessing the cookie by the correct name?

you have a variable name in there...can we see the code you're using to retrive the cookie value?

Posted: Tue Jul 19, 2005 8:50 am
by neophyte

Code: Select all

setcookie("cart[$code]", $qty, time()+3600, "/", ".saigonartframe.com", 1);
Did you try adding a domain to the setcookie function? Try this. Open a second browser and see if you are getting the cookie there.

Posted: Tue Jul 19, 2005 10:20 pm
by Phoenixheart
neophyte wrote:

Code: Select all

setcookie("cart[$code]", $qty, time()+3600, "/", ".saigonartframe.com", 1);
Did you try adding a domain to the setcookie function? Try this. Open a second browser and see if you are getting the cookie there.
Yes I tried it too. But it doesn't work :evil:
And I MANUALLY check the cookies folder, so there's nothing to do with my code. But I post it here:

Code: Select all

if(isset($_COOKIE['cart'])){
//do a foreach loop here to show items, one by one row
}
else{
//something to echo "Your shopping cart is empty!"
}
And, as I said, because there's no cookie set, it always shout out "Empty, empty!!!". 8)

Posted: Tue Jul 19, 2005 10:40 pm
by infolock
if ur having troubles making cookies work, why not use sessions?


secondly, are you sure you need to use $_REQUEST
anyways, try printing the request and cookie arrays once you define them..

Code: Select all

$code = $_REQUEST['code'];
$qty = $_REQUEST['txtNo'];
echo '<pre>';
print_r($_REQUEST);
echo '</pre>';
setcookie("cart[$code]",$qty,time()+3600,"/");
echo '<pre>';
print_r($_COOKIE);
echo '</pre>';
if there is data in the cookie array, then your settings are preventing the cookie to be dumped.

lastly, try reading the posts on the bottom of php.net's site. you'll usually find a post relating to the same issue you are having and solve it quite easily..

http://www.php.net/setcookie

Posted: Tue Jul 19, 2005 11:48 pm
by Phoenixheart
Yeah I know it will be quite simple changing to $_SESSION when developing. But you know this problem occurs after my web finished, and it's too complex to change now.
---
PLEASE NOTE AGAIN:
Cookies havenot been set, no relations with any varibles. As I said, a simple setcookie("cart","cart_values") doesn't work!
My opinion is, maybe there's a problem from server?

Posted: Wed Jul 20, 2005 9:11 am
by infolock
please try what i said, and post the results.

Posted: Wed Jul 20, 2005 10:19 am
by patrikG
Which browser were you using to check on cookies?

IE6 may need a 3rd Party Policy if the user has set the Security Level is set to "medium" (which is default). Hence, your cookies might work in Firefox and others, but not IE.

IE6 also retains short-term cookies (usually, but not always session-cookies) in memory rather than in the "temporary internet files" directory.

IE6 also likes to be particularly picky about expiry-times etc.

Posted: Wed Jul 20, 2005 10:21 am
by theda
You may have an outdated version of PHP that doesn't support $_REQUEST, therefore, you might be able to get around this problem by using the old version "$HTTP_GET_VARS."

Posted: Fri Jul 22, 2005 3:55 am
by Phoenixheart
I checked this problem on some other computers in an Internet Service, and my cookies did work well ==> The problem comes from my company's local network.
But again, I don't know how to deal with a network!

Posted: Tue Jul 26, 2005 1:35 am
by Phoenixheart
I've noticed that:

Code: Select all

setcookie("a_name","a_value"); //WORKS WELL
setcookie("a_name","a_value",73018643); //3rd parameter is a long - WORKS WELL
But if you put whatever other than a long (time(), $duration) it doesnot work, and cookie will not be created

Code: Select all

setcookie("a_name","a_value",time()+3600); //DOESN'T WORK!
setcookie("a_name","a_value",time()+24*60*60); //DOESN'T WORK!
$duration = 73018643;
setcookie("a_name","a_value",$duration); //DOESN'T WORK!
setcookie("a_name","a_value",(int)$duration); //DOESN'T WORK!
setcookie("a_name","a_value",(float)$duration); //DOESN'T WORK!
Any ideas?

Posted: Tue Jul 26, 2005 7:29 am
by theda
Nope, not really, but I guess you can get around the retarded cookie problem by just doing the math before hand and just inputting it manually... I've had something <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> me off similar. (The fact that maybe 1 out of every 3 IE viewers simply can't view my website because it won't save my cookies...)

Posted: Tue Jul 26, 2005 7:31 am
by timvw
Well, if they use a b0rken browser, it's up to them to fix their problems...

Posted: Tue Jul 26, 2005 7:35 am
by theda
Yeah, but I can't really change that now can I? ^_^;; I have a question about sessions, I think I'll ask the question in a new thread though.

Posted: Tue Aug 02, 2005 3:36 am
by Phoenixheart
I found out the problem... Sorry, too busy to post it earlier.
That is, something wrong with your system or server's clock will cause setcookie() doesn't work with a short-range cookie. Maybe the cookie will be deleted right after created.
Now, if you set a cookie with time()+7*24*3600 (expires after one week), it will be OK. On the other hand, about one or two days life-time of the cookie may not function.
Hope this will help you!