Web Analytics Made Easy -
StatCounter cookie not setting correctly - CodingForum

Announcement

Collapse
No announcement yet.

cookie not setting correctly

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • cookie not setting correctly

    Ok, i'm trying to creat a cookie with these paramaters

    setcookie("cookieid", "$random", time()+86400, '/', 'localhost');

    the thing is, it doesn't work but if i put it in like this:

    setcookie("cookieid", "$random", time()+86400, '/');

    it works fine and sets the host as localhost EXACTLY as I've spelt it, whats going on here?

  • #2
    setcookie("cookieid", "$random", time()+86400, '/');

    You're saving "cookieid" as "$random" (with the dollar sign), not the variable.
    ... by putting the variable in quotes.

    Try this:

    setcookie("cookieid", $random, time()+86400, '/');


    .
    Last edited by mlseim; Oct 11, 2006, 09:50 AM.

    Comment


    • #3
      Because the variable $random is enclosed in double-quotes, it gets parsed and replaced with its value. If they were single-quotes, it probably would not work.

      The reason that the cookie does not work when the domain parameter is set to 'localhost' is that localhost is not a domain. It is a WINS name that resolves to the 127.0.0.1 loop back IP address, referring to the local computer.
      Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

      Comment


      • #4
        first, I dont belive this works:
        setcookie("cookieid", $random, time()+86400, '/');

        as value has to be a string and sending in a number will produce an error, second:

        If i don't set the cookies domain, when I check it, it responds with a domain of localhost

        Any ideas?

        Comment


        • #5
          Originally posted by netscape cookie spec
          Only hosts within the specified domain can set a cookie for a domain and domains must have at least two (2) or three (3) periods in them to prevent domains of the form: ".com", ".edu", and "va.us"
          why do you need to specify the domain anyway? if it defaults to the correct thing, then there's no problem, surely?
          My thoughts on some things: http://codemeetsmusic.com
          And my scrapbook of cool things: http://gjones.tumblr.com

          Comment


          • #6
            The version without any quotes surrounding $random works (just verified) because PHP always does a good job of converting things to strings when a string is needed.

            The localhost/ value that you see in the cookie file is (from the PHP manual) "The path on the server in which the cookie will be available on." This does not have anything to do with a domain, which localhost is not.
            Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

            Comment


            • #7
              I dont know if you read that wrong but thats not totaly correct. you can setup the domain because if you check the php manual right at the top it says that you should set the domain to .yourdomain.com without the www that way it works on all subdomains (www and others) of your website.

              the path is what folders it works in, if you select / for your path, it works everywhere on your website.

              and localhost is the domain because if you're running the file locally what else could be the domain, it cant be blank or else it would never be used.

              Comment


              • #8
                Originally posted by GJay View Post
                why do you need to specify the domain anyway? if it defaults to the correct thing, then there's no problem, surely?
                so it can work with sub domains. for example if the cookie is set at www.yourdomain.com it wont work at images.yourdomain.com

                Comment


                • #9
                  Originally posted by CFMaBiSmAd View Post
                  The localhost/ value that you see in the cookie file is (from the PHP manual) "The path on the server in which the cookie will be available on." This does not have anything to do with a domain, which localhost is not.
                  You're quoting the wrong bit of the manual.
                  path is the 4th argument, domain is the 5th.

                  The part of the cookie-spec I posted wasn't for fun, it's telling you how the domain argument works.

                  The easiest solution would be to either get some web-hosting, and develop there, or learn how to setup vhosts in apache, so that you can point a domain-name of your choosing at localhost.
                  My thoughts on some things: http://codemeetsmusic.com
                  And my scrapbook of cool things: http://gjones.tumblr.com

                  Comment


                  • #10
                    No I quoted the part I intended, because - Skinner927 stated in his 1st post that - "sets the host as localhost EXACTLY" The only place he could be getting this statement from is looking in the actual cookie file, where when he used the second form that worked, with the 4th parameter of '/', that this sets the path to localhost/ and is somehow confusing this to mean that the localhost portion of this is a domain, which he has been told it is not.

                    I quoted the path parameter definition as a possible reason why someone might attempt to interpret what they saw as something that it is not.

                    Edit: Let me clarify a little more. Doing the following using http://localhost/somefile.php -
                    PHP Code:
                    $random 123;
                    setcookie("cookieid""$random"time()+86400'/'); 
                    Causes a cookie file with the following -
                    Code:
                    cookieid
                    123
                    localhost/
                    1024
                    3588502400
                    29814350
                    2885208896
                    29814149
                    *
                    Someone could interpret the path portion of this to reinforce the idea that localhost is a domain.
                    Last edited by CFMaBiSmAd; Oct 12, 2006, 04:50 AM.
                    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

                    Comment


                    • #11
                      Ok, I think i understand what you're saying now. I just wont deal with it until this goes live. I'm currently developing it on my computer instead of the server because its easier. i'll deal with the cookies at a later time.

                      thanks for the help.

                      Comment

                      Working...
                      X