通过Javascript获取地理位置,并通过PHP将其保存到文本文件

I want to get latitude and longitude of the guest and save them to a text file..

I used this:

<html>
<body>

<?php

 $location = print '
   <SCRIPT LANGUAGE="JavaScript">
window.onload=function(){
if(navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else
{
alert("Geolocation is not supported by this browser.");
}
}
function showPosition(pos){
document.write("Location: "+pos.coords.latitude+","+pos.coords.longitude);
</script>
'

;



$Seb = "*******************" ;
 $file = "save.txt";
 $a = fopen($file, "a");
 fwrite($a,$location."
");
    fwrite($a,$Seb."
");
 fclose($a);


?>
<form action="index.php">
<input type="submit" value="Refresh">
</form>


</body>
</html>

but the result in the text file is :

1

with out any geolocation data

Any Help please?

This is totally not how it works. As javascript is client-side, and PHP is server-side, you can't just do it the way you are trying to. You need to send that information to the server from client browser. The most common way, would be using ajax to POST javascript generatred content...

With jQuery it would be as easy as:

In html file...

<script>
window.onload=function(){
if(navigator.geolocation)
{
  navigator.geolocation.getCurrentPosition(showPosition);
}
else
{
  alert("Geolocation is not supported by this browser.");
}
}
function showPosition(pos){
   $.post('saver.php',{'lat':pos.coords.latitude,'lng':pos.coords.longitude},function(res){
      console.log(res);
   });
}
</script>

In saver.php

<?php
   print_r($_POST);
   $a = fopen("save.txt", "a");
   fwrite($a,"Location: $_POST[lat],$_POST[lng]
*******************
");
   fclose($a);
?>

PS. If you can't or don't want to use jQuery, please check this answer for information on how to use ajax in raw javascript.