开发者

My function recreates a cookie even if it already exists

Hello I have a problem.

I am using the below function to create a cookie when there is no one available.

The problem is that when the browser has been closed an opened again, this function creates 开发者_运维知识库the Cookie again...even if it is already there.. So everytime the user opens and closes the browser, this cookie gets created and created again..

This is really bad for me. How do I avoid this issue?

What 's wrong in my function?

Thanks

 <?php
  function GenerateTuittingID()
  {
      $todaydate = date('Ymd');
      $time = mktime(date('G'), date('i'), date('s'));
      $NowisTime = date('Gis', $time);
      $random_numbers = substr(number_format(time() * rand(), 0, '', ''), 0, 10);
      $random = $random_numbers . $todaydate;
      if (!isset($_COOKIE["tuittingID"])) {
          setcookie("tuittingID", $random, mktime(0, 0, 0, 12, 31, 2015));
      }
  }
?>


I see no problem with your code

  • problem is in user's browser
  • there is another problem in another part of your script


The cookie can never be created again and again --

If it already existed then it will get overwritten, otherwise, if it does not exist it will be created. make sure you browser does not clear cookies when closing.


1) Open your browser settings, go to Privacy.

2) Click on Use custom settings for history.

3) Make sure the drop down beside Keep Until say They Expire. If not, update it and press okay.

4) Let me know if that solved your problem.

Edit (Just saw your comment)

Set the path and domain on your cookies so they are accessible from the whole site:

setcookie("tuittingID", $random, mktime(0, 0, 0, 12, 31, 2015),
  '/', '.yourdomain.com');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜