检查特定访客是否设置了COOKIE / null

时间:2015-10-01 04:09:44

标签: php cookies geolocation

首先,我想检查cookie是否为null。如果是null,则运行条件以尝试检索访问者IP。如果设置了cookie,则它将检查该访问者的国家/地区并打印特定的基于地区的消息。目前,当我运行脚本时,我所描述的实际情况都没有发生:

<?php
function ip_visitor_country()
    {
        GLOBAL $country ;

        if(isset($_COOKIE["country"])=='') {

            $client  = @$_SERVER['HTTP_CLIENT_IP'];
            $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
            $remote  = $_SERVER['REMOTE_ADDR'];
            //$remote ='1.6.0.0';
            $country  = "Unknown";
            setcookie('country',$country,time()+(86400));

            if(filter_var($client, FILTER_VALIDATE_IP)) {
                $ip = $client;
            }
            elseif(filter_var($forward, FILTER_VALIDATE_IP)) {
                $ip = $forward;
            }
            else {
                $ip = $remote;
            }

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "http://www.geoplugin.net/json.gp?ip=".$ip);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            $ip_data_in = curl_exec($ch); // string
            curl_close($ch);

            $ip_data = json_decode($ip_data_in,true);
            $ip_data = str_replace('&quot;', '"', $ip_data); // for PHP 5.2 see stackoverflow.com/questions/3110487/
            if($ip_data && $ip_data['geoplugin_countryName'] != null) {
                $country = $ip_data['geoplugin_countryName'];
            }
        }
        else {
            //  return 'IP: '.$ip.' # Country: '.$country;
            echo $country;
            if($country=='India') {
                echo'Msg';
            }
            else {
                echo'Msg1';
            }
        }
    }

echo ip_visitor_country(); // output Coutry name
?>

1 个答案:

答案 0 :(得分:2)

看看这是不是你想要做的。这很难说,但是你的剧本在关键路径方面似乎到处都是:

<?php
// Create a cURL function to accept a URL. Helps keep your code clean
function cURL($url = false)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $response = curl_exec($ch); // string
        curl_close($ch);
        // for PHP 5.2 see stackoverflow.com/questions/3110487/
        // Convert your json to an array here
        return (!empty($response))? json_decode(str_replace('&quot;','"',$response),true) : false;
    }
// This will both set and return the country to and from cookie
function set_country_cookie()
    {
        // Globals are not great, but do it here if you
        // are going to use it
        global $country;
        // Assign the ip defaults here
        // Don't suppress errors, fix them
        $client     =   (!empty($_SERVER['HTTP_CLIENT_IP']))? $_SERVER['HTTP_CLIENT_IP'] : false;
        $forward    =   (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))? $_SERVER['HTTP_X_FORWARDED_FOR'] : false;
        $remote     =   $_SERVER['REMOTE_ADDR'];
        // Get the ip
        if(filter_var($client, FILTER_VALIDATE_IP))
            $ip = $client;
        elseif(filter_var($forward, FILTER_VALIDATE_IP))
            $ip = $forward;
        else
            $ip = $remote;
        // Apply the handy dandy cURL function
        $ip_data    =   cURL("http://www.geoplugin.net/json.gp?ip=".$ip);
        // Assign country here
        if(!empty($ip_data['geoplugin_countryName']))
            $country    =   $ip_data['geoplugin_countryName'];
        // Check that the return is not empty
        if(!empty($country))
            setcookie('country',$country,time()+(86400));
        // Return the country, even after setting cookie
        return $country;
    }
// This is the main function that applies all the others
function ip_visitor_country()
    {
        // If you want to echo the function, use a buffer here
        ob_start();
        // Call your...ugh...global
        global $country;
        // Get the country from the cookie or create a new one
        $country    =   (empty($_COOKIE["country"]))? set_country_cookie() : $_COOKIE['country'];
        // Echo your country-specific message
        echo ($country == 'India')? 'You get a message specific to India' : $country." is not as cool as India";
        // Get the contents of the buffer
        $data   =   ob_get_contents();
        ob_end_clean();
        // Return the contents
        return $data;
    }

// Echo the message
echo ip_visitor_country();
// See the results of the cookie
print_r($_COOKIE['country']);

就我而言,这就是我所得到的:

  

美国并不像印度那样酷。   美国