Cookie Question

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

    Cookie Question

    I wrote a small .php script that creates a cookie on my local PC - I
    also wrote one that deletes it.

    My question is this: when the cookie has been created and I run the
    delete .php script, when it first loads it does not delete the cookie
    - I always have to click on the browser refresh button - why doesn't
    the script execute on the first execution?

    Thanks...

  • John Smith

    #2
    Re: Cookie Question

    I'd suggest you have a further read on cookies and how they relate to
    intricacies of HTTP. It can get a bit tricky ;).

    Anyway. The PHP script wouldn't 'delete' the cookie from your computer,
    it simply sends a header to your browser stating that the cookie has
    expired, and it is then up to the browser to delete that cookie from
    _then on after_.

    So usually once you have sent a cookie that will expire the current
    cookie (read: setting a cookie of the same name, but a past expiry date):

    setcookie('myco okie', '', mktime()- 31536000, $cookiepath,
    $cookiedomain, $cookiesecure);

    You will then need to instruct the browser to refresh the page, by
    sending a location header:

    //
    // Time to refresh!
    //
    header ('Location: index.php');

    Hope this helps,
    Grant.

    Ralph Freshour wrote:
    [color=blue]
    > I wrote a small .php script that creates a cookie on my local PC - I
    > also wrote one that deletes it.
    >
    > My question is this: when the cookie has been created and I run the
    > delete .php script, when it first loads it does not delete the cookie
    > - I always have to click on the browser refresh button - why doesn't
    > the script execute on the first execution?
    >
    > Thanks...
    >[/color]

    Comment

    • R. Rajesh Jeba Anbiah

      #3
      Re: Cookie Question

      Ralph Freshour <ralph@primemai l.com> wrote in message news:<luo8jv8g5 knn1cnpjvuqnp7v h2bbjhjuho@4ax. com>...[color=blue]
      > I wrote a small .php script that creates a cookie on my local PC - I
      > also wrote one that deletes it.
      >
      > My question is this: when the cookie has been created and I run the
      > delete .php script, when it first loads it does not delete the cookie
      > - I always have to click on the browser refresh button - why doesn't
      > the script execute on the first execution?[/color]



      Common Pitfalls:


      Cookies will not become visible until the next loading of a page that
      the cookie should be visible for. To test if a cookie was successfully
      set, check for the cookie on a next loading page before the cookie
      expires. Expire time is set via the expire parameter. A nice way to
      debug the existence of cookies is by simply calling
      print_r($_COOKI E);.

      Cookies must be deleted with the same parameters as they were set
      with. If the value argument is an empty string (""), and all other
      arguments match a previous call to setcookie, then the cookie with the
      specified name will be deleted from the remote client.

      Cookies names can be set as array names and will be available to your
      PHP scripts as arrays but seperate cookies are stored on the users
      system. Consider explode() or serialize() to set one cookie with
      multiple names and values.




      ---
      Email: rrjanbiah-Y!com

      Comment

      • Ralph Freshour

        #4
        Re: Cookie Question

        Yes that helped.

        I have a form where I use a checkbox to have the user inform me if
        they want to use a cookie to control color of the web page - if they
        enable the checkbox where is it that I can set the cookie using php?
        In the submitted form? I have tried the submitted form but I get an
        error saying headers have already been sent!

        Your right, this is a bit tricky!!!


        On Sat, 09 Aug 2003 17:45:34 +1000, John Smith
        <unleaded@unlea dedonline.net.n ospamthanks> wrote:
        [color=blue]
        >I'd suggest you have a further read on cookies and how they relate to
        >intricacies of HTTP. It can get a bit tricky ;).
        >
        >Anyway. The PHP script wouldn't 'delete' the cookie from your computer,
        >it simply sends a header to your browser stating that the cookie has
        >expired, and it is then up to the browser to delete that cookie from
        >_then on after_.
        >
        >So usually once you have sent a cookie that will expire the current
        >cookie (read: setting a cookie of the same name, but a past expiry date):
        >
        >setcookie('myc ookie', '', mktime()- 31536000, $cookiepath,
        >$cookiedomai n, $cookiesecure);
        >
        >You will then need to instruct the browser to refresh the page, by
        >sending a location header:
        >
        >//
        >// Time to refresh!
        >//
        >header ('Location: index.php');
        >
        >Hope this helps,
        >Grant.
        >
        >Ralph Freshour wrote:
        >[color=green]
        >> I wrote a small .php script that creates a cookie on my local PC - I
        >> also wrote one that deletes it.
        >>
        >> My question is this: when the cookie has been created and I run the
        >> delete .php script, when it first loads it does not delete the cookie
        >> - I always have to click on the browser refresh button - why doesn't
        >> the script execute on the first execution?
        >>
        >> Thanks...
        >>[/color][/color]

        Comment

        Working...