This is a discussion on Cookie Trouble: getting the information back out... within the PHP General forums, part of the PHP Programming Forums category; Jim Lucas wrote: > Mark Weaver wrote: >> Andrew Ballard wrote: >>> On Tue, Mar 25, 2008 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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) |
|
|||
|
Mark:
You said: > I'm gonna shit and go blind cause I haven't got a clue... and > The only thing preventing me from gouging out my eyes right now is ... Are you sure that programming is right for you? It sounds like you're going to hurt yourself. This was just a cookie. :-) Cheers, tedd -- ------- http://sperling.com http://ancientstones.com http://earthstones.com |
|
|||
|
tedd wrote:
> Mark: > > You said: > >> I'm gonna shit and go blind cause I haven't got a clue... > > and > >> The only thing preventing me from gouging out my eyes right now is ... > > Are you sure that programming is right for you? > > It sounds like you're going to hurt yourself. This was just a cookie. :-) > > Cheers, > > tedd There's an old proverb that basically says that if you present a mule with two choices, (1) a easy, meandering path up the side of a mountain that triples the time it would take to get to the top, and (2) a brutally hard path that goes straight up the mountain, but would most certainly have a good chance of killing the mule if taken, the mule will take path number 2 each and every time. It's the mule in me! :) I can't help myself. It's like sitting a pair of shoes down in front of a leprechaun; he can't resist the compulsion the shine and clean those shoes. I can't resist the compulsion to solve a problem by coding a solution for it. I really enjoy programming. It satisfies a creative bent in me, but from time to time I do get very frustrated with it. Especially when, as in this case, it's only a cookie and an easy concept. What frustrates me is I know I'm missing something, but for the life of me I can't see it. Therefore the shoe that I'm compelled to clean and shine keeps dipping itself back into the mud. For me moving from procedural PERL programming to OOP PHP feels like a paradigm shift! some of it coming back easily and some of it not so easily. Ya know... old dog new tricks... that sort of thing. But if I don't challenge myself and learn new things I could run the risk of getting stuck in a rut of thinking the same way about things and well... never mind... shit! more mud on that shoe again. :) -- 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) |
|
|||
|
At 8:37 AM -0400 3/26/08, Mark Weaver wrote:
>I really enjoy programming. It satisfies a creative bent in me, but >from time to time I do get very frustrated with it. Especially when, >as in this case, it's only a cookie and an easy concept. What >frustrates me is I know I'm missing something, but for the life of >me I can't see it. Therefore the shoe that I'm compelled to clean >and shine keeps dipping itself back into the mud. Well, if it's any solace to you I remember facing the same problem and finally resorted to a refresh. But it did slow me down a bit. Cheers, tedd -- ------- http://sperling.com http://ancientstones.com http://earthstones.com |
|
|||
|
On Tue, March 25, 2008 8:11 pm, Mark Weaver wrote:
> I suspect I already know part of the answer to this, but I'm not sure > which way to go with it. I've got a project I'm working on and one of > the things it's got to do is set cookies and then read them later. > When > the app was first written I was doing everything in PERL and cookies > are > fairly straight-forward, however I'm finding cookies in PHP somewhat > problematic. > > Setting the cookie is a snap, however getting the info back out is, > well... problematic. > > this is basically what I'm doing, what I'm seeing in the cookie, and > what I'm getting back out. > > Setting the cookie > ========================== > $values = "blah|blah|blah"; > setcookie("cookiename", $values, time()+$timevalue); Because IE engineers CANNOT READ a technical document to save their lives, you MUST supply a "path" if you supply a "time": setcookie("cookiename", $values, time() + $timevalue, "/"); You also don't tell use what $timevalue is, so that could be something very wrong... :-) > Inside the Cookie > ========================== > Content: blah%7Cblah%7Cblah > > > Getting info Out Of Cookie > ========================== > list($first,$second,$third) = explode("|", $values); Unless you have register_globals set to "ON" (bad!) then $values will only have meaning in the setcookie script... > Cookie Test Page > ========================== > if (isset($_COOKIE["cookiename"])){ > list($first,$second,$third) = explode('|',$_COOKIE["cookiename"]); > echo "<p>I found your cookie</p>\n"; > echo "<p>The following Values were Contained in the cookie:<BR> > Username: $first<BR> > Password: $second<BR> > Type : $third</p>\n"; You should NOT NOT NOT NOT NOT be storing a username *or* password in a cookie!!! > } > else{ > echo "<p>I wasn't able to find your cookie.</p>\n"; > } > > Now, I've constructed a cookie_test.php page to check things out and > the > strange behavior I'm seeing is, upon first execution I get the "else" > block, but if I hit the browser's reload button I get the "if" block. > At > first I thought the cookie wasn't being read at all because of weird > characters, but then upon reloading the page and seeing the "if" block > being displayed I'm thoroughly confused. It's gotta something simple > I'm > missing. What *is* in your cookies? var_dump($_COOKIES); Perhaps putting '|' in there is not a valid character? You could base64 encode it or ... > and I swear if someone tells me to RTFM I'm gonna shit and go blind > cause I haven't got a clue as to "which" part of the FM to read > concerning this. :) It would be some chunk of the Netscape Cookie spec. Google for "Netscape Cookie spec" and read that. It's only a page. -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/from/lynch Yeah, I get a buck. So? |
|
|||
|
On Tue, Mar 25, 2008 at 9:49 PM, Andrew Ballard <aballard@gmail.com> wrote:
> On Tue, Mar 25, 2008 at 9:31 PM, Daniel Brown <parasane@gmail.com> wrote: > > > > Is this block of code executed immediately after the cookie is > > set? Sometimes PHP works too fast for its own good and the client > > doesn't even realize it has a cookie yet. Try setting it with one > > page and either sleep()'ing for a bit or forcing a link-click or page > > refresh before checking for the cookie. > > Um... Cookie data ISN'T available to the same script that sets it. If > you use setcookie(), all it does is send a header to the browser > immediately ahead of the output of your script telling the browser to > store those values in either memory or on disk. The value will not > appear in the $_COOKIE array until the browser requests the next page > and includes the Cookie: header as part of the request. You're correct. I was saying basically the same thing, but re-reading it, it sure doesn't look like it in English. ;-P The sentences should've instead been rewritten like so: "Try setting it with one page and forcing a link-click or sleep()'ing for a bit and then refreshing." It wasn't meant to insinuate -- </Daniel P. Brown> Forensic Services, Senior Unix Engineer 1+ (570-) 362-0283 |
|
|||
|
On Tue, Mar 25, 2008 at 10:22 PM, Mark Weaver <mdw1982@mdw1982.com> wrote:
> > 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... Sessions are only good on the same server as which they were set. This is because the server writes the data to a file on its side, then sends just a session ID cookie to the browser. This session ID holds no information except the key to the session file on the server with which it's associated. However, if you're still on the same server, make sure that you've used session_start() at the top of every page to which you want the session to carry over. EXAMPLE: Page1 session_start() is used and the UID of the visitor is set. Page 2 session_start() is used, but no data is read/written. Page 3 session_start() is NOT used, no $_SESSION data available. Page 4 session_start() is used, and is re-initialized despite missing Page 3. -- </Daniel P. Brown> Forensic Services, Senior Unix Engineer 1+ (570-) 362-0283 |