在PHP中,我将设置cookie并为其赋予一个变量值,当用户输入他们的名字时,他们将被带到另一个页面,但当他们到达那个新页面时,我需要将该cookie的值更改为他们输入的名字,有人可以告诉我这是怎么做的吗?
答案 0 :(得分:3)
您只需将名称传递给setcookie
,这将覆盖以前存储的任何值。
setcookie("name", $name, time() + 60 * 60 * 24); // expires in a day
答案 1 :(得分:0)
在第一页(index.php)中,例如
<?php
//check if the form is submitted
if($_POST['update_name']){
if(!empty($_POST['name'])){
//name filed is filled
//define cookie expire time
$expire = time()+60*60*24*30; #cookie will expire after a month
//set the cookie
setcookie("name", $_POST['name'], $expire);
//take the user to another page
header("location: page_two.php");
}else{
//form was submitted with empty name field
//show error message
echo "Name is required";
}
}
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="text" name="name" />
<input type="submit" name="update_name" value="Submit" />
</form>
page_two.php中的
<?php
echo $_COOKIE["name"];
?>