douzhao7445 2011-02-05 20:24
浏览 96

使用PHP $ _SERVER [HTTP_REFERER]显示自定义欢迎消息/内容

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"; 
} 
?>

Tried using

<?php 
if(isset($_SERVER['HTTP_REFERER'])) {
    echo $_SERVER['HTTP_REFERER'];
}
?>

to get this to work but no luck.

  • 写回答

2条回答 默认 最新

  • duanqun9740 2011-02-05 20:26
    关注

    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";
    }
    

    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";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题