严格的标准:不应静态调用非静态方法CountryDetector :: countryMatches()

时间:2014-02-06 05:25:49

标签: php

我试图限制一些国家,并且它完全重定向到受限制的国家/地区用户。但是,当用户不是来自Restrict国家/地区时,我的网站正在加载,但网站顶部显示以下错误消息。

Strict standards: Non-static method CountryDetector::countryMatches() should not be called statically in C:\wamp\www\final\country\CountryDetector.class.php on line 61

我在我的index.php文件中调用这个类:

<?php
    include('/country/CountryDetector.class.php');
    CountryDetector::redirect("DE", "http://google.com");
?>

我正在使用的类库是:

<?php


    include("CountryDetector.interface.php");

    /**
     * Class that lookup's for a country code then it redirects (include files, or callbacks) the user to desired content you want.
     * The function of this class is like Google that redirects users from countries that support google 
     * e.g. in UK when you enter google.com you will be redirected to google.co.uk and this is excatly 
     * what you can do with CountryDetector
     *
     * This class is builded in static context for purpose of best usage


    class CountryDetector implements CountryDetectorInterface
    {
        private static $current_country;

        private static $include_done;


        /** Defined Countries */
        private static $BALKANS = array("AL", "BA","HR", "RS", "ME", "BG", "MK", "GR");

        private static $EUROPE = array("AL", "AD", "AM", "AT", "AZ", "BY", "BE", "BA", "BG", "HR", "CY", "CZ", "CS", "DK", "EE", "FI", "FR", "FX", "GE", "DE", "GR", "HU", "IS", "IE", "IT", "KZ", "LV", "LI", "LT", "LU", "MK", "MT", "MD", "MC", "ME", "NL", "NO", "PL", "PT", "RO", "RU", "SM", "RS", "SI", "ES", "SE", "CH", "TR", "UA", "UK", "VA");

        private static $MIDDLEEAST = array("TR", "BH", "KW", "OM", "QA", "SA", "AE", "YE", "PS", "IQ", "IL. JO", "LB", "SY", "IR", "CY", "EG");

        private static $ASIA = array("AF", "AM", "AZ", "BH", "BD", "BT", "IO", "DN", "KH", "CN", "CX", "CC", "GE", "HK", "IN", "ID", "IR", "IQ", "IL", "JP", "JO", "KZ", "KP", "KR", "KW", "KG", "LA", "LB", "MO", "MY", "MV", "MN", "NP", "OM", "PK", "PS", "PH", "QA", "RU", "SA", "SG", "LK", "SY", "TW", "TJ", "TH", "TP", "AE", "UZ", "VN", "YE");

        private static $AMERICA = array("US", "BZ", "CR", "SV", "GT", "HN", "NI", "PA", "MX", "HT", "CU", "AR", "BO", "BR", "CL", "CO", "EC", "FK", "GF", "GY", "PY", "PE", "SR", "UY", "VE");

        private static $NORTHAMERICA = array("US");

        private static $SOUTHAMERICA = array("AR", "BO", "BR", "CL", "CO", "EC", "FK", "GF", "GY", "PY", "PE", "SR", "UY", "VE");

        private static $CENTRALAMERICA = array("BZ", "CR", "SV", "GT", "HN", "NI", "PA", "MX", "HT", "CU");

        private static $CARIBBEAN = array("AI", "AG", "AW", "BS", "BB", "VG", "KY", "CU", "DM", "DO", "GD", "GP", "HT", "JM", "MQ", "MS", "AN", "PR", "KN", "LC", "VC", "TT", "TC", "VI");


        /**
         * Redirect to URL based on country code
         *
         * @param $country - Country that want to apply this rule
         * @param $url - If country matches then will be redirected to this url
         *
         * @return void
         */

        public static function redirect($country, $url, $except = null)
        {
            self::_lookup();

            $matches = self::countryMatches($country, $except);

            if( $matches )
            {
                if( !@header("Location: $url") )
                {
                    print('<script type="text/javascript"> window.location = "'.$url.'"; </script>');
                }               
            }
        }


        /**
         * Use reserved word `include` to require a file based on country code
         *
         * @param $country - Country that want to apply this rule
         * @param $include_path - If country matches then will be included the selected file
         *
         * @return void
         */

        public static function includeFile($country, $include_path, $except = null)
        {
            self::_lookup();

            $matches = self::countryMatches($country, $except);

            if( $matches )
            {
                include($include_path);
            }
        }


        /**
         * Include Once - It will include just one time file
         *
         * @param $country - Country that want to apply this rule
         * @param $include_path - If country matches then will be included the selected file
         *
         * @return void
         */

        public static function includeFileOnce($country, $include_path, $except = null)
        {
            if( !self::$include_done )
            {
                self::_lookup();

                $matches = self::countryMatches($country, $except);

                if( $matches )
                {
                    include($include_path);
                    self::$include_done = true;
                }
            }
        }


        /**
         * Call's specified function based on country code
         *
         * @param $country - Country that want to apply this rule
         * @param $callback_func - If country matches then the function named $callback_func() will be called (with no parameters)
         *
         * @return void
         */

        public static function callback($country, $callback_func, $except = null)
        {
            self::_lookup();

            $matches = self::countryMatches($country, $except);

            if( $matches )
            {
                call_user_func($callback_func);
            }
        }


        /**
         * Lookup's for a country (based on IP)
         *
         * @return void
         */

        private static function _lookup()
        {
            if( !self::$current_country )
            {
                $remote_addr = $_SERVER['REMOTE_ADDR'];
                $api_url = str_replace("#REMOTE_ADDR#", $remote_addr, self::IP_API_URL);

                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $api_url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $country_code = curl_exec($ch);
                curl_close($ch);

                self::$current_country = strtoupper($country_code);             
            }
        }


        /**
         * Check if country matches with the country parameter
         *
         * @return boolean - true if it matches, false if not
         */

        private function countryMatches($country, $except)
        {
            $cs = $country;

            if( ! is_array($country) )
            {
                switch( strtoupper($country) )
                {
                    case "AMERICA":
                        $country = self::$AMERICA;
                        break;

                    case "ASIA":
                        $country = self::$ASIA;
                        break;

                    case "BALKANS":
                    case "BALKAN":
                        $country = self::$BALKANS;
                        break;

                    case "CARIBBEAN":
                        $country = self::$CARIBBEAN;
                        break;

                    case "CENTRALAMERICA":
                        $country = self::$CENTRALAMERICA;
                        break;

                    case "EUROPE":
                        $country = self::$EUROPE;
                        break;

                    case "MIDDLEEAST":
                    case "MIDDLE EAST":
                        $country = self::$MIDDLEEAST;
                        break;

                    case "NORTHAMERICA":
                    case "UNITED STATES":
                        $country = self::$NORTHAMERICA;
                        break;

                    case "SOUTHAMERICA":
                    case "LATIN AMERICA":
                        $country = self::$SOUTHAMERICA;
                        break;

                    default:
                        $country = array($country);
                }
            }

            if( is_array($except) )
            {
                foreach($except as $except_country)
                {
                    if( $except_country == self::$current_country )
                        return false;
                }
            }

            if( $cs == "*" || strtoupper($cs) == "INTERNATIONAL" )
                return true;

            if( in_array(self::$current_country, $country) && !in_array($except, $country) )
            {
                return true;
            }

            return false;
        }


        /* v1.1 */

        // Get Country Code of current country
        public static function getCountryCode()
        {
            if( ! self::$current_country)
            {
                self::_lookup();
            }

            return self::$current_country;
        }

        public static function is($country_code)
        {
            if( ! self::$current_country)
            {
                self::_lookup();
            }

            $cc = strtolower(self::$current_country);
            $country_code = strtolower($country_code);

            return $cc == $country_code;
        }
    }   
?>

1 个答案:

答案 0 :(得分:0)

你的方法是countryMatches没有静态声明,只是private static function countryMatches($country, $except)