简体   繁体   English

如何使用PHP在cookie中存储数据?

[英]How to store data in cookies using php?

I am new in PHP and don't have much knowledge about cookies. 我是PHP的新手,对cookie没有太多了解。

It seems that I must store my data in cookies because I just need those data temporarily. 似乎我必须将我的数据存储在cookie中,因为我只是暂时需要这些数据。 I have read some article and tried some of it but I get blank result from it. 我已经阅读了一些文章并尝试了一些但我得到了空白的结果。

here is my code: 这是我的代码:

<?php
if (isset($_COOKIE['vaccine'])) {
setcookie('vaccine',$vaccine);
foreach ($_COOKIE[$vaccine] as $vaccine){ ?>

<div class="control-group">
<label class="control-label">
<?php echo '&nbsp'.'<a href="javascript:void(0);"  rel="tooltip"    
 title="Delete" onclick="delete_vaccination('.$vaccine->vaccination_record_id.');"><i class="icon-minus-sign"></i></a>'.'';?><?php echo $vaccine->vaccination_record_brand;?>

</label>
</div>

<?php }} ?>

You could (should?) use sessions for this. 你可以(应该?)使用会话。 Cookies are not temporary, they are stored on the client's computer. Cookie不是临时的,它们存储在客户端的计算机上。 Sessions are temporary, they are alive until you destroy them. 会话是暂时的,他们一直活着,直到你摧毁他们。

Furthermore, you have some errors in your PHP (not so much in the cookie handling): $_COOKIE[$vaccine] will dereference the value of $vaccine in your $_COOKIE array. 此外,您的PHP中存在一些错误(在cookie处理中没有那么多): $_COOKIE[$vaccine]将取消引用$_COOKIE数组中$vaccine As $vaccine seems to be an array, you are looking at $_COOKIE["Array"] . 由于$vaccine似乎是一个数组,你正在看$_COOKIE["Array"] You will want to fix it thus: 您将需要修复它:

foreach ($_COOKIE["vaccine"] as $vaccine)

Also, as you are putting an object in a cookie, you have to serialize/unserialize it, before setting or getting the cookie: 此外,当您将对象放入cookie时,您必须在设置或获取cookie之前序列化/反序列化它:

// fetch the vaccines somewhere first
setcookie('vaccine',serialize($vaccines));

Then, lastly, why do you set the cookie, when the cookie is set? 然后,最后,为什么在设置cookie时设置cookie? You should read it, when set. 你应该在设置时阅读它。

Complete code: 完整代码:

<?php
if (isset($_COOKIE['vaccine'])) {
   $vaccines = unserialize($_COOKIE['vaccine']);
   foreach ($vaccines as $vaccine){ ?>

      <div class="control-group">
      <label class="control-label">
      <?php echo '&nbsp'.'<a href="javascript:void(0);"  rel="tooltip"    
          title="Delete" onclick="delete_vaccination('.$vaccine->vaccination_record_id.');">
          <i class="icon-minus-sign"></i></a>'.'';?>
          <?php echo $vaccine->vaccination_record_brand;?>

      </label>
      </div>

<?php }} ?>

If that is your entire code then where is the data that goes into $vaccine? 如果这是您的整个代码,那么进入$疫苗的数据在哪里? It appears that you set that cookie to be empty. 您似乎将该Cookie设置为空。

then there are code bugs as well; 然后还有代码错误; foreach should have $_COOKIE['vaccine'] instead of $_COOKIE[$vaccine]. foreach应该有$ _COOKIE ['疫苗']而不是$ _COOKIE [$疫苗]。 But then see below for comments about cookies and arrays. 但是,请参阅下面有关cookie和数组的评论。

Additionally you cannot store a PHP array into a cookie. 此外,您无法将PHP数组存储到cookie中。 You will have to serialize the array while storing it in cookie, and unserialize it when you get the cookie back, into a PHP array. 您必须在将数组存储在cookie中时序列化数组,并在将cookie返回时将其反序列化为PHP数组。 Either that, or look at example #3 in PHP manual - http://php.net/manual/en/function.setcookie.php on how to deal with arrays in cookies. 要么是这样,要么看看PHP手册中的例子#3 - http://php.net/manual/en/function.setcookie.php关于如何处理cookie中的数组。 Be cautious that if you use this way, of using array cookies, then that many cookies are set. 要小心,如果你使用这种方式,使用数组cookie,那么设置了很多cookie。 Which may be a problem. 这可能是个问题。 And so you might want to think about serialization. 所以你可能想要考虑序列化。

One implementation of serialization is given in the setcookie entry in the PHP manual itself. 序列化的一个实现在PHP手册本身的setcookie条目中给出。 See the comment by 'cwillard at fastmail dot fm' in the manual page is gave above. 请参阅上面给出的手册页中的'cwillard at fastmail dot fm'的评论。

Also the cookies that you set would be accessible only on the next page load. 您设置的cookie也只能在下一页加载时访问。 Even when you stuff data into $vaccine, the first time around you will see a blank. 即使你把数据填入$ vaccine,你第一次看到空白。 On page reload you should see wha 在页面重新加载你应该看到wha

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM