Re: Cookie Trouble: getting the information back out...

From: Date: Wed, 26 Mar 2008 04:54:25 +0000
Subject: Re: Cookie Trouble: getting the information back out...
References: 1 2 3 4 5 6 7  Groups: php.general 
Request: Send a blank email to php-general+get-272106@lists.php.net to get a copy of this message
Jim Lucas wrote:
Mark Weaver wrote:
Andrew Ballard wrote:
On Tue, Mar 25, 2008 at 9:59 PM, Mark Weaver <mdw1982@mdw1982.com> wrote:
Thank you Andrew... Now it all makes perfect sense. Good grief! there's so much to learn. It seems that Java was easier. ;)
That's not specific to PHP. It's just how http works, so it's the same for ASP, Perl, I suspect Java and most (if not all) other languages. There might be a language that sets a cookie when you assign a value to a special cookie variable, but I'm not familiar with any. Andrew
Unless I was doing something differently when I originally wrote this in PERL I don't recall having this issue. At that time I would set the cookie and then redirect (load the index with the full menu) if cookie existed. Geez! now my $_SESSION isn't persisting to the next page when the screen refreshes. The only thing preventing me from gouging out my eyes right now is that I know I'll get this stuff. It's just a matter of time...
The "problem" that you are encountering is because the $_COOKIE array is "populated" when the script is executed. More then likely the other languages that you used, would allow you to set a cookie and then they would enter them into the "global" array for you, and not make you wait until the next page load. You could accomplish this yourself by making a wrapper function for the setcookie() function and have your function set the data using setcookie() and having it enter the data directly into the $_COOKIE array. Something like this should do the trick <?php /* bool setcookie ( string $name
              [, string $value
              [, int $expire
              [, string $path
              [, string $domain
              [, bool $secure
              [, bool $httponly  ]]]]]] )
*/ function mySetCookie($name,
                     $value=null,
                     $expire=0,
                     $path='/',
                     $domain=null,
                     $secure=FALSE,
                     $httponly=FALSE) {
    if ( is_null($domain) )
        $domain = $_SERVER['SERVER_NAME'];
    if ( setcookie( $name, $value, $expire,
                        $path, $domain, $secure, $httponly) ) {
        $_COOKIE[$name] = $value;
        return true;
    }
    return false;
} ?>
Wow! very sweet!! Thank you Jim. I'm getting my brain good and wrinkled today. -- Mark ------------------------- the rule of law is good, however the rule of tyrants just plain sucks! Real Tax Reform begins with getting rid of the IRS. ============================================== Powered by CentOS5 (RHEL5)

« previous php.general (#272106) next »