Is there a way to get lat/long
into PHP
in a single file solution? Normally using JS
I would use this in a pure JS
sulution, but now I need the values in PHP
variables:
<script>
$(document).ready(function(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showLocation);
} else {
$('#location').html('Geolocation is not supported by this browser.');
}
});
function showLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
return latitude;
}
</script>
I need to get the lat/lon
g vars into PHP
without using an AJAX
post into a url: xxx.php type of sulution. Single file only. Any ideas on a clean way to execute this anyone?
BTW- If I write them into cookies this only works if I reload the doc. :-(
Thanks!
Is there a way to get lat/long
into PHP
in a single file solution? Normally using JS
I would use this in a pure JS
sulution, but now I need the values in PHP
variables:
<script>
$(document).ready(function(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showLocation);
} else {
$('#location').html('Geolocation is not supported by this browser.');
}
});
function showLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
return latitude;
}
</script>
I need to get the lat/lon
g vars into PHP
without using an AJAX
post into a url: xxx.php type of sulution. Single file only. Any ideas on a clean way to execute this anyone?
BTW- If I write them into cookies this only works if I reload the doc. :-(
Thanks!
Share Improve this question edited Nov 26, 2018 at 6:26 Madhur Sharma 1427 bronze badges asked Nov 26, 2018 at 4:32 user1892989user1892989 5- 2 PHP runs on the server so asking fo the location in PHP will return the servers location. Is that what you are looking for? If not then some kind of clientside code is needed. – Andreas Commented Nov 26, 2018 at 4:35
- Since the script correctly returns the latitude, longitude of the browser, I need to get them to php vars (possibly) without using AJAX. If possible. – user1892989 Commented Nov 26, 2018 at 5:19
- The page needs to load for the JS to run. The PHP isn't not present once the page has loaded. You could send an ajax request to the PHP with the address which wouldn't require a reload. Why can't ajax be used, JS is already required for this solution. – user3783243 Commented Nov 26, 2018 at 5:21
- The AJAX examples that I have seen only pass the data into forms. Is there a way to pass the data into a PHP var using a single page solution, or am I heading down there wrong path? – user1892989 Commented Nov 26, 2018 at 5:29
- You can assign PHP var to js variable but you can't assign js va to PHP variable. IMO put your js variable to hidden field of the form and submit that form using PHP. You'll get it in PHP. – Abhishek Commented Nov 26, 2018 at 5:37
2 Answers
Reset to default 1You can use geoplugin api Link
<?php
echo var_export(unserialize(file_get_contents('http://www.geoplugin/php.gp?
ip='.$_SERVER['REMOTE_ADDR'])));
?>
I did this In the JS (g1.php):
function showPosition(position) { var latlon = position.coords.latitude + "," + position.coords.longitude; var prog = "g2.php?latlon=" + latlon; location.href = prog;}
and then did this in g2.php
$field = explode(',', $_GET['latlon']);
I don't know if it is good programming but it works