setcookie issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GregoryD

    setcookie issue

    I'm going to try to simplify this issue as much as possible.

    In my public_html/ directory, I have a login script that sets a cookie to be
    used for authentication purposes. To set the cookie, it has something like
    this:

    <?php
    setcookie('code ',$value, time + $lifetime, '/', '.example.com') ;
    ?>

    This, to my knowledge, is supposed to set a cookie named code with a value
    of $value and an expiration date of time + $lifetime. Additionally, it is
    to be read in public_html and all subdirectories over the entire domain of
    *.example.com

    Yet if I'm trying to get the value of the cookie, as in:

    <?php
    $cookie = $_COOKIE['code'];
    echo $cookie;
    ?>

    in a subdirectory, say public_html/auth/, the result of the second script is
    nothing. If I put that same script in the public_html/ directory, it'll
    print out my $value.

    I'm using php 4.3

    Any thoughts?

    GregoryD


  • Jerry Stuckle

    #2
    Re: setcookie issue

    GregoryD wrote:
    I'm going to try to simplify this issue as much as possible.
    >
    In my public_html/ directory, I have a login script that sets a cookie to be
    used for authentication purposes. To set the cookie, it has something like
    this:
    >
    <?php
    setcookie('code ',$value, time + $lifetime, '/', '.example.com') ;
    ?>
    >
    This, to my knowledge, is supposed to set a cookie named code with a value
    of $value and an expiration date of time + $lifetime. Additionally, it is
    to be read in public_html and all subdirectories over the entire domain of
    *.example.com
    >
    Yet if I'm trying to get the value of the cookie, as in:
    >
    <?php
    $cookie = $_COOKIE['code'];
    echo $cookie;
    ?>
    >
    in a subdirectory, say public_html/auth/, the result of the second script is
    nothing. If I put that same script in the public_html/ directory, it'll
    print out my $value.
    >
    I'm using php 4.3
    >
    Any thoughts?
    >
    GregoryD
    >
    >
    Which browser are you using, and what are the security settings?
    Cookies have to be received from the browser before your code can access
    them.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Andy Hassall

      #3
      Re: setcookie issue

      On Sun, 8 Oct 2006 14:19:38 -0500, "GregoryD" <deergregoryd@g mail.comwrote:
      >I'm going to try to simplify this issue as much as possible.
      >
      >In my public_html/ directory, I have a login script that sets a cookie to be
      >used for authentication purposes. To set the cookie, it has something like
      >this:
      >
      ><?php
      >setcookie('cod e',$value, time + $lifetime, '/', '.example.com') ;
      >?>
      That would raise a warning "use of undefined constant 'time', assumed string"
      if you have PHP error reporting set to a sensible level, and your expiry time
      is likely to be way in the past - "time" should be "time()". But that might
      just be a typo in your post.
      >This, to my knowledge, is supposed to set a cookie named code with a value
      >of $value and an expiration date of time + $lifetime. Additionally, it is
      >to be read in public_html and all subdirectories over the entire domain of
      >*.example.co m
      >
      >Yet if I'm trying to get the value of the cookie, as in:
      >
      ><?php
      $cookie = $_COOKIE['code'];
      echo $cookie;
      >?>
      >
      >in a subdirectory, say public_html/auth/, the result of the second script is
      >nothing. If I put that same script in the public_html/ directory, it'll
      >print out my $value.
      >
      >I'm using php 4.3
      >
      >Any thoughts?
      Does the browser already have a more specific cookie set with the same name
      but for the /auth directory? If so, your first setcookie won't override it, and
      this other cookie will be the one that's read.

      Try clearing all cookies for the domain and trying again, and also using a
      cookie viewer tool to see what you actually have set (e.g.
      http://addneditcookies.mozdev.org/) and/or an HTTP headers viewer to see what
      cookies the browser is really sending (http://livehttpheaders.mozdev.org/ or
      http://www.fiddlertool.com/fiddler/).

      --
      Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
      http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

      Comment

      • GregoryD

        #4
        Re: setcookie issue


        "Jerry Stuckle" <jstucklex@attg lobal.netwrote in message
        news:WoCdnd7D8L w-z7TYnZ2dnUVZ_qe dnZ2d@comcast.c om...
        GregoryD wrote:
        >I'm going to try to simplify this issue as much as possible.
        >>
        >In my public_html/ directory, I have a login script that sets a cookie to
        >be used for authentication purposes. To set the cookie, it has something
        >like this:
        >>
        ><?php
        >setcookie('cod e',$value, time + $lifetime, '/', '.example.com') ;
        >?>
        >>
        >This, to my knowledge, is supposed to set a cookie named code with a
        >value of $value and an expiration date of time + $lifetime.
        >Additionally , it is to be read in public_html and all subdirectories over
        >the entire domain of *.example.com
        >>
        >Yet if I'm trying to get the value of the cookie, as in:
        >>
        ><?php
        > $cookie = $_COOKIE['code'];
        > echo $cookie;
        >?>
        >>
        >in a subdirectory, say public_html/auth/, the result of the second script
        >is nothing. If I put that same script in the public_html/ directory,
        >it'll print out my $value.
        >>
        >I'm using php 4.3
        >>
        >Any thoughts?
        >>
        >GregoryD
        >>
        >>
        >
        Which browser are you using, and what are the security settings? Cookies
        have to be received from the browser before your code can access them.
        I've tried this on firefox 1.5, ie 6, and Opera 9, and all my security
        permissions in the browsers are set to accept cookies. And yeah, I know
        that cookies have to be received by the browser, which is why I specifically
        pointed out that the code to receive the value in the cookie is in a second
        script. One page has the setcookie, the second page (after a header
        redirect) authenticates it. Sorry if that was a bit confusing.

        GregoryD


        Comment

        • Snef

          #5
          Re: setcookie issue (solution!)



          GregoryD wrote:
          "Jerry Stuckle" <jstucklex@attg lobal.netwrote in message
          news:WoCdnd7D8L w-z7TYnZ2dnUVZ_qe dnZ2d@comcast.c om...
          >GregoryD wrote:
          >>I'm going to try to simplify this issue as much as possible.
          >>>
          >>In my public_html/ directory, I have a login script that sets a cookie to
          >>be used for authentication purposes. To set the cookie, it has something
          >>like this:
          >>>
          >><?php
          >>setcookie('co de',$value, time + $lifetime, '/', '.example.com') ;
          >>?>
          >>>
          >>This, to my knowledge, is supposed to set a cookie named code with a
          >>value of $value and an expiration date of time + $lifetime.
          >>Additionall y, it is to be read in public_html and all subdirectories over
          >>the entire domain of *.example.com
          >>>
          >>Yet if I'm trying to get the value of the cookie, as in:
          >>>
          >><?php
          >> $cookie = $_COOKIE['code'];
          >> echo $cookie;
          >>?>
          >>>
          >>in a subdirectory, say public_html/auth/, the result of the second script
          >>is nothing. If I put that same script in the public_html/ directory,
          >>it'll print out my $value.
          >>>
          >>I'm using php 4.3
          >>>
          >>Any thoughts?
          >>>
          >>GregoryD
          >>>
          >>>
          >Which browser are you using, and what are the security settings? Cookies
          >have to be received from the browser before your code can access them.
          >
          I've tried this on firefox 1.5, ie 6, and Opera 9, and all my security
          permissions in the browsers are set to accept cookies. And yeah, I know
          that cookies have to be received by the browser, which is why I specifically
          pointed out that the code to receive the value in the cookie is in a second
          script. One page has the setcookie, the second page (after a header
          redirect) authenticates it. Sorry if that was a bit confusing.
          >
          GregoryD
          >
          >
          Hi,

          When you use a header redirect, the just set cookie will not be known in the
          redirect page.
          You have to use another way to set the cookie before the redirect. Use this
          little function to set the cookie and it will work!

          function set_cookie($nam e, $value = '', $expires = 0, $path = '', $domain = '',
          $secure = false, $http_only = false)
          {
          header('Set-Cookie: ' . rawurlencode($n ame) . '=' . rawurlencode($v alue)
          . (empty($expires ) ? '' : '; expires=' . gmdate('D,
          d-M-Y H:i:s \\G\\M\\T', $expires))
          . (empty($path) ? '' : '; path=' . $path)
          . (empty($domain) ? '' : '; domain=' . $domain)
          . (!$secure ? '' : '; secure')
          . (!$http_only ? '' : '; HttpOnly'), false);
          }

          Comment

          • Snef

            #6
            Re: setcookie issue



            GregoryD wrote:
            "Jerry Stuckle" <jstucklex@attg lobal.netwrote in message
            news:WoCdnd7D8L w-z7TYnZ2dnUVZ_qe dnZ2d@comcast.c om...
            >GregoryD wrote:
            >>I'm going to try to simplify this issue as much as possible.
            >>>
            >>In my public_html/ directory, I have a login script that sets a cookie to
            >>be used for authentication purposes. To set the cookie, it has something
            >>like this:
            >>>
            >><?php
            >>setcookie('co de',$value, time + $lifetime, '/', '.example.com') ;
            >>?>
            >>>
            >>This, to my knowledge, is supposed to set a cookie named code with a
            >>value of $value and an expiration date of time + $lifetime.
            >>Additionall y, it is to be read in public_html and all subdirectories over
            >>the entire domain of *.example.com
            >>>
            >>Yet if I'm trying to get the value of the cookie, as in:
            >>>
            >><?php
            >> $cookie = $_COOKIE['code'];
            >> echo $cookie;
            >>?>
            >>>
            >>in a subdirectory, say public_html/auth/, the result of the second script
            >>is nothing. If I put that same script in the public_html/ directory,
            >>it'll print out my $value.
            >>>
            >>I'm using php 4.3
            >>>
            >>Any thoughts?
            >>>
            >>GregoryD
            >>>
            >>>
            >Which browser are you using, and what are the security settings? Cookies
            >have to be received from the browser before your code can access them.
            >
            I've tried this on firefox 1.5, ie 6, and Opera 9, and all my security
            permissions in the browsers are set to accept cookies. And yeah, I know
            that cookies have to be received by the browser, which is why I specifically
            pointed out that the code to receive the value in the cookie is in a second
            script. One page has the setcookie, the second page (after a header
            redirect) authenticates it. Sorry if that was a bit confusing.
            >
            GregoryD
            >
            >
            PS... found this solution is the user comments at php.net in the setcookie part! ;)

            Comment

            • GregoryD

              #7
              Re: setcookie issue


              "Snef" <s.franke@snefi t.comwrote in message
              news:9b1c0$4529 72f6$3ec24187$2 1889@news.chell o.nl...
              >
              >
              GregoryD wrote:
              >"Jerry Stuckle" <jstucklex@attg lobal.netwrote in message
              >news:WoCdnd7D8 Lw-z7TYnZ2dnUVZ_qe dnZ2d@comcast.c om...
              >>GregoryD wrote:
              >>>I'm going to try to simplify this issue as much as possible.
              >>>>
              >>>In my public_html/ directory, I have a login script that sets a cookie
              >>>to be used for authentication purposes. To set the cookie, it has
              >>>something like this:
              >>>>
              >>><?php
              >>>setcookie('c ode',$value, time + $lifetime, '/', '.example.com') ;
              >>>?>
              >>>>
              >>>This, to my knowledge, is supposed to set a cookie named code with a
              >>>value of $value and an expiration date of time + $lifetime.
              >>>Additionally , it is to be read in public_html and all subdirectories
              >>>over the entire domain of *.example.com
              >>>>
              >>>Yet if I'm trying to get the value of the cookie, as in:
              >>>>
              >>><?php
              >>> $cookie = $_COOKIE['code'];
              >>> echo $cookie;
              >>>?>
              >>>>
              >>>in a subdirectory, say public_html/auth/, the result of the second
              >>>script is nothing. If I put that same script in the public_html/
              >>>directory, it'll print out my $value.
              >>>>
              >>>I'm using php 4.3
              >>>>
              >>>Any thoughts?
              >>>>
              >>>GregoryD
              >>>>
              >>>>
              >>Which browser are you using, and what are the security settings? Cookies
              >>have to be received from the browser before your code can access them.
              >>
              >I've tried this on firefox 1.5, ie 6, and Opera 9, and all my security
              >permissions in the browsers are set to accept cookies. And yeah, I know
              >that cookies have to be received by the browser, which is why I
              >specifically pointed out that the code to receive the value in the cookie
              >is in a second script. One page has the setcookie, the second page
              >(after a header redirect) authenticates it. Sorry if that was a bit
              >confusing.
              >>
              >GregoryD
              PS... found this solution is the user comments at php.net in the setcookie
              part! ;)
              Thanks, I'll give it a try tomorrow.

              GregoryD


              Comment

              Working...