By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,643 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

setcookie() returns true but no cookie appears.

P: 7
I have two hosting accounts. On one, my setcookie script works fine; cookies are created. On the other, the same script doesn't work. The function returns TRUE, but no cookies is created.

I checked to make sure that the variables have values and that the proper expire time is set and etc.

Any ideas?

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     foreach($_POST as $name => $value){
  3.         if($name == 'hItem'){
  4.             $item = $value;
  5.         }
  6.         else if($name == 'hPrice'){
  7.             $price = $value;
  8.         }
  9.         else if($name == 'hQuantity'){
  10.             $quantity = $value;
  11.         }
  12.         else if($name == 'wQuantity'){
  13.             $wantedQuantity = $value;
  14.         }    
  15.         else if($name == 'hImage'){
  16.             $image = $value;
  17.         }
  18.         else if($name == 'hShipping'){
  19.             $shipping = $value;
  20.         }
  21.         else if($name == 'hCategory'){
  22.             $category = $value;            
  23.         }                            
  24.     }
  25.  
  26.     if($quantity <= 0){
  27.         echo "This item is no longer available. Please select another item!";
  28.     }
  29.     else{
  30.         if($wantedQuantity > $quantity){
  31.             $wantedQuantity = $quantity;
  32.         }
  33.  
  34.         $i = 0;
  35.         while($i < 1000){
  36.             if(isset($_COOKIE['cItem' . $i])){
  37.                 if($_COOKIE['cItem' . $i] == $item){
  38.                     break;
  39.                 }
  40.             }
  41.             else
  42.                 break;
  43.             $i++;
  44.         }
  45.  
  46.         setcookie("cItem" . $i, $item, time() + 3600);
  47.         setcookie("cPrice" . $i, $price, time() + 3600);
  48.         setcookie("cQuantity" . $i, $wantedQuantity, time() + 3600);
  49.         setcookie("cImage" . $i, $image, time() + 3600);
  50.         setcookie("cShipping" . $i, $shipping, time() + 3600);
  51.         setcookie("cCategory" . $i, $category, time() + 3600);
  52.  
  53.         $link ="<script>document.location.href = 'viewcart_t.php'</script>";
  54.         echo $link;
  55.     }
  56. ?>
Sep 25 '08 #1
Share this Question
Share on Google+
8 Replies


Markus
Expert 5K+
P: 6,050
I'm sorry, I couldn't find a question in your post?
Sep 25 '08 #2

P: 7
Question is, do you see anything wrong with my script that wouldn't create a cookie? As I said, the same exact script works on the other account. Same browser is used, IE7. Variables, Time, and return values are all good.

Could it possibly be a setting on the account or server that's preventing the cookie to be created?
Sep 25 '08 #3

Markus
Expert 5K+
P: 6,050
Question is, do you see anything wrong with my script that wouldn't create a cookie? As I said, the same exact script works on the other account. Same browser is used, IE7. Variables, Time, and return values are all good.

Could it possibly be a setting on the account or server that's preventing the cookie to be created?
Ah.

Well, if you're saying that the scripts is *exactly* the same and the only variable in this is the server it is hosted on, then, yes, it must be the server causing a problem.

Someone else may be able to shed some light on this.

Do the php versions on the servers differ?
Sep 25 '08 #4

Atli
Expert 5K+
P: 5,058
Hi.

Are there any differences in the two servers?
Different PHP versions or HTTP servers?
Are the URL's used by them different?

You could try setting the path and domain parameters of the setcookie function.

And just a suggestion.
You really should remove the JavaScript redirect in lines 53 and 54 and just set the Location header via the header function.
Expand|Select|Wrap|Line Numbers
  1. header("Location: viewcart_t.php");
Much cleaner and won't fail in browsers that don't allow JavaScript.
Sep 25 '08 #5

P: 7
The server is the same and the accounts have the same settings. It seems the problems is fixed now.


This behaviour was very odd and maybe somebody can explain why.

1. I uploaded all the files from one domain to the other domain. The cookies worked on the orginal domain, but not the new one.
2. I tried clearing the cookies, temp internet files, closing browsers completley, and etc. Still didn't work on the other domain.
3. I then tried it in firefox. It worked in there oddly enough. It seems IE7 was giving an issue for the other domain...even though the same scripts worked from the other site in Firefox and IE lol.
4. I gave my computer a reboot and now everything works fine.

So, just out of curiosity, why would have one site worked in both browsers and not the other if they are the same files, same server, same account settings. Maybe some memory issue with IE7, not sure really.
Sep 26 '08 #6

Atli
Expert 5K+
P: 5,058
Sounds like a caching problem in IE. Maybe an identical set of cookies from another domain confused it or something.

Well it's IE... what do you expect :P

I'm pretty sure it's wasn't a problem with your server, seeing as it working in FF and then in IE once the client computer was rebooted.
Sep 26 '08 #7

P: 7
Okay lol, I found the problem and I must apologize to IE7 lol.

Hopefully this will help other people down the road. I was looking more closely at my backup script and I now realize that the script was slight different.

I was using time() + 846400 on the old script and time() + 3600 on the script that had the problem.

Now the issue is, I didn't take into account that the server is located in PST and I'm in EST. Therefore 1 hour [3600] was not sutiable for a 3 hour time difference. I was basically setting the cookie to delete.

Why was it working when I rebooted? Well my PC is on it's last legs and I have to reboot it every 2 or 3 days. When I do, the time gets set back a few hours. So yeah lol. I'm glad I figured it all out though.

Thanks for helping guys. Sometimes it just helps to talk stuff out.
Sep 27 '08 #8

Atli
Expert 5K+
P: 5,058
Ahh ok. That I wouldn't have predicted :)

Glad you solved the problem tho.
Sep 27 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.