【问题标题】:Header redirect if user agent equals variables using PHP如果用户代理等于使用 PHP 的变量,则标头重定向
【发布时间】:2019-02-07 13:58:34
【问题描述】:

如果用户代理等于变量,我有一个脚本必须重定向到 403 页面,但如果不是 - 必须显示正常页面。而不是这个脚本只显示空白页,仅此而已。请帮我解决我的问题或我做错了什么。

这是脚本:

<?php

 //-- Get user agent
 //-- Thanks @creditosrapidos10min for hint about strtolower() 
 $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);

 //-- BadBot variable
 $Baiduspider = stripos($useragent, "Baiduspider");
 $DotBot = stripos($useragent, "DotBot");

 //-- BadBot constant
 $BADBOT = ($Baiduspider||$DotBot);

if ($agent == $BADBOT){

    header("Location: ohno/403.php");
    exit;


} else { ?>

 Display home page

 <?php }?>

【问题讨论】:

    标签: php redirect header user-agent


    【解决方案1】:

    尝试使用 $HTTP_SERVER_VARS 而不是 $_SERVER,以免全局变量出现问题。

    如果没有,请尝试使用 strtolower:

    <?php
    
     //-- Get user agent
     $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
    
     //-- BadBot variable
     $Baiduspider = stripos($useragent, "baiduspider");
     $DotBot = stripos($useragent, "dotbot");
    
     //-- BadBot constant
     $BADBOT = ($Baiduspider||$DotBot);
    
    if ($agent == $BADBOT){
    
        header("Location: ohno/403.php");
        exit;
    
    
    } else { ?>
    
     Display home page
    
     <?php }?>
    

    【讨论】:

    • 非常感谢,但仍然没有效果...只需将所有用户代理重定向到第 403 页(..您可以在 sauna.ho.ua/badbot.php 看到它
    【解决方案2】:

    我认为您使用了$user 而不是$user_agent

    根据php手册php manual on stipos你应该使用triple = like ===。

    这是一个应该如何做的例子。

    <?php
        //-- Get user agent
        //-- Thanks @creditosrapidos10min for hint about strtolower() 
        $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
    
        //-- BadBot variable
        $Baiduspider = stripos($useragent, "Baiduspider");
        $DotBot = stripos($useragent, "DotBot");
    
        //-- BadBot constant
        $BADBOT = ($Baiduspider||$DotBot);
    
        if ($useragent === $BADBOT){
    
        header("Location: ohno/403.php");
        exit;
    
        } else { ?>
    
        Display home page
    
        <?php }
        ?>
    

    【讨论】:

    • no... === 不工作.. 因为 === 必须为假或真...
    【解决方案3】:

    您在$useragent 上使用了stripos,但您还没有定义$useragent,您只定义了$agent。请尝试更正此问题并重试。

    【讨论】:

    • 对不起..我的错误..我改变它...但仍然没有解决我的问题(
    【解决方案4】:

    在我的浏览器上工作!也许它不是您的浏览器的代码?我正在使用 Opera 浏览器

    【讨论】:

    • 你的屏幕截图来自我的页面,脚本代码使用 ==,而不是 === ;)...没关系..谢谢您的回答...您的示例非常完美。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    相关资源
    最近更新 更多