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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

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

Post 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?
Last edited by Phoenixheart on Tue Aug 02, 2005 3:40 am, edited 1 time in total.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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?
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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.
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Post 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)
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Post 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?
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

please try what i said, and post the results.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post 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."
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Post 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!
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Post 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?
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post 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...)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Well, if they use a b0rken browser, it's up to them to fix their problems...
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post 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.
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Post 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!
Post Reply