使用 PHP $_SERVER[HTTP_REFERER] 显示自定义欢迎消息/内容 [英] Using PHP $_SERVER[HTTP_REFERER] to display custom welcome messages/content
问题描述
我正在尝试根据访问者来自哪个社交媒体网站向访问者显示自定义欢迎信息.我无法让这个字符串工作,但无论引用站点是什么,它都会回显.谁能帮我让它工作?非常感谢!
I'm trying to display a custom welcome message to visitors based off what social media site they are coming in from. I can't get this string to work but it is echoing back no matter what the refering site is. Can anyone help me get this to work? Many thanks!
<?php
if (strpos("twitter.com",$_SERVER[HTTP_REFERER])==0) {
echo "Welcome, Twitter User! If you enjoy this post, don't hesitate to retweet it to your followers";
}
?>
尝试使用
<?php
if(isset($_SERVER['HTTP_REFERER'])) {
echo $_SERVER['HTTP_REFERER'];
}
?>
让它工作但没有运气.
推荐答案
strpos
当搜索词没有出现在字符串中时返回 false.与布尔值相比,0 的计算结果也为 false.所以 false == 0,你的代码总是运行.
strpos
returns false when the search word does not appear in the string. 0 also evaluates to false when compared to a boolean. So false == 0, and your code always runs.
使用严格比较来要求值和类型都匹配而不是 ==
Use a strict comparison to require both value and type match instead of ==
if (strpos("twitter.com", $_SERVER['HTTP_REFERER']) === 0) {
echo "Welcome, Twitter User! If you enjoy this post, don't hesitate to retweet it to your followers";
}
但是,引荐来源不会以 twitter.com 开头,而是以http://
或https://
开头所以你的情况一开始就不对.要在字符串中的任何其他位置搜索 twitter.com
:
However, the referrer will not start with twitter.com, it'll start with http://
or https://
so your condition wasn't right in the first place. To search for twitter.com
anywhere else in the string:
if (strpos("twitter.com", $_SERVER['HTTP_REFERER']) !== false) {
echo "Welcome, Twitter User! If you enjoy this post, don't hesitate to retweet it to your followers";
}
这篇关于使用 PHP $_SERVER[HTTP_REFERER] 显示自定义欢迎消息/内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!