Click to See Complete Forum and Search --> : setting and getting cookies...


bubbisthedog
06-19-2006, 06:55 PM
Okay, an hour-and-a-half of trying to figure this out has gotten me nowhere...

I've seen on some sites to only 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:

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

RESULT: alert returns nothing (null).

May God bless you if you can help me. :)

Regards,

bubbis

NogDog
06-19-2006, 07:21 PM
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.

NogDog
06-19-2006, 07:23 PM
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).

bubbisthedog
06-19-2006, 07:51 PM
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:

<?php
$_GET['val'];
setcookie('val1', $val);
?>
<html>
<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'];
echo ('<script type="text/javascript">alert('.$myCookie.'</script>');
?>
</body>
</html>

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. :)

NogDog
06-19-2006, 08:28 PM
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:

<?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>

bubbisthedog
06-19-2006, 08:37 PM
egad, Nogdog. I can't thank you enough for taking the time to help me. :D 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