setting cookie before leaving page

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

    #1

    setting cookie before leaving page

    Hi all ...
    I've been learning and using PHP for a couple months now,
    working on a couple of somewhat different sites.

    On one site, I have a page with a form,
    and that form includes a couple of ACTION=SUBMIT buttons,
    so that clicking either button will immediately reload the page
    only with an argument tacked on, e.g. "?size=128" or "?size=256" .
    This causes some images to be resized,
    but otherwise the page appears the same.

    I would like to save choice made by clicking either of those two buttons
    by setting a cookie on the visitor's computer.
    (At least, I *think* that's how I should do it.)
    I can put some code at the tippy-top of the page
    that looks at the incoming $_GET[] arg and call setcookie() accordingly.
    This really seems like it ought to do the job.
    However, what I'm finding is that the cookie does not get set
    unless and until the visitor clicks REFRESH in the browser
    (or does anything equivalent);
    if they don't think to do that, the cookie remains obsolete.

    Are my expectations out of whack?
    If not, any likely ideas re what I'm doing wrong?
    Oh, fine, here's the code:

    <?php
    $size_str_fm_ge t= $_GET["size"];
    $time= time()+60*60*24 *30;
    if ( strcmp( $size_str_fm_ge t, "small" ) == 0 )
    setcookie("size ", "small", $time);
    elseif ( strcmp( $size_str_fm_ge t, "large" ) == 0 )
    setcookie("size ", "large", $time);
    ?>

    (The slightly strange conditional is so that if the value of the $_GET[] arg
    is neither of the expected values, the cookie is not set at all.)
    (And, yes, this code is at the very top of the file, *before* any HTML code.)

    Thanks!
  • Mike Placentra II

    #2
    Re: setting cookie before leaving page

    On Jan 16, 10:16 am, MangroveRoot <zcukpk...@snea kemail.comwrote :
    Are my expectations out of whack?
    If not, any likely ideas re what I'm doing wrong?
    Are you sure the cookie is only set after a refresh? With what
    browser(s) does this happen? And have you tried inspecting the
    response headers? (IE with the Live HTTP Headers extension for
    Firefox)

    -Michael Placentra II | PHP5 ZCE

    Comment

    • =?ISO-8859-1?Q?thib=B4?=

      #3
      Re: setting cookie before leaving page

      MangroveRoot wrote:
      However, what I'm finding is that the cookie does not get set
      unless and until the visitor clicks REFRESH in the browser
      (or does anything equivalent);
      This is absolutely normal. From the man':
      "Cookies will not become visible until the next loading of a page that the
      cookie should be visible for."
      if they don't think to do that, the cookie remains obsolete.
      Well do it for them; after setcookie(), just do a header('Locatio n: ' .
      $_SERVER['PHP_SELF']) and, of course, a die().

      Basically, this isn't necessary since you've got the date you need in the
      $_GET. However, the refreshing method might be a better solution to avoid
      the user to be prompted by his browser to resend the post data if he reloads
      the page manually, for whatever reason.

      Read about the PRG design pattern.



      -thib“

      Comment

      • Mike Placentra II

        #4
        Re: setting cookie before leaving page

        On Jan 17, 2:06 pm, thib“ <th...@coralsna ke-team.comwrote:
        This is absolutely normal. From the man':
        "Cookies will not become visible until the next loading of a page that the
        cookie should be visible for."
        Heh...I really over-thought this one. I thought they meant literally
        that the cookie is not being set.

        Please disregard my post.

        -Mike Placentra II | ZCE

        Comment

        Working...