首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在codeigniter中获取当前位置和网络IP?

如何在codeigniter中获取当前位置和网络IP?
EN

Stack Overflow用户
提问于 2015-08-28 12:02:54
回答 3查看 13.8K关注 0票数 2

当员工登录时,我们需要自动检索当前位置并插入它,它是一个在线打卡system.So,只是为了确保从其他地方打进/打出不会误用进出。使用下面提到的Gaurang Joshi的http://ipinfo.io/,我得到了互联网的IP和latitude和logitudes.So,我需要使用任何谷歌地图库来获得具体的位置吗?如何实现它?任何帮助。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-08-28 13:20:55

服务器IP

通过$_SERVER['SERVER_ADDR']获取服务器IP地址

本地IP

通过$_SERVER['REMOTE_ADDR']获取客户端IP

要查找位置,只需执行以下操作:

代码语言:javascript
复制
//ipinfo grabs the ip of the person requesting

 $getloc = json_decode(file_get_contents("http://ipinfo.io/"));

 echo $getloc->city; //to get city

如果您使用的是坐标,则它以单个字符串形式返回,如32,-72 so,为此,您可以使用:

代码语言:javascript
复制
$coordinates = explode(",", $getloc->loc); // -> '32,-72' becomes'32','-72'
echo $coordinates[0]; // latitude
echo $coordinates[1]; // longitude
票数 5
EN

Stack Overflow用户

发布于 2017-06-06 21:42:35

代码语言:javascript
复制
public function index(){

    $ip = $_SERVER['REMOTE_ADDR'];
    //$location = file_get_contents('http://freegeoip.net/json/'.$_SERVER['REMOTE_ADDR']);
    $location = file_get_contents('http://ip-api.com/json/'.$ip);
    //you can also use ipinfo.io or any other ip location provider API
    //print_r($location);exit;
    $data_loc = json_decode($location);
    $marker = array();
    $data = array(
                'view_file'=>'index',
                'current_menu'=>'index',
                'site_title' =>'site title',
                'logo'      => 'logo',
                'title'=>'Family Owned Pest Control',
            );
    $Lat = '';
    $Lon = '';
    if(isset($_GET['searchsubmit']))
    {
        $Address = $this->input->get('addr_loc'); 
        $jobtitle = $this->input->get('jobtitle'); 

        $request_url = "//maps.googleapis.com/maps/api/geocode/xml?address=".$Address."&sensor=true";
        $xml = simplexml_load_file($request_url) or die("url not loading");

        $status = $xml->status;

        if ($status=="OK") {
            $Lat = $xml->result->geometry->location->lat;
            $Lon = $xml->result->geometry->location->lng;
        }
    }

    if(empty($Lat) AND empty($Lon))
    {
        $config['center'] = 'auto';
        $marker['position'] = $data_loc->lat.",". $data_loc->lon ;
        $Lat = $data_loc->lat;
        $Lon = $data_loc->lon;
    }
    else{
        $config['center'] ='auto';
        $marker['position'] = $Lat.",". $Lon ;
    }

    $config['zoom'] = '11';
    $config['places'] = TRUE;
    $config['placesAutocompleteInputID'] = 'myPlaceTextBox';
    $config['placesAutocompleteBoundsMap'] = TRUE; // set results biased towards the maps viewport
    $this->googlemaps->initialize($config);
    $marker['infowindow_content'] =  $data_loc->city.'<br>'.$data_loc->regionName.'<br>'.$data_loc->country;
    $marker['icon'] = '//chart.apis.google.com/chart?chst=d_map_pin_letter&chld=A|9999FF|000000';
    $this->googlemaps->add_marker($marker);
    $Lat1 = $Lat+0.05;
    $Lon1 = $Lon+0.05;
    $marker = array();
    $marker['position'] = $Lat1.",". $Lon1;
    $marker['draggable'] = TRUE;
    $marker['animation'] = 'DROP';
    $marker['infowindow_content'] =  'dskjfhdsjhfsd' ;
    $marker['icon'] = '//chart.apis.google.com/chart?chst=d_map_pin_letter&chld=B|9999FF|000000';
    $this->googlemaps->add_marker($marker);
    $Lat2 = $Lat1+0.05;
    $Lon2 = $Lon1+0.05;
    $marker = array();
    $marker['position'] = $Lat2.",". $Lon2;
    $marker['draggable'] = TRUE;
    $marker['animation'] = 'DROP';
    $marker['infowindow_content'] =  'dskjfhdsjdsfsdfhfsd' ;
    $marker['icon'] = '//chart.apis.google.com/chart?chst=d_map_pin_letter&chld=C|9999FF|000000';
    $this->googlemaps->add_marker($marker);


    $data['content']='content/peta';

    $data['map'] = $this->googlemaps->create_map();

    $this->template->load_index_template($data);
}
票数 2
EN

Stack Overflow用户

发布于 2020-03-07 14:07:54

您可以像这样使用codeigniter库https://github.com/4riel/Geoip-codeigniter

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32263142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档