/    Sign up×
Community /Pin to ProfileBookmark

setting and getting cookies…

Okay, an hour-and-a-half of trying to figure this out has gotten me nowhere…

I’ve seen on some sites to [b]only[/b] set cookies before any output (<html>, for instance); I’ve seen some sites set cookies in embedded php. This, of course, confuses the s**t out of me.

Could someone PLEASE, I beg you, lead me to a very simple working example of how to set and retrieve a cookie (w3schools, php.net didn’t help, among others)? Or, please show me how my approach is incorrect:

[code]
<html>
<body>
<?php
$_GET[‘val’];
setcookie(‘val1’, $val);
$myCookie = $_COOKIE[‘val1’];
echo (‘<script type=”text/javascript”>alert(‘.$myCookie.'</script>’);
?>
</body>
</html>
[/code]

RESULT: alert returns nothing (null).

May God bless you if you can help me. ?

Regards,

bubbis

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 20.2006 — When you set a cookie, it is not available in the $_COOKIE array until another page on your site is accessed (or the same page is re-accessed), as the browser has to send the cookie data to the server in the next page request.
Copy linkTweet thisAlerts:
@NogDogJun 20.2006 — Oh, and unless you use the ob_* functions to do output buffering, you can only have your setcookie() calls before anything gets output to the browser (includng the text outside of your <?php ... ?> tags).
Copy linkTweet thisAlerts:
@bubbisthedogauthorJun 20.2006 — Thank you very much for the responses, NogDog.

Okay, so now I know to set the cookie prior to any output; thanks. So here's what I'll try:
<i>
</i>&lt;?php
$_GET['val'];
setcookie('val1', $val);
?&gt;
&lt;html&gt;
&lt;body&gt;
&lt;form action="&lt;?php echo $_SERVER['PHP_SELF'] ?&gt;" method="get"&gt;
&lt;input type="text" id="val1" name="val1" /&gt;
&lt;input type="submit" value="submit" /&gt;
&lt;/form&gt;
&lt;?php
$myCookie = $_COOKIE['val1'];
echo ('&lt;script type="text/javascript"&gt;alert('.$myCookie.'&lt;/script&gt;');
?&gt;
&lt;/body&gt;
&lt;/html&gt;

If I understand you correctly, when I click the Submit button, my alert should display the text in the val1 text box, right? Well, it doesn't. I can see ?val1=hello in the URL (supposing I type in hello in the text box and click Submit), but my alert is still null. What's going on?

Thanks again, NogDog.

bubbis

P.S. Please pardon my ignorance. ?
Copy linkTweet thisAlerts:
@NogDogJun 20.2006 — After playing around with it a bit, there were some problems with variable name consistency as well as JavaScript syntax. Here's a working version I came up with:
[code=php]
<?php
if(isset($_GET['val1'])) // let's only do this if form was submitted
{
setcookie('val1', $_GET['val1']);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>
<title>Cookie Test</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="get">
<input type="text" id="val1" name="val1" />
<input type="submit" value="submit" />
</form>
<?php
$myCookie = $_COOKIE['val1'];
// fixed problems with quotes, brackets, etc.:
echo ('<script type="text/javascript">alert("'.$myCookie.'");</script>');
?>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@bubbisthedogauthorJun 20.2006 — egad, Nogdog. I can't thank you enough for taking the time to help me. ? You've always been there to kindly help this newbie, and I appreciate it a lot. I now understand how to set and retrieve a cookie properly.

Have a great evening,

bubbis
×

Success!

Help @bubbisthedog spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...