setcookie() returns true but no cookie appears.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SupraFast
    New Member
    • Sep 2008
    • 7

    #1

    setcookie() returns true but no cookie appears.

    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?

    Code:
    <?php
    	foreach($_POST as $name => $value){
    		if($name == 'hItem'){
    			$item = $value;
    		}
    		else if($name == 'hPrice'){
    			$price = $value;
    		}
    		else if($name == 'hQuantity'){
    			$quantity = $value;
    		}
    		else if($name == 'wQuantity'){
    			$wantedQuantity = $value;
    		}	
    		else if($name == 'hImage'){
    			$image = $value;
    		}
    		else if($name == 'hShipping'){
    			$shipping = $value;
    		}
    		else if($name == 'hCategory'){
    			$category = $value;			
    		}							
    	}
    	
    	if($quantity <= 0){
    		echo "This item is no longer available. Please select another item!";
    	}
    	else{
    		if($wantedQuantity > $quantity){
    			$wantedQuantity = $quantity;
    		}
    		
    		$i = 0;
    		while($i < 1000){
    			if(isset($_COOKIE['cItem' . $i])){
    				if($_COOKIE['cItem' . $i] == $item){
    					break;
    				}
    			}
    			else
    				break;
    			$i++;
    		}
    	
    		setcookie("cItem" . $i, $item, time() + 3600);
    		setcookie("cPrice" . $i, $price, time() + 3600);
    		setcookie("cQuantity" . $i, $wantedQuantity, time() + 3600);
    		setcookie("cImage" . $i, $image, time() + 3600);
    		setcookie("cShipping" . $i, $shipping, time() + 3600);
    		setcookie("cCategory" . $i, $category, time() + 3600);
    		
    		$link ="<script>document.location.href = 'viewcart_t.php'</script>";
    		echo $link;
    	}
    ?>
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    I'm sorry, I couldn't find a question in your post?

    Comment

    • SupraFast
      New Member
      • Sep 2008
      • 7

      #3
      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?

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by SupraFast
        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?

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          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.
          [code=php]header("Locatio n: viewcart_t.php" );[/code]
          Much cleaner and won't fail in browsers that don't allow JavaScript.

          Comment

          • SupraFast
            New Member
            • Sep 2008
            • 7

            #6
            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.

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              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.

              Comment

              • SupraFast
                New Member
                • Sep 2008
                • 7

                #8
                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.

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  Ahh ok. That I wouldn't have predicted :)

                  Glad you solved the problem tho.

                  Comment

                  Working...