创建PHP函数

时间:2015-05-31 20:51:26

标签: php

所以我正在尝试创建一个PHP函数来跟踪我的访问者。我想要它的功能的原因是因为我不想将代码复制/粘贴到我将使用跟踪器的每个页面。现在我必须承认PHP函数是我最弱的。任何帮助将不胜感激。

这是我目前的职能:

function visitor_ip_data($ip_city) {

    if(!isset($_SESSION['ip_country']) || !isset($_SESSION['ip_city']) || !isset($_SESSION['ip_postal']) || !isset($_SESSION['ip_region']) || !isset($_SESSION['ip_isp']) ) {
        //Niet geset, haal nieuwe IP data op
        $url = file_get_contents('http://api.ipaddresslabs.com/iplocation/v1.8/locateip?key=MY-KEY-GOES-HERE&ip='.$_SERVER['REMOTE_ADDR'].'&format=JSON');
        $html = json_decode($url);

        if ($html->query_status->query_status_code != 'OK') {
            //API data ERROR
            //Destroy the SESSIONS
            unset($_SESSION['ip_city']);
            unset($_SESSION['ip_postal']);
            unset($_SESSION['ip_region']);
            unset($_SESSION['ip_country']);
            unset($_SESSION['ip_isp']);

            //echo "Error! ".$html->query_status->query_status_description." ";
            //die();
        } else {
            //API data SUCCESS
            //Create SESSIONS
            $feed = $html->geolocation_data;

            $_SESSION['ip_city'] = $feed->city;
            $_SESSION['ip_postal'] = $feed->postal_code;
            $_SESSION['ip_region'] = $feed->region_name;
            $_SESSION['ip_country'] = $feed->country_name;
            $_SESSION['ip_isp'] = $feed->isp;

            $ip_city = $_SESSION['ip_city'];
            $ip_postal = $_SESSION['ip_postal'];
            $ip_region = $_SESSION['ip_region'];
            $ip_country = $_SESSION['ip_country'];
            $ip_isp = $_SESSION['ip_isp'];

            //echo "Gathered IP data from API";
            //die();
        } 

    } else {
        //IP data are already in sessions
        //use the sessions
        $ip_city = $_SESSION['ip_city'];
        $ip_postal = $_SESSION['ip_postal'];
        $ip_region = $_SESSION['ip_region'];
        $ip_country = $_SESSION['ip_country'];
        $ip_isp = $_SESSION['ip_isp'];

        //echo "Using IP data from sessions";
        //die();
    }

}

我要做的是使用该函数回显页面上的$ ip_city。部分页面代码如下:

echo visitor_ip_data($ip_city); 

但是这给了我Undefined Variable错误消息。现在我想知道它为什么这样做。我错过了什么或我做错了什么?

感谢你。

1 个答案:

答案 0 :(得分:0)

你不能从功能中归还任何东西。

从函数定义中删除参数:function visitor_ip_data()

      //echo "Error! ".$html->query_status->query_status_description." ";
      return NULL;

    } else {
        //API data SUCCESS
        //Create SESSIONS
        $feed = $html->geolocation_data;

        $_SESSION['ip_city'] = $feed->city;
        $_SESSION['ip_postal'] = $feed->postal_code;
        $_SESSION['ip_region'] = $feed->region_name;
        $_SESSION['ip_country'] = $feed->country_name;
        $_SESSION['ip_isp'] = $feed->isp;

        $data['ip_city'] =  = $_SESSION['ip_city'];
        $data['ip_postal'] = $_SESSION['ip_postal'];
        $data['ip_region'] = $_SESSION['ip_region'];
        $data['ip_country'] = $_SESSION['ip_country'];
        $data['ip_isp'] = $_SESSION['ip_isp'];

        //echo "Gathered IP data from API";
        //die();
    } 

} else {
    //IP data are already in sessions
    //use the sessions
    $data['ip_city'] = $_SESSION['ip_city'];
    $data['ip_postal'] = $_SESSION['ip_postal'];
    $data['ip_region'] = $_SESSION['ip_region'];
    $data['ip_country'] = $_SESSION['ip_country'];
    $data['ip_isp'] = $_SESSION['ip_isp'];

    //echo "Using IP data from sessions";
    //die();
    }
    return $data;

然后

if (($data = visitor_ip_data()) == NULL) echo 'error';
else echo $data['ip_city'];