Forum Moderators: coopster

Message Too Old, No Replies

setcookie with variable?

         

Gruessle

10:07 am on Apr 16, 2005 (gmt 0)

10+ Year Member




The code below
$$abr = $_POST["$abr"];
&
$_COOKIE["$abr"] = $_POST["$abr"];
work but
setcookie("$abr", ($_POST["$abr"]));
does not why?

Here is the code:
<?
if ( isset ( $_POST["$abr"] ) )
{
$$abr = $_POST["$abr"];

$_COOKIE["$abr"] = $_POST["$abr"];

setcookie("$abr", ($_POST["$abr"]));
}
?>

dmmh

10:11 am on Apr 16, 2005 (gmt 0)

10+ Year Member



<?
if (isset($_POST['abr']))
{
$abr = $_POST['abr'];

$_COOKIE['abr'] = $abr;
setcookie('abr', ($abr));
}
?>

?

Gruessle

10:21 am on Apr 16, 2005 (gmt 0)

10+ Year Member



No,No,No,No :)
$$abr is correct see below:

if ( isset ( $_POST["subselect"] ) ) {

while (list($abr,$full) = each ($forms)) {

if ( isset ( $_POST["$abr"] ) ) {

$$abr = $_POST["$abr"];
$_COOKIE["$abr"] = $_POST["$abr"];
setcookie("$abr", ($_POST["$abr"]));

}}}

dcrombie

4:16 pm on Apr 16, 2005 (gmt 0)



AFAIK this code doesn't set a cookie - it just adds a value to the global variable $_COOKIE:

$_COOKIE["$abr"] = $_POST["$abr"];

OTOH, this code _does_ set a cookie:

setcookie("$abr", ($_POST["$abr"]));

but you can only read the cookie on the _next_ visit to the page. If you want to set the cookie _and_ have it readable on the same page, then you need both.

;)

Gruessle

8:38 pm on Apr 16, 2005 (gmt 0)

10+ Year Member




Well I am doing both as you can see; setting $_COOKIE["$abr"] works fine, when I come back to the page it is gone --> setcookie doesn't work and that is my question.
Why is setcookie not working?

<?
if ( isset ( $_POST["$abr"] ) )
{
$$abr = $_POST["$abr"];
$_COOKIE["$abr"] = $_POST["$abr"];
setcookie("$abr", ($_POST["$abr"]));
}
?>

realitybytes

8:15 am on Apr 18, 2005 (gmt 0)

10+ Year Member



<?
if ( isset ( $_POST["$abr"] ) )
{
$$abr = $_POST["$abr"];
$_COOKIE["$abr"] = $_POST["$abr"];
setcookie("$abr", ($_POST["$abr"]));
}
?>

if (isset($_COOKIE["$abr"])) {
$abr = $_COOKIE["abr"];
}

will get the vaule of $_POST["abr"] if the cookie is present.

realitybytes

9:19 am on Apr 18, 2005 (gmt 0)

10+ Year Member



<?
if (isset($_COOKIE["$abr"])) {
$_POST["$abr"] = $_COOKIE["abr"];
}

if ( isset ( $_POST["$abr"] ) )
{
$$abr = $_POST["$abr"];
$_COOKIE["$abr"] = $_POST["$abr"];
setcookie("$abr", ($_POST["$abr"]));
}
?>

That should do it