1

如果用户代理等于变量,我有一个脚本必须重定向到 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 }?>
4

4 回答 4

2

尝试使用 $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 }?>
于 2019-02-07T14:15:14.573 回答
1

您正在使用striposon$useragent但尚未定义$useragent,您只定义了$agent. 尝试更正此问题并重试。

于 2019-02-07T14:02:04.120 回答
1

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

根据 php manual php manual on stipos,您应该使用三重 = 像 ===。

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

<?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 }
    ?>
于 2019-02-07T14:41:21.273 回答
0

在我的浏览器上运行!也许它不是您的浏览器的代码?我正在使用 Opera 浏览器 在此处输入图像描述

于 2019-02-07T15:14:18.883 回答