简体   繁体   English

在 PHP 中使用 Cookies

[英]Using Cookies in PHP

I have a code that counts how many time a user visited a page.我有一个代码可以计算用户访问页面的次数。 I am trying to display a special message when somebody visits for the 5th time.当有人第 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>

Just use a if statement like so只需像这样使用 if 语句

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

This question has morphed a bit, so I am interpreting what you want because it is not clear.这个问题有点变形,所以我正在解释你想要什么,因为它不清楚。 I interpreted that you want mutually exclusive messaging.我解释说你想要互斥的消息传递。 You do that with an if-then-else chain of statements.您可以使用 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>";
  } 

A cleaner way of doing this is to use a switch statement.一种更简洁的方法是使用 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>";      
  }

Some tips:一些技巧:

  1. There is no reason to compute the 5,10,20 messages because you are using equality, so they have to be 5th, 10th, 20th.没有理由计算 5,10,20 条消息,因为您正在使用相等,所以它们必须是第 5、10、20 条。 That is wasted computation.那是浪费计算。 It's also not DRY code.它也不是 DRY 代码。 It would be better if you had a function that output that message rather than essentially the same computing code.如果你有一个 function 和 output 那个消息,而不是基本上相同的计算代码,那就更好了。
  2. I used string interpolation, as that reads easier and is easier to update when compared to a lot of string concatenation.我使用了字符串插值,因为与许多字符串连接相比,它更容易阅读并且更容易更新。 When you have an array with single quotes in it, you make that work by wrapping the variable in {} as I did here: {$_SESSION['count']}th time!当您有一个带有单引号的数组时,您可以通过将变量包装在 {} 中来完成这项工作,就像我在这里所做的那样: {$_SESSION['count']}th time! You use double quotes to tell PHP to interpolate variables into the string.您使用双引号告诉 PHP 将变量插入到字符串中。
  3. Don't use the php end tag ?> at the end of a script.不要在脚本末尾使用 php 结束标记?> It's not needed and can introduce some odd output bugs when you are including scripts.它不是必需的,并且在包含脚本时可能会引入一些奇怪的 output 错误。

Hopefully you can see that this will only produce one message per visit to the page.希望您可以看到每次访问该页面只会产生一条消息。

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

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