我需要在作业中设置Cookie。当我使用字符串设置它们时,即setcookie('Name', 'John', time() + 86400)它可以工作,但是当使用PHP变量时,即setcookie('Name', $name, time() + 86400)未设置cookie。
我不确定自己在做什么错。我已经在页面上回显了$ name变量,它显示了出来,所以我知道这已经设置了。当我使用显示Cookie时$_COOKIE['Name'],它在接收到字符串时才起作用,但在时则不起作用PHP variable。
order01.php
<form action="order02.php" class="formLayout">
<div class="formGroup">
<label>First name:</label>
<input type="text" name="fname" class="textbox" autofocus
required placeholder="First name" title="first name"
maxlength="20" pattern="[A-Za-z'-]{2,20}">
</div>
<div class="formGroup">
<label> Car model:</label>
<div class="formElements">
<input type="radio" name="model" required value="Mustang">Ford Mustang<br>
<input type="radio" name="model" required value="Subaru">Subaru WRX
STI<br>
<input type="radio" name="model" required value="Corvette">Corvette<br>
</div>
</div>
<?php
$name = $_GET['fname'];
$model = $_GET['model'];
//write cookies for name and model for 1 day
setcookie('Name', $name, time() + 86400);
setcookie('Model', $model, time() + 86400);
?>
order02.php
if(isset($_COOKIE['Name'])){
echo "Cookie ".$_COOKIE['Name']." is set";
}
else{
echo "<div class='pageContainer'>";
echo "<h2 class='containerText, centerText'>Failed to validate inputs";
echo "<br><br>";
echo "<a href='order01.php'><button>Go Back</button></a>";
echo "</div>";
exit();
}
繁星淼淼
智慧大石