The output of this script is fine in browser. I tested on myself and got accurate results. What I want to know is, how do I write the response to a file? I tried writing the response and all I got was the codes written instead of the output.
<?php
$loc = '<body onload="getLocation()">
<p id="demo"></p>
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>';
//File Write
echo $loc;
$log = fopen("loc.txt", "a");
$info = "$loc
";
fwrite($log, $info);
fclose($log);
?>
Output = GPS Coordinates of the browser