[英]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:一些技巧:
{$_SESSION['count']}th time!
{$_SESSION['count']}th time!
You use double quotes to tell PHP to interpolate variables into the string.?>
at the end of a script.?>
。 It's not needed and can introduce some odd output bugs when you are including scripts. Hopefully you can see that this will only produce one message per visit to the page.希望您可以看到每次访问该页面只会产生一条消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.