Php 我想在转到另一页时更改cookies值?

Php 我想在转到另一页时更改cookies值?,php,redirect,cookies,setcookie,Php,Redirect,Cookies,Setcookie,在PHP中,我将设置cookie并给它一个变量值,当用户输入他们的名字时,他们将被带到另一个页面,但当他们到达新页面时,我需要将cookie的值更改为他们输入的名字,有人能告诉我怎么做吗?您只需将名称传递给setcookie,它将覆盖以前存储的任何值 setcookie("name", $name, time() + 60 * 60 * 24); // expires in a day 您只需将名称传递给setcookie,即可覆盖以前存储的任何值 setcookie("name", $nam

在PHP中,我将设置cookie并给它一个变量值,当用户输入他们的名字时,他们将被带到另一个页面,但当他们到达新页面时,我需要将cookie的值更改为他们输入的名字,有人能告诉我怎么做吗?

您只需将名称传递给
setcookie
,它将覆盖以前存储的任何值

setcookie("name", $name, time() + 60 * 60 * 24); // expires in a day

您只需将名称传递给
setcookie
,即可覆盖以前存储的任何值

setcookie("name", $name, time() + 60 * 60 * 24); // expires in a day
例如,在第一页(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>

例如,在第一页(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>