find a cookie by part of it's name

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

    #1

    find a cookie by part of it's name

    Hi all,

    cookies are stored in an array called $HTTP_COOKIE_VA RS right ?

    Is it possible to find a cookie if you know part of it's name ?

    For example the cookiename is firstname and I want to check all
    cookies that start with "first" in there name.

    I tried to work wih string and substrings, but that didn't seem
    to work ?
  • Erwin Moller

    #2
    Re: find a cookie by part of it's name

    Ron Croonenberg wrote:
    [color=blue]
    > Hi all,[/color]

    Hi,
    [color=blue]
    >
    > cookies are stored in an array called $HTTP_COOKIE_VA RS right ?[/color]

    Sometimes, it is an 'old' array, possibly not available in future version of
    PHP.

    Use $_COOKIE instead if you can.

    read more here:

    [color=blue]
    >
    > Is it possible to find a cookie if you know part of it's name ?[/color]

    Yes.
    Try this:

    <pre>
    <?php print_r($_COOKI E); ?>
    </pre>

    That will output to html, nicely formatted, what is in the cookiestring the
    clientbrowser send along the request to your page.
    [color=blue]
    >
    > For example the cookiename is firstname and I want to check all
    > cookies that start with "first" in there name.[/color]

    Just examine the output of the above, and you'll figure out a way to search
    for substrings. If not, come back here with some samplecode.
    [color=blue]
    >
    > I tried to work wih string and substrings, but that didn't seem
    > to work ?[/color]

    Show us what you tried, along with the content of $_COOKIE your script
    received.

    Regards,
    Erwin Moller

    Comment

    • Andy Jeffries

      #3
      Re: find a cookie by part of it's name

      On Mon, 08 May 2006 20:53:38 -0400, Ron Croonenberg wrote:[color=blue]
      > Hi all,
      >
      > cookies are stored in an array called $HTTP_COOKIE_VA RS right ?[/color]

      $_COOKIE is better.
      [color=blue]
      > Is it possible to find a cookie if you know part of it's name ?
      >
      > For example the cookiename is firstname and I want to check all cookies
      > that start with "first" in there name.
      >
      > I tried to work wih string and substrings, but that didn't seem
      > to work ?[/color]

      The answer is easy, but this is sounding a bit like homework... Post the
      code you have and we'll help more.

      As a hint, use foreach to iterate the $_COOKIE array, look at the key for
      each item and compare it using either substr or regular expressions.

      That should have given you enough terms to Google your way through it.

      Cheers,


      Andy


      --
      Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
      http://www.gphpedit.org | PHP editor for Gnome 2
      http://www.andyjeffries.co.uk | Personal site and photos

      Comment

      • Kim André Akerø

        #4
        Re: find a cookie by part of it's name

        Ron Croonenberg wrote:
        [color=blue]
        > Hi all,
        >
        > cookies are stored in an array called $HTTP_COOKIE_VA RS right ?
        >
        > Is it possible to find a cookie if you know part of it's name ?
        >
        > For example the cookiename is firstname and I want to check all
        > cookies that start with "first" in there name.
        >
        > I tried to work wih string and substrings, but that didn't seem
        > to work ?[/color]

        First of all, as others have said, use $_COOKIE instead.

        Second, $_COOKIE is an array, so you can use the array functions to
        find the cookie.

        And since you're looking for the cookie *name*, array_search() and
        array_keys() would be useful in this case. Like this:

        $cookiename = array_search("f irstname",array _keys($_COOKIE) );

        If the search turns up empty, then $cookiename = false, otherwise,
        $cookiename will contain the name of the cookie. The value for the
        cookie is then easily retrieved by $_COOKIE[$cookiename].

        --
        Kim André Akerø
        - kimandre@NOSPAM betadome.com
        (remove NOSPAM to contact me directly)

        Comment

        Working...