为了账号安全,请及时绑定邮箱和手机立即绑定

setcookie()PHP无法使用变量

setcookie()PHP无法使用变量

PHP
ibeautiful 2021-05-07 15:14:56
我需要在作业中设置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.phpif(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();}
查看完整描述

2 回答

?
繁星淼淼

TA贡献1775条经验 获得超11个赞

不得在Cookie行(如echo,print_r()和HTML标记)之前输出


您可以在任何输出之前编写cookie代码


 <?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);

       ?>


<form action="order01.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>


   <input type="submit" value="submit">

  </div>

 </form>


查看完整回答
反对 回复 2021-05-21
?
智慧大石

TA贡献1946条经验 获得超3个赞

您的代码中几乎没有问题,首先,没有结束</form>标记,其次,您必须将表单提交到order01.php保存位置,cookie因此请尝试这样做,它将起作用


Order01.php


<form action="order01.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>


 <input type="submit" value="submit">

</div>

</form>

Order02.php


  <?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);


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();

  }

?>


查看完整回答
反对 回复 2021-05-21
  • 2 回答
  • 0 关注
  • 122 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信