【问题标题】:PHP Fatal error: Cannot redeclare [duplicate]PHP致命错误:无法重新声明[重复]
【发布时间】:2017-05-22 01:26:27
【问题描述】:

我尝试了一个 PHP 脚本,但它总是给我一个错误。我已将脚本添加到我的 .htaccess 中以每次自动加载。

php_value auto_prepend_file /var/www/fantasycf/public/proxycheck.php

无论我尝试什么,我都会收到此错误:

Sat Jan 07 06:40:04.756520 2017] [php7:error] [pid 5269] [client 187.57.43.66:23391] PHP Fatal error:  Cannot redeclare checkProxy() (previously declared in /var/my/private/directory/proxycheck.php:11) in /var/my/private/directory/proxycheck.php on line 11, referer: http://test.com/test.html

我的 PHP 脚本如下:

    <?php
/*
* A PHP function that interacts with http://getIPIntel.net to look up an IP address
* returns TRUE if the IP returns a value greater than $banOnProability,
* FALSE otherwise, including errors
* HTTP error codes are NOT explicitly implemented here
* This should be used as a guide, be sure to edit and test it before using it in production
* MIT License
*/
//function requires curl
function checkProxy($ip){
    $contactEmail="admin@mysite.com"; //you must change this to your own email address
    $timeout=5; //by default, wait no longer than 5 secs for a response
    $banOnProbability=0.99; //if getIPIntel returns a value higher than this, function returns true, set to 0.99 by default

    //init and set cURL options
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    //if you're using custom flags (like flags=m), change the URL below
    curl_setopt($ch, CURLOPT_URL, "http://check.getipintel.net/check.php?ip=$ip&contact=$contactEmail");
    $response=curl_exec($ch);

    curl_close($ch);


    if ($response > $banOnProbability) {
        return true;
    } else {
        if ($response < 0 || strcmp($response, "") == 0 ) {
            //The server returned an error, you might want to do something
            //like write to a log file or email yourself
            //This could be true due to an invalid input or you've exceeded
            //the number of allowed queries. Figure out why this is happening
            //because you aren't protected by the system anymore
            //Leaving this section blank is dangerous because you assume
            //that you're still protected, which is incorrect
            //and you might think GetIPIntel isn't accurate anymore
            //which is also incorrect.
            //failure to implement error handling is bad for the both of us
        }
        return false;
    }
}
$ip=$_SERVER['REMOTE_ADDR'];
if (checkProxy($ip)) {
    /* A proxy has been detected based on your criteria
     * Do whatever you want to here
     */
    echo '<META HTTP-EQUIV="Refresh" Content="0; URL=google.com">';    
    exit;
}

?>

希望你能帮上忙。

亲切的问候。

【问题讨论】:

  • 您是在使用require 还是include 两次?

标签: php .htaccess


【解决方案1】:

使用requireinclude 两次时会出现此问题。这可以通过查找使用该文件的所有 includerequire 实例并确保使用以下方法进行更改来解决:

require_once "filename";

另一种方法是检查函数是否已经声明,如果是,不要重新声明。你可以使用function_exists()

if (!function_exists("checkProxy")) {
  function checkProxy($ip) {
    // rest of function code
  }
}

【讨论】:

    【解决方案2】:

    将你的函数声明放在 if 语句中

    if(!function_exists('checkProxy'))
    {
        /*
    * A PHP function that interacts with http://getIPIntel.net to look up an IP address
    * returns TRUE if the IP returns a value greater than $banOnProability,
    * FALSE otherwise, including errors
    * HTTP error codes are NOT explicitly implemented here
    * This should be used as a guide, be sure to edit and test it before using it in production
    * MIT License
    */
    //function requires curl
    function checkProxy($ip){
        $contactEmail="admin@mysite.com"; //you must change this to your own email address
        $timeout=5; //by default, wait no longer than 5 secs for a response
        $banOnProbability=0.99; //if getIPIntel returns a value higher than this, function returns true, set to 0.99 by default
    
        //init and set cURL options
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        //if you're using custom flags (like flags=m), change the URL below
        curl_setopt($ch, CURLOPT_URL, "http://check.getipintel.net/check.php?ip=$ip&contact=$contactEmail");
        $response=curl_exec($ch);
    
        curl_close($ch);
    
    
        if ($response > $banOnProbability) {
            return true;
        } else {
            if ($response < 0 || strcmp($response, "") == 0 ) {
                //The server returned an error, you might want to do something
                //like write to a log file or email yourself
                //This could be true due to an invalid input or you've exceeded
                //the number of allowed queries. Figure out why this is happening
                //because you aren't protected by the system anymore
                //Leaving this section blank is dangerous because you assume
                //that you're still protected, which is incorrect
                //and you might think GetIPIntel isn't accurate anymore
                //which is also incorrect.
                //failure to implement error handling is bad for the both of us
            }
            return false;
        }
    }
    $ip=$_SERVER['REMOTE_ADDR'];
    if (checkProxy($ip)) {
        /* A proxy has been detected based on your criteria
         * Do whatever you want to here
         */
        echo '<META HTTP-EQUIV="Refresh" Content="0; URL=google.com">';    
        exit;
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-17
      • 2014-01-13
      • 2010-10-17
      • 2014-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多