Header 如果用户代理使用 PHP 等于变量则重定向

Header redirect if user agent equals variables using PHP

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

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

尝试使用 $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 }?>

我想你用了 $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 }
    ?>

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