json_decode return null i guess - php

I try to run this on my server:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
echo $ip;
$city = $details -> city;
echo $city;
?>
But, this print ip only.
Maybe a server problem or configuration?

You need to code a bit more defensively, if that site has no data for the ip address you give it, then it wont return any city property
This is a little safer
$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
echo $ip . ' ';
if (isset($details->city)){
echo $details->city;
} else {
echo 'data not available';
}
Judging from what it returned for my IP Address, the details it provides are not very accurate anyway

Related

why is geoplugin api doesnt work on online server?

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>";
}
?>
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

why geoip not works for me

I am trying to working with geoip to display Ips and their Countries . I am trying to display this code but nothing comes up?
$country = geoip_country_name_by_name($ip);
You must have the GeoIP functions installed first. If they are installed, then it could be possible, that the IP you're providing to the function does not exist in the database.
Try this code:
<?php
$country = geoip_country_name_by_name($ip);
if ($country) {
echo 'This host is located in: ' . $country;
} else {
echo 'Cannot find the IP in the database.'
}

Get country and city with HTTPS

I am currently using the code below but I want an equivalent that uses HTTPS so that the browser does say that my page contains unsecured information.
<?php
$user_ip = getenv('REMOTE_ADDR');
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));
if(isset($geo)) {
$city = $geo["geoplugin_city"];
$region = $geo["geoplugin_regionName"];
$country = $geo["geoplugin_countryName"];
}
elseif(!isset($geo)){
$city = "NaN";
$region = "NaN";
$country = "NaN";
}
?>
Ideas?
It looks like Geoplugin.net requires a valid API key to access their API via HTTPS (therefore possibly only allowing premium users to access data via HTTPS).
The best choice for you will be contacting them about your enquiry.

php geo ip loc does not work

http://chir.ag/projects/geoiploc/
Hello, I am trying to set this up, but whenever I use include("geoiploc.php"); the page is blank and whenever I remove include("geoiploc.php"); I only see my IP address. I've uploaded geoiploc.php and index.php to a webhost that can run PHP.
If there is ANY other easier way to show country name by IP or any other way, which is it? Please I need this fast
So as I said, it only shows my IP whenever I remove include("geoiploc.php"); why?
You need this library: http://chir.ag/projects/geoiploc/autogen/geoiploc.tar.gz and this is the code to run the script:
include("geoiploc.php"); // Must include this
// ip must be of the form "192.168.1.100"
// you may load this from a database
$ip = $_SERVER["REMOTE_ADDR"];
echo "Your IP Address is: " . $ip . "<br />";
echo "Your Country is: ";
// returns country code by default
echo getCountryFromIP($ip);
echo "<br />\n";
// optionally, you can specify the return type
// type can be "code" (default), "abbr", "name"
echo "Your Country Code is: ";
echo getCountryFromIP($ip, "code");
echo "<br />\n";
// print country abbreviation - case insensitive
echo "Your Country Abbreviation is: ";
echo getCountryFromIP($ip, "AbBr");
echo "<br />\n";
// full name of country - spaces are trimmed
echo "Your Country Name is: ";
echo getCountryFromIP($ip, " NamE ");
echo "<br />\n";
You can use the free MaxMind Geo Lite. Download the files here: http://www.maxmind.com/download/geoip/api/php/php-latest.tar.gz
Then download the Geo Country database from here: http://dev.maxmind.com/geoip/legacy/geolite
You can now use it like this:
<?php
include("geoip.inc");
function ipAddress(){
if (!empty($_SERVER['HTTP_CLIENT_IP'])){ //check ip from share internet
$ip=$_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ // proxy pass ip
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
$gi = geoip_open("path/to/GeoIP.dat",GEOIP_STANDARD);
echo geoip_country_name_by_addr($gi, ipAddress());
// echo geoip_country_code_by_addr($gi, ipAddress()); <-- country code
geoip_close($gi);
?>
UPDATE: to get the user's city you should download the top link and look for the file called sample_city.php to see some example code. You'll need to download this file: http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz and place it in the same directory as your php file. A quick example would be:
<?php
include("geoipcity.inc");
function ipAddress(){
if (!empty($_SERVER['HTTP_CLIENT_IP'])){ //check ip from share internet
$ip=$_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ // proxy pass ip
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
$gi = geoip_open("GeoIPCity.dat",GEOIP_STANDARD);
$record = geoip_record_by_addr($gi,ipAddress());
print $record->country_code . " " . $record->country_code3 . " " . $record->country_name . "\n";
geoip_close($gi);
?>

PHP to return gethostbyname($lookup) value

I am using the code below (simplified version) to determine if my IPs are on a blacklist. I need to modify it to be able to determine if an IP is on a Whitelist. The function will require me to see a specific code returned.
127.0.0.1
127.0.0.2
127.0.0.3
127.0.0.4
127.0.0.5
How can this be adjusted to return the (code) output value when the script runs?
$host = '222.22.222.222';
$rbl = 'hostkarma.junkemailfilter.com';
$rev = array_reverse(explode('.', $host));
$lookup = implode('.', $rev) . '.' . $rbl;
if ($lookup != gethostbyname($lookup)) {
echo "ip: $host is listed in $rbl\n";
} else {
echo "ip: $host NOT listed in $rbl\n";
}
EDIT: Sorry guys, The function of the script above will return confirmation if the IP address is on the blacklist entered in $rlb. However, Hostkarma returns a code, one of the 127.0 codes shown above as each code indicates a different block status. I need to get the code. "echo $lookup;" just returns the reverse lookup, like this: 222.222.22.222.hostkarma.junkemailfilter.com
$lookup = implode('.', $rev) . '.' . $rbl;
$value = gethostbyname($lookup);
if ($lookup != $value){
echo "ip: $host is listed in $rbl\n";
echo "return value: $value\n";
}
else{
echo "ip: $host NOT listed in $rbl\n";
}
The 127.x.x.x code should be given to you as the value returned by gethostbyname.
Do you mean this?
echo $lookup;

Categories