Web Hosting Talk







View Full Version : Setcookie()


tylerg
07-06-2004, 02:17 PM
Hello Helper -

Can you please tell me why my cookie is not printing out?

<?php
//WHICH CATEGORY IS POSTING
if (isset($_POST['COMP_x']['COMP_y']))
{
echo "compete";
setcookie ('CAT_ID','1',time()+1200);
}
if (isset($_POST['FUND_x']['FUND_y']))
{
echo "fundamental";
setcookie ('CAT_ID','2',time()+1200);
}
if (isset($_POST['BASK_x']['BASK_y']))
{
echo "basketball";
setcookie ('CAT_ID','3',time()+1200);
}
if (isset($_POST['TRAIN_x']['TRAIN_y']))
{
echo "train";
setcookie ('CAT_ID','4',time()+1200);
}
if (isset($_POST['WIN_x']['WIN_y']))
{
echo "win";
setcookie ('CAT_ID','5');
}
if (isset($_COOKIE['CAT_ID']))
{
$cook=$_COOKIE('CAT_ID');
echo $cook;
}
?>

It IS in fact POSTing properly, as the ECHO before the setcookie() is printing to the screen, but the ECHO $COOK doesn't seem to be printing anything.... pls help!:confused:

rejje
07-06-2004, 02:24 PM
try this:
$cook = $_COOKIE['CAT_ID'];

TechSolution
07-06-2004, 02:34 PM
Are you sending any HTML before you call setcookie?

SeņorAmor
07-06-2004, 04:01 PM
You cannot output any data before setting cookies. Set your cookie before echoing.

Rich2k
07-06-2004, 04:06 PM
Additionally you can't view the contents of a cookie you have just set without refreshing the page.