I want to find current location of user in php
If you don't want to use the HTML5 Geolocoation API, you can detect a bit from IP.
Or try other other solution
The Google Geocoding API
Geolocator-PHP
Maxmind http://www.maxmind.com/app/php
ipinfodb
How to get a User's Current Location Using Google Geocode API and PHP
There is no way to get the user location using PHP since it's running on the server side. You can get the user location using javascript through the browser.
Here is an example. In this example I separated the code in two files. One for processing and storing the information using PHP (geocoordinates.php) and another one (HTML) for collecting the geocoding informantion (index.html), index.html.
You could combine both files into index.php but I'll keep them separated for simplicity.
geocoordinates.php
<?php
if(isset($_POST['lat'], $_POST['lng'])) {
$lat = $_POST['lat'];
$lng = $_POST['lng'];
$url = sprintf("https://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s", $lat, $lng);
$content = file_get_contents($url); // get json content
$metadata = json_decode($content, true); //json decoder
if(count($metadata['results']) > 0) {
// for format example look at url
// https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452
$result = $metadata['results'][0];
// save it in db for further use
echo $result['formatted_address'];
}
else {
// no results returned
}
}
?>
index.html<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Geocoding Page</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(savePosition, positionError, {timeout:10000});
} else {
//Geolocation is not supported by this browser
}
}
// handle the error here
function positionError(error) {
var errorCode = error.code;
var message = error.message;
alert(message);
}
function savePosition(position) {
$.post("geocoordinates.php", {lat: position.coords.latitude, lng: position.coords.longitude});
}
</script>
</head>
<body>
<button onclick="getLocation();">Get My Location</button>
</body>
</html>
Keep in mind that in this example the once the user clicks "Get My Location" the browser will prompt the user to allow the geolocation. You could also call the getLocation function once the page loads, but the browser will always ask for the user's permissionYou can learn more about geolocation at http://www.w3schools.com/htmL/html5_geolocation.asp
How can I get the location of a user?
I normally use ipinfodb for PHP, where you get the user information based on the IP. I'm located in Sweden, and I find it very accurate even here. Though there is always a problem when the ISPs are hiding their customers location, but it has nothing to do with the API accuracy.
php how to find the location where a user came from?
In PHP, you can use the $_SERVER['HTTP_REFERER'] to know where the user came from.
There is no mechanism to know where the user is going, unless they clicked a link on your site to leave your page. (If that is the kind of exit that you want to track, you'll need to rely on javascript and implement something like Google Analytics outbound link tracking: http://www.google.com/support/analytics/bin/answer.py?answer=55527)
Automatically locate a user's geographical location in PHP
ip-api.com provides free xml based api.
To get the ip based location using their api, you may use :
$xml=simplexml_load_file("http://ip-api.com/xml/ipaddress");
echo $xml->RegionName;
How to track users location / region in PHP
Try these:
http://ip-to-country.webhosting.info/
http://www.ip2location.com/
Both are IP address-to-country databases, which allow you to look up the country of origin of a given IP address.
However it's important to note that these databases are not 100% accurate. They're a good guide, but you will get false results for a variety of reasons.
- Many people use proxying to get around country-specific blocks and filters.
- Many IP ranges are assigned to companies with large geographic spread; you'll just get the country where they're based, not where the actual machine is (this always used to be a big problem for tracking AOL users, because they were all apparently living in Virginia)
- Control of IP ranges are sometimes transferred between countries, so you may get false results from that (especially for smaller/less well-connected countries)
Related Topics
PHP Upload File Enhance Security
What Is the Js Equivalent to the PHP Function Number_Format
Make Script Execution to Unlimited
Best Way to Get Hostname with PHP
Howto Generate JSON with Smarty
Mongodb: Benefit of Using Objectid VS a String Containing an Id
Should a Function Use: Return Null;
Can One Modify the Templates Created by Artisan Migrate Command
File_Get_Contents(): Stream Does Not Support Seeking/When Was PHP Behavior About This Changed
File_Get_Contents('Php://Input') Always Returns an Empty String
How to Increase by 1 All Keys in an Array
How to Get the Session Id in Laravel
Creating and Invoking an Anonymous Function in a Single Statement
Php, How to Get Current Date in Certain Format
PHP Date Function Seven Days Previous
Encoding Byte Data into Digits
Pdo Prepared Statements for Insert and on Duplicate Key Update with Named Placeholders