470,849 Members | 1,095 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

find a cookie by part of it's name

Hi all,

cookies are stored in an array called $HTTP_COOKIE_VARS 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 ?
May 9 '06 #1
3 2868
Ron Croonenberg wrote:
Hi all,
Hi,

cookies are stored in an array called $HTTP_COOKIE_VARS right ?
Sometimes, it is an 'old' array, possibly not available in future version of
PHP.

Use $_COOKIE instead if you can.

read more here:
http://nl3.php.net/manual/en/reserve...iables.cookies

Is it possible to find a cookie if you know part of it's name ?
Yes.
Try this:

<pre>
<?php print_r($_COOKIE); ?>
</pre>

That will output to html, nicely formatted, what is in the cookiestring the
clientbrowser send along the request to your page.

For example the cookiename is firstname and I want to check all
cookies that start with "first" in there name.
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.

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


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

Regards,
Erwin Moller
May 10 '06 #2
On Mon, 08 May 2006 20:53:38 -0400, Ron Croonenberg wrote:
Hi all,

cookies are stored in an array called $HTTP_COOKIE_VARS right ?
$_COOKIE is better.
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 ?


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

May 10 '06 #3
Ron Croonenberg wrote:
Hi all,

cookies are stored in an array called $HTTP_COOKIE_VARS 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 ?


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("firstname",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ĝ
- ki******@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
May 10 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Dan Stromberg | last post: by
3 posts views Thread by Karsten Grombach | last post: by
1 post views Thread by Randall Parker | last post: by
4 posts views Thread by Gridlock | last post: by
8 posts views Thread by mutale82 | last post: by
5 posts views Thread by mosscliffe | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.