0

I'm trying to get the IP and location data of my visitors but I'm getting an error.

My Code works on localhost but refer no data but not on my online server there is the code:

<?php
//GET th passed data
$page = $_GET['page'];
//GET VISITOR IP
function get_ip(){
    if(isset($_SERVER['HTTP_CLIENT_IP'])){
        return $_SERVER['HTTP_CLIENT_IP'];
    }elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
        return $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else{
        return (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '');
    }
}
$ip =get_ip();
if($ip){
    $query = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$ip));
    if($query){    
        //getting th variabales
        $country = $query['geoplugin_countryName'];
        $countrycode = $query['geoplugin_countryCode'];
        $city = $query['geoplugin_city'];
        $regionname = $query['geoplugin_regionName'];
        $lat =  $query['geoplugin_latitude'];
        $long = $query['geoplugin_longitude'];
        $timezone = $query['geoplugin_timezone'];
        //move the variabales
        echo "<script> window.location.assign('../".$page.".php?IP=".$ip."&country=".$country."&countrycode=".$countrycode."&city=".$city."&latitude=".$lat."&longtitude=".$long."&timez=".$timezone."&regionName=".$regionname."&gettingdata= success');</script>";
    }else{
        echo "<script type='text/javascript'>alert('حدث خطأ ما والرجاء المحاولة في ما بعد')</script>";
        echo "<script> window.location.assign('../index.php?getting data = failed');</script>"; 
    } 
}else{
        echo "<script type='text/javascript'>alert('حدث خطأ يرجى المحاولة في ما بعد')</script>";
        echo "<script> window.location.assign('../index.php?getting IP= failed');</script>"; 
}
?>
Fabian Bettag
  • 799
  • 12
  • 22
  • 1
    Do you have any error? – Simone Rossaini Jan 30 '20 at 13:19
  • 1
    What do you mean by "not working"? Do you get any errors? Add [error reporting](//php.net/manual/function.error-reporting.php) at the top of your file(s): `ini_set("display_errors", 1); error_reporting(E_ALL);` and tell us what you get. – Machavity Jan 30 '20 at 13:22
  • Instead of using `file_get_contents`, you should switch to cURL. It's a lot more configurable, and easier to get error messages out of. And it works when `file_get_contents` cannot get URLs. – aynber Jan 30 '20 at 13:38
  • @Machavity , it give this: file_get_contents(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /home/menuelis/public_html/php/data.php on line 18 – Mohammad hamad Jan 30 '20 at 13:59
  • @aynber , which function shoud be used is it curl_getinfo, thank u – Mohammad hamad Jan 30 '20 at 14:06
  • No, that is just one function of the [cURL library](https://www.php.net/manual/en/book.curl.php) – aynber Jan 30 '20 at 14:07
  • @aynber excuse me, but i dont know very much about curl so when i replace file_get_contents() what should I put instead of it – Mohammad hamad Jan 30 '20 at 14:14
  • thank you guys, i have changed allow_url_fopen setting and its worked – Mohammad hamad Jan 30 '20 at 14:46

1 Answers1

0

Most likely your live server doesn't allow file_get_contents to load files via HTTP, which is a security measure.

You should look into your php.inito find the value set for allow_url_fopen

A similar problem was solved here

Fabian Bettag
  • 799
  • 12
  • 22