2012-11-11 34 views
0

在PHP中,我會設置cookie並給它一個變量的值,當用戶輸入他們的名字時,他們會被帶到另一個頁面,但是當他們到達那個新頁面時,我需要該cookie的值改爲什麼名字他們進來了,請問有人會告訴我該怎麼做?我想要改變一個cookie值,當前往另一個頁面?

回答

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"]; 
?> 
3

您只需將名稱傳遞給setcookie即可覆蓋之前存儲的任何值。

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