setting a cookie from a form value
| From: | Fernando Fiore | Date: | Mon, 10 Aug 2020 21:58:04 +0000 |
| Subject: | setting a cookie from a form value | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-327266@lists.php.net to get a copy of this message | ||
<?php
// this fails twice before working
// first with undefined index 'txt' on line 5
// then with undefined index 'val' on line 10 then it works
$name = $_POST['txt'];
setcookie("val",$name,time() + 24 * 60 * 60);
if(isset($_COOKIE) && !empty($_COOKIE))
{
$var = $_COOKIE['val'];
print('cookie = '. $var . '</br>');
}
else
echo ' cookie not set </br>';
?>
<form action="cookie2.php" name="myform" method="post">
<input type="text" name="txt" id="txt" value="abcdef"
/>
<input type="submit" formmethod="post" />
</form>