PHP: Get Visitors IP and Location and Save to TXT File

How to get visitors IP address and Location and save them to Text file in php

This simple php code can be used to save visitors data such as location, IP address etc and log them to a text file.

$ip = $_SERVER['REMOTE_ADDR']; //get supposed IP
$query = @unserialize(file_get_contents(''.$ip));
$handle = fopen("data.txt", "a"); //open log file
fwrite($handle, "IP: $ip \r\n");
if($query && $query['status'] == 'success') {
$hh = $query['country'].', '.$query['city'].'!';
fwrite($handle, "Location: $hh \r\n \r\n");

} else {
  fwrite($handle, "Location: Unavailable \r\n \r\n");