ive gone through many explanations of using cookies with php. for the most part, i understand the setcookie() function. however, retrieving it and using it effectively in php page is what im confused about.
say i set a cookie like so
<?php
setcookie(nameofvariable1, $valueofvariable2, time()+3600);
?>
<html>
<head> ...
i put this in a sample page for myself and tried it out, but i looked in my cookies folder, and there was nothing there (i just previously cleared it out.) why wasnt there a cookie there?
another thing--
what variable (or what would i put) in the body section to echo the value of the cookie? would i put
echo ("$nameofvariable1"); or
echo ("$valueofvariable2");
thank yousetcookie(nameofvariable1, $valueofvariable2, time()+3600);
for one you should have a $ in front of nameofvariable1
to get the value of the cookie, yo use the $_COOKIE superglobal, so say you use this to set the cookie
setcookie("MyCookie", "I AM THE WALRUS", time()+3600);
you would then use $_COOKIE['MyCookie'] to get it again. Originally posted by tommeh
ive gone through many explanations of using cookies with php. for the most part, i understand the setcookie() function. however, retrieving it and using it effectively in php page is what im confused about.
say i set a cookie like so
<?php
setcookie(nameofvariable1, $valueofvariable2, time()+3600);
?>
<html>
<head> ...
i put this in a sample page for myself and tried it out, but i looked in my cookies folder, and there was nothing there (i just previously cleared it out.) why wasnt there a cookie there?
another thing--
what variable (or what would i put) in the body section to echo the value of the cookie? would i put
echo ("$nameofvariable1"); or
echo ("$valueofvariable2");
thank you
re question 1: honestly I have no idea...are you sure the cookie set propery and that your browser accepts them?
re question 2:
$cookiename = $_COOKIE["name"];
echo $cookiename;Originally posted by n8thegreat
setcookie(nameofvariable1, $valueofvariable2, time()+3600);
for one you should have a $ in front of nameofvariable1
nice catch N8 I never really thought about the missing $thanks.
n8,
my intention of using a variable in the value was to represent a string, sorry. The name shouldnt get a $ because, like in forms it gets made into a variable when it passes through to the browser, correct?i tried it out, and i was successful. However, I did not use the global variable $_COOKIE. Simply,
<?php
setcookie("MyCookie", "I AM THE WALRUS", time()+3600);
?>
<html>
<head></head>
<body>
<? echo ("$MyCookie");
?>
</body>
</html>
worked perfectly, and I did find the cookie in my cookies folder. Thank you both, you helped me much.Originally posted by tommeh
i tried it out, and i was successful. However, I did not use the global variable $_COOKIE. Simply,
<?php
setcookie("MyCookie", "I AM THE WALRUS", time()+3600);
?>
<html>
<head></head>
<body>
<? echo ("$MyCookie");
?>
</body>
</html>
worked perfectly, and I did find the cookie in my cookies folder. Thank you both, you helped me much.
thats an insecure way that relys on register_globals.Instead of specifically a cookie,I could easily pass MyCookie through the query string or any other request method.ALWAYS use the superglobal arrays.using the superglobal array provoked an error, which is why i even attempted to try it without one...Originally posted by tommeh
using the superglobal array provoked an error, which is why i even attempted to try it without one...
what error?expecting T_STRING etc..Originally posted by tommeh
expecting T_STRING etc..
are you using it properly?proper usage is as follows:
$cookie = $_COOKIE["MyCookie"];
echo 'My Cookie:'.$cookie;i guess not, i did exactly what n8 said. I didnt make a new variable, i just echoed $_COOKIE["MyCookie"]. It should do the same thing. Making a new variable seems like an unnecessary extra step.Originally posted by tommeh
i guess not, i did exactly what n8 said. I didnt make a new variable, i just echoed $_COOKIE["MyCookie"]. It should do the same thing. Making a new variable seems like an unnecessary extra step.
indeed...but the question is,HOW did you echo it?
echo "$_COOKIE["MyCookie"]"; //will NOT work
echo ''.$_COOKIE["MyCookie"].''; //will work
echo $_COOKIE["MyCookie"]; //will workim not quite stupid enough to mix up quotations.. I use phped, which color codes everything, so its easy to tell if you had an error or not. the third example of yours is how i echoed it.Originally posted by tommeh
im not quite stupid enough to mix up quotations.. I use phped, which color codes everything, so its easy to tell if you had an error or not. the third example of yours is how i echoed it.
:| I wasn't intending to offend you.I truely apologize.oh...one thing I just thought of...the page has to be reloaded before the cookie is available...so you can't access a cookie on the same page it's created.I'm not sure if your reloading or not.lol, i wasnt offended at all, my bad. Yeah, i refreshed and it still produced an error... oh well...what is the exact code you are using and the exact error you get. its not a problem with the $_COOKIE superglobal, i can tell you that.<?php
setcookie("MyCookie", "I AM THE WALRUS", time()+3600);
?>
<html>
<head></head>
<body>
<? echo ("$_COOKIE['MyCookie']");
?>
</body>
</html>
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in D:\Websites\tom\ex.php on line 8echo ("$_COOKIE['MyCookie']");
you cant do that you dont need the parenthesis anyways...
do echo $_COOKIE['MyCookie'];
or echo "{$_COOKIE['MyCookie']}";shouldn't it be
<? echo $_COOKIE["MyCookie"];
?>
Or does it not matter?
EDIT: n8 beat me ok thanks all, the correct way to do it is without the (" "). ThanksOriginally posted by tommeh
<?php
setcookie("MyCookie", "I AM THE WALRUS", time()+3600);
?>
<html>
<head></head>
<body>
<? echo ("$_COOKIE['MyCookie']");
?>
</body>
</html>
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in D:\Websites\tom\ex.php on line 8
oh but you just said
Originally posted by tommeh
im not quite stupid enough to mix up quotations.. I use phped, which color codes everything, so its easy to tell if you had an error or not. the third example of yours is how i echoed it.
this
echo ("$_COOKIE['MyCookie']");
is the same as this
echo "$_COOKIE['MyCookie']"; // which will not work
I don't mean to sound rude but I just wanted to let you to understand why it was wrong. the () had nothing to do with it. you could have did it this way too
echo ("{$_COOKIE['MyCookie']}");
and it would still work.
say i set a cookie like so
<?php
setcookie(nameofvariable1, $valueofvariable2, time()+3600);
?>
<html>
<head> ...
i put this in a sample page for myself and tried it out, but i looked in my cookies folder, and there was nothing there (i just previously cleared it out.) why wasnt there a cookie there?
another thing--
what variable (or what would i put) in the body section to echo the value of the cookie? would i put
echo ("$nameofvariable1"); or
echo ("$valueofvariable2");
thank yousetcookie(nameofvariable1, $valueofvariable2, time()+3600);
for one you should have a $ in front of nameofvariable1
to get the value of the cookie, yo use the $_COOKIE superglobal, so say you use this to set the cookie
setcookie("MyCookie", "I AM THE WALRUS", time()+3600);
you would then use $_COOKIE['MyCookie'] to get it again. Originally posted by tommeh
ive gone through many explanations of using cookies with php. for the most part, i understand the setcookie() function. however, retrieving it and using it effectively in php page is what im confused about.
say i set a cookie like so
<?php
setcookie(nameofvariable1, $valueofvariable2, time()+3600);
?>
<html>
<head> ...
i put this in a sample page for myself and tried it out, but i looked in my cookies folder, and there was nothing there (i just previously cleared it out.) why wasnt there a cookie there?
another thing--
what variable (or what would i put) in the body section to echo the value of the cookie? would i put
echo ("$nameofvariable1"); or
echo ("$valueofvariable2");
thank you
re question 1: honestly I have no idea...are you sure the cookie set propery and that your browser accepts them?
re question 2:
$cookiename = $_COOKIE["name"];
echo $cookiename;Originally posted by n8thegreat
setcookie(nameofvariable1, $valueofvariable2, time()+3600);
for one you should have a $ in front of nameofvariable1
nice catch N8 I never really thought about the missing $thanks.
n8,
my intention of using a variable in the value was to represent a string, sorry. The name shouldnt get a $ because, like in forms it gets made into a variable when it passes through to the browser, correct?i tried it out, and i was successful. However, I did not use the global variable $_COOKIE. Simply,
<?php
setcookie("MyCookie", "I AM THE WALRUS", time()+3600);
?>
<html>
<head></head>
<body>
<? echo ("$MyCookie");
?>
</body>
</html>
worked perfectly, and I did find the cookie in my cookies folder. Thank you both, you helped me much.Originally posted by tommeh
i tried it out, and i was successful. However, I did not use the global variable $_COOKIE. Simply,
<?php
setcookie("MyCookie", "I AM THE WALRUS", time()+3600);
?>
<html>
<head></head>
<body>
<? echo ("$MyCookie");
?>
</body>
</html>
worked perfectly, and I did find the cookie in my cookies folder. Thank you both, you helped me much.
thats an insecure way that relys on register_globals.Instead of specifically a cookie,I could easily pass MyCookie through the query string or any other request method.ALWAYS use the superglobal arrays.using the superglobal array provoked an error, which is why i even attempted to try it without one...Originally posted by tommeh
using the superglobal array provoked an error, which is why i even attempted to try it without one...
what error?expecting T_STRING etc..Originally posted by tommeh
expecting T_STRING etc..
are you using it properly?proper usage is as follows:
$cookie = $_COOKIE["MyCookie"];
echo 'My Cookie:'.$cookie;i guess not, i did exactly what n8 said. I didnt make a new variable, i just echoed $_COOKIE["MyCookie"]. It should do the same thing. Making a new variable seems like an unnecessary extra step.Originally posted by tommeh
i guess not, i did exactly what n8 said. I didnt make a new variable, i just echoed $_COOKIE["MyCookie"]. It should do the same thing. Making a new variable seems like an unnecessary extra step.
indeed...but the question is,HOW did you echo it?
echo "$_COOKIE["MyCookie"]"; //will NOT work
echo ''.$_COOKIE["MyCookie"].''; //will work
echo $_COOKIE["MyCookie"]; //will workim not quite stupid enough to mix up quotations.. I use phped, which color codes everything, so its easy to tell if you had an error or not. the third example of yours is how i echoed it.Originally posted by tommeh
im not quite stupid enough to mix up quotations.. I use phped, which color codes everything, so its easy to tell if you had an error or not. the third example of yours is how i echoed it.
:| I wasn't intending to offend you.I truely apologize.oh...one thing I just thought of...the page has to be reloaded before the cookie is available...so you can't access a cookie on the same page it's created.I'm not sure if your reloading or not.lol, i wasnt offended at all, my bad. Yeah, i refreshed and it still produced an error... oh well...what is the exact code you are using and the exact error you get. its not a problem with the $_COOKIE superglobal, i can tell you that.<?php
setcookie("MyCookie", "I AM THE WALRUS", time()+3600);
?>
<html>
<head></head>
<body>
<? echo ("$_COOKIE['MyCookie']");
?>
</body>
</html>
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in D:\Websites\tom\ex.php on line 8echo ("$_COOKIE['MyCookie']");
you cant do that you dont need the parenthesis anyways...
do echo $_COOKIE['MyCookie'];
or echo "{$_COOKIE['MyCookie']}";shouldn't it be
<? echo $_COOKIE["MyCookie"];
?>
Or does it not matter?
EDIT: n8 beat me ok thanks all, the correct way to do it is without the (" "). ThanksOriginally posted by tommeh
<?php
setcookie("MyCookie", "I AM THE WALRUS", time()+3600);
?>
<html>
<head></head>
<body>
<? echo ("$_COOKIE['MyCookie']");
?>
</body>
</html>
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in D:\Websites\tom\ex.php on line 8
oh but you just said
Originally posted by tommeh
im not quite stupid enough to mix up quotations.. I use phped, which color codes everything, so its easy to tell if you had an error or not. the third example of yours is how i echoed it.
this
echo ("$_COOKIE['MyCookie']");
is the same as this
echo "$_COOKIE['MyCookie']"; // which will not work
I don't mean to sound rude but I just wanted to let you to understand why it was wrong. the () had nothing to do with it. you could have did it this way too
echo ("{$_COOKIE['MyCookie']}");
and it would still work.