Find the user city

I want to create a web application that give user recommendations regards to his city.

For example, if the visitor is in London, he will be recognized and the website will give him recommendations in London.

Is there any API or something accuarate and useful for this usage?

What if he is in New York and is planning on visiting London?
How is the recognise going to work automatically?

I would suggest the user needs to input the location.

Geolocation using JavaScript or a service such as Maxmind that provides location API that can be used server-side. Those are really the only two, semi-reliable options short of building your own system and that is a box left closed. Either a API service layer or JavaScript Geolocation.

They offer a free database of IP addresses that you can cross reference.

Here is a simple class I use to get the county code and save it to a cookie, there is a more advanced database that can do cities as well but I’m cheap and didn’t need that functionality:

You will need to replace Server() with your own to fetch the client IP.

$ipl = new IPLocation()
$country = $ipl->clientcountry    

class IPLocation {

    /** @var null database connection */
    public $db = null;
    public $clientip = null;
    public $clientcountry = null;
    public $clientnumber = null;

    /** On create, open a database connection, get submitted values and fetch password from database */
    public function __construct(PDO $db) {
  
        $server = new Server();
        $this->clientip = $server->getClientIp();
        $this->clientnumber = $this->IPAddresstoNumber($this->clientip);
        $SiteCookie = new Cookie();
        $cookie = $SiteCookie->get('ipc');

        if (!$cookie) {

            $ClientCountry = $this->getCountry();

            foreach ($ClientCountry as $ClientResult) {
                $Cookieval = $ClientResult->country_code;
            }

            //Default Back to GB if nothing found
            if ($Cookieval == '-') {
                $Cookieval = 'GB';
                throw new Exception('Failed to Find IP Number for:' . $this->clientip . ' using GB as default');
            }

            $this->clientcountry = $Cookieval;
            $SiteCookie->set('ipc', $Cookieval);
            return $Cookieval;
        } else {
            $this->clientcountry = $cookie;
        }
    }

    public function getNumber() {
        return $this->clientnumber;
    }

    public function getCountry($IPAddress = null) {

        if ($IPAddress == null) {
            $ipnumber = (int) $this->clientnumber;
        } else {
            $ipnumber = $this->IPAddresstoNumber($IPAddress);
        }

        try {
            $sql = "SELECT country_code FROM tbl_iplocation WHERE :clientnumber BETWEEN ip_from and ip_to";
            $query = $this->db->prepare($sql);
            $query->execute(array(':clientnumber' => $ipnumber));
            return $query->fetchAll();
        } catch (Exception $e) {
            //echo 'Caught exception: ', $e->getMessage(), "\n";
            return;
        }
    }

    private function IPAddresstoNumber($IPaddr) {
        if ($IPaddr == "") {
            return 0;
        } else {
            $ips = explode(".", (string) $IPaddr);
            return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
        }
    }

}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.