I'm trying to use the geoplugin to get the users gelocation and city in PHP.
However, when i run this code I get the following error:
Notice: unserialize(): Error at offset 0 of 172 bytes in ndex.php on line 21
This is my entire code:
$user_ip = getenv('REMOTE_ADDR');
echo $user_ip;
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=".$user_ip.""));
$city = $geo["geoplugin_city"];
$region = $geo["geoplugin_regionName"];
$country = $geo["geoplugin_countryName"];
echo "City: ".$city."<br>";
echo "Region: ".$region."<br>";
echo "Country: ".$country."<br>";
and the line 21 that is shown in the error is this:
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=".$user_ip.""));
I have used this code on other server and worked just fine but on a new server I get that error which I have no idea why.
could someone please advise on this issue?
any help would be appreciated.
EDIT:
I edited my code to the following and still get the same error:
$user_ip = getenv('REMOTE_ADDR');
$geo = file_get_contents("http://www.geoplugin.net/php.gp?ip=".$user_ip."");
$string = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $geo);
$geo = unserialize($string);
$city = $geo["geoplugin_city"];
$region = $geo["geoplugin_regionName"];
$country = $geo["geoplugin_countryName"];
echo "City: ".$city."<br>";
echo "Region: ".$region."<br>";
echo "Country: ".$country."<br>";