【问题标题】:PHP Cookies and Visit CounterPHP Cookies 和访问计数器
【发布时间】:2016-08-09 21:13:04
【问题描述】:

我正在尝试学习 php(全新),并且我正在尝试实现在这篇文章中发现的完全相同的东西,即使用 cookie 进行计数器和网站上的最后访问:

PHP cookie visit counter not working

但与该帖子中的用户不同,我遇到了这个臭名昭著的错误:

“这是您第一次使用服务器!警告:无法修改标头信息 - 标头已由(输出开始于第 17 行的 (...) 开始)

这是第 17 行:setcookie('visitCount1');

现在我意识到这很常见并搜索了SO并找到了这篇文章:

How to fix "Headers already sent" error in PHP

我彻底阅读并了解了为什么会发生这种情况的可能原因,包括空格和在任何 html 代码之前输入我的浏览器提到的注释 php 行以及删除结束标记“?>”和我作为很好地尝试将 ob_start() 放在我的代码开头,但仍然出现相同的错误结果。

这是我试图运行的代码(取自上面的帖子):

<?php
$Month = 3600 + time();

date_default_timezone_set('EST');
setcookie('AboutVisit1', date("D M j G:i:s T Y"), $Month);
?>

 <?php
if(isset($_COOKIE['AboutVisit1']))
{
$last = $_COOKIE['AboutVisit1'];
echo "Welcome back! <br> You last visited on ". $last . "<br>";


}
if(isset($_COOKIE['visitCount1'])){


 $cookie = ++$_COOKIE['visitCount1'];


 echo ("You have viewed this page" . $cookie . "times.");
}
else
{
echo "It's your first time on the server!";
setcookie('visitCount1');
}
?>

我使用带有 wamp 服务器和 chrome 的 netbeans 8.1 作为我的浏览器。还有什么办法可以解决这个问题?

如果我只是在浏览器上或通过 netbeans 进行测试,是否真的可以查看 cookie 和会话记录?

我是否必须包含一个 html 标头和正文,或者我可以将它放在 php 正文中吗?

在 php fiddle 上它可以工作(有点),我得到了这个(总是同时):

欢迎回来! 您上次访问时间为 2016 年 8 月 9 日星期二 15:43:00 EST 这是你第一次上服务器!

【问题讨论】:

  • 您发布的代码包含一个空格。看看setcookie('AboutVisit1', date("D M j G:i:s T Y"), $Month);,然后看看你做了什么——你用?&gt;关闭php,然后你有一个空白,然后你再次用&lt;?php打开。

标签: php html cookies header counter


【解决方案1】:

您发布的代码包含一个空格。看:

setcookie('AboutVisit1', date("D M j G:i:s T Y"), $Month);
?>

 <?php

看到关闭和打开之间的空间了吗?

【讨论】:

    【解决方案2】:

    问题是您在发送最后一个 cookie 之前正在回显消息:setcookie('visitCount1'); 在您已经运行 echo "It's your first time on the server!";(或 echo "Welcome back! &lt;br&gt; You last visited on ". $last . "&lt;br&gt;";)之后被发送到浏览器。确保在发送最后一个 cookie 之前不要使用 echo 函数。

    编辑:正如其他人指出的那样,您的代码中也有一个空格。

    【讨论】:

      【解决方案3】:

      鉴于您是全新的,这里有一些提示

      <?php
      
      // if you are going to change the timezone, best to always make this first, so it effects everything in the script
      date_default_timezone_set('EST');
      
      // 3600 seconds = 1 hour not Month
      // give your variables proper names it will help if you get into that habit from day one
      $one_hour = 3600;
      $expires = $one_hour + time();
      
      // check if visit cookie exists and increment it, otherwise, this must be visit 1
      $count = isset($_COOKIE['visitCount1']) ? ++$_COOKIE['visitCount1'] : 1;
      
      // set cookies at the start, before outputting anything
      // i would also recommend setting the path, as that can catch you out if you have a deep directory structure, / will mean the cookie works for the whole site
      setcookie('AboutVisit1', date("D M j G:i:s T Y"), $expires, '/');
      setcookie('visitCount1', $count, $expires, '/');
      
      if(isset($_COOKIE['AboutVisit1']))
      {
          $last_visit = $_COOKIE['AboutVisit1'];
          // you don't need to use . to concatenate variables if you use double "
          echo "Welcome back! <br> You last visited on $last_visit <br>";
      }
      
      if(isset($_COOKIE['visitCount1'])){
          // you dont' need () when you do echo
          // you don't need to use . to concatenate variables if you use double quotes "
          echo "You have viewed this page $count times.";
      } else {
          echo "It's your first time on the server!";
      }
      
      // you don't need trailing ?>
      

      【讨论】:

      • 好的,如果我想再次访问该站点,它不会显示我的计数和上次访问次数,我如何通过浏览器或 netbeans 实现这一点?
      • 在浏览器中检查您的 cookie 并查看设置了什么,该代码已经过测试并且可以正常工作
      【解决方案4】:

      setcookie() 函数应该在你输出任何东西之前被调用。因此,在调用 setcookie 之前,您的代码中的某些部分会输出一些内容。

      【讨论】:

        猜你喜欢
        • 2019-01-14
        • 1970-01-01
        • 2011-11-20
        • 1970-01-01
        • 2018-01-13
        • 1970-01-01
        • 1970-01-01
        • 2022-01-18
        • 1970-01-01
        相关资源
        最近更新 更多