Log in

View Full Version : Cookie help



Tim Dobson
04-07-2010, 10:53 PM
I am new to php and just started doing a bit of stuff with cookies and now got slightly stuck.

I am fully aware on how to create a cookie, make a cookie invalid.

im also aware of
print_r($_COOKIE);
to print the value of the cookie on the page... but what if i wanted $thecookie value to be the value of the cookie then wanted to print $thecookievalue

Im not going to echo $thecookievalue i just need to value of the cookie in a string so i can set an if statement to check what the value of the cookie is so i can set the correct page of the user.

Thanks

Nile
04-08-2010, 12:15 AM
To get the cookie value, wouldn't you just do $_COOKIE['name'] and then manipulate that?

Tim Dobson
04-10-2010, 07:22 AM
Yea sure i can do that but new to all this stuff and what ever iv tried has failed :( just need an example to show me how its done lol

james438
04-10-2010, 07:40 AM
It sounds like what you want is to retrieve the cookie value from a cookie without actually manipulating the cookie itself.

$thecookievalue=$_COOKIE['name'];

that's it.

Tim Dobson
04-10-2010, 08:04 AM
Ah yes i did that and it pulled out an error but i figured it out so i asked because i wasent sure if this was what i was doing wrong but thanks

djr33
04-10-2010, 05:22 PM
What is the error? Just like any variable, if you try to use a variable that has no value it will give you a warning (not an error, but similar) if the error reporting is high enough. You can fix this by making sure it's set first:
$var = ''; //set it to blank
if (isset($_COOKIE['var'])) { $var = $_COOKIE['var']; }

There are lots of other ways to approach it as well. You can also just lazily suppress the error, but that's not a great idea (since it can be unpredictable), but at least you won't get an error message for visitors:
echo @$_COOKIE['var'];
(the @ symbol means 'and don't show an error if there is one')