【问题标题】:Using Cookies in PHP在 PHP 中使用 Cookie
【发布时间】:2020-05-06 09:02:51
【问题描述】:

我有一个代码可以计算用户访问页面的次数。当有人第 5 次访问时,我试图显示一条特殊消息。

   <?php
session_start();
if (empty($_SESSION['count'])) {
    $_SESSION['count'] = 1;
} if($_SESSION['count'] === 5){
    echo '<p> This is your' . $_SESSION['count'] . 'th time! Glad to have you back! </p>';
}if($_SESSION['count'] === 10) {
    echo '<p> This is your' . $_SESSION['count'] . 'th time! You must love it here</p>';
}if($_SESSION['count'] === 20){
    echo '<p> Hi again, this is your' . $_SESSION['count'] . 'th time! This will restart the page. Thanks for visiting so much! </p>';
    $_SESSION['count'] = 0;
} else {
    $_SESSION['count']++;
}

?>

<p> Hello Visitor! You have seen this page <?php echo $_SESSION['count'];?> times!</p>

【问题讨论】:

  • 您的问题或问题是什么?
  • 我正在尝试最多跟踪 20 个访客,然后重置计数器。我有代码,但是当我运行时,我得到了这个。它似乎在跟踪 1,我不知道为什么.. 这是你的第 10 次!你一定喜欢这里你好访客!你已经看过这个页面 11 次了!
  • 这是你第十次了!你一定喜欢这里你好访客!你已经看过这个页面 11 次了!
  • 您应该使用当前代码更新您的问题。
  • 好的,我更新了!让我知道你是否能帮我弄清楚为什么它的跟踪落后..

标签: php cookies


【解决方案1】:

只需像这样使用 if 语句

if($_SESSION['count'] >= 5){
    echo '<p> Hello Visitor! You have seen this page ' . $_SESSION['count'] . ' times!</p>';
}

【讨论】:

  • 当我添加此代码时,它会跟踪它并且几乎可以工作,但它落后了 1 个数字 这是您的第 10 次!你一定喜欢这里你好访客!你已经看过这个页面 11 次了!
  • 这是因为您每次刷新页面时都会将会话计数更新为 1。你可以使用 $_SESSION['count']++;显示消息后的部分代码,或者您可以执行此操作 $_SESSION['count'] -1;当您回显消息时。
  • 我尝试添加它,它说表达式未在任何地方使用..
【解决方案2】:

这个问题有点变形,所以我解释你想要什么,因为它不清楚。我解释说你想要互斥的消息传递。您可以使用 if-then-else 语句链来做到这一点。

<?php
  session_start();
  if (empty($_SESSION['count'])) {
    $_SESSION['count'] = 1;
  } else {
    $_SESSION['count']++;    
  }

  // Display message
  if ($_SESSION['count'] == 5){
    echo "<p>This is your {$_SESSION['count']}th time! Glad to have you back!</p>";
  } else if ($_SESSION['count'] == 10) {
    echo "<p>This is your {$_SESSION['count']}th time! You must love it here</p>";
  } else if ($_SESSION['count'] == 20) {
    echo "<p>Hi again, this is your {$_SESSION['count']}th time! This will restart the page. Thanks for visiting so much!</p>";
    $_SESSION['count'] = 0;
  } else {
    echo "<p>Hello Visitor! You have seen this page {$_SESSION['count']} times!</p>";
  } 

更简洁的方法是使用 switch 语句。

<?php
  session_start();
  if (empty($_SESSION['count'])) {
    $_SESSION['count'] = 1;
  } else {
    $_SESSION['count']++;    
  }

  // Display message
  switch ($_SESSION['count']) {
    case 5:
      echo "<p>This is your {$_SESSION['count']}th time! Glad to have you back!</p>";
      break;
    case 10:
      echo "<p>This is your {$_SESSION['count']}th time! You must love it here</p>";
      break
    case 20:
      echo "<p>Hi again, this is your {$_SESSION['count']}th time! This will restart the page. Thanks for visiting so much!</p>";
      $_SESSION['count'] = 0;
      break
    default:
      echo "<p>Hello Visitor! You have seen this page {$_SESSION['count']} times!</p>";      
  }

一些提示:

  1. 没有理由计算 5,10,20 条消息,因为您使用相等,所以它们必须是第 5、10、20 条。那是浪费计算。它也不是 DRY 代码。如果您有一个输出该消息的函数而不是本质上相同的计算代码,那就更好了。
  2. 我使用了字符串插值,因为与大量字符串连接相比,它更易于阅读且更易于更新。当您有一个带有单引号的数组时,您可以通过将变量包装在 {} 中来完成这项工作,就像我在这里所做的那样:{$_SESSION['count']}th time! 您使用双引号告诉 PHP 将变量插入到字符串中。
  3. 不要在脚本末尾使用 php 结束标记 ?&gt;。它不是必需的,并且在包含脚本时可能会引入一些奇怪的输出错误。

希望您可以看到每次访问该页面只会产生一条消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    相关资源
    最近更新 更多