From 7e3a1dc50edafd90c9005813f6d268a03b6d817e Mon Sep 17 00:00:00 2001 From: adolfintel Date: Fri, 15 Mar 2019 08:42:37 +0100 Subject: [PATCH] Improved performance of getIP.php --- .gitignore | 1 + doc.md | 2 ++ getIP.php | 22 ++++++++++++++++++---- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 13d4e88..aaa13dd 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ telemetry/idObfuscation_salt.php +getIP_serverLocation.php diff --git a/doc.md b/doc.md index 2c55bf2..cc6a810 100644 --- a/doc.md +++ b/doc.md @@ -41,6 +41,8 @@ Later we'll see how to use the test without PHP, and how to configure the teleme __Important:__ keep all the files together; all paths are relative to the js file +__Important:__ Make sure PHP is allowed to write to the directory where you're installing the speedtest because getIP.php needs to create a cache file to improve performance. + ## Basic usage You can start using this speedtest on your site without any special knowledge. Start by copying one of the included examples. Here's a description for each of them: diff --git a/getIP.php b/getIP.php index 9fb8c4c..ff63a71 100644 --- a/getIP.php +++ b/getIP.php @@ -89,10 +89,24 @@ if (isset($_GET["isp"])) { } if (isset($_GET["distance"])) { if ($clientLoc) { - $json = file_get_contents("https://ipinfo.io/json"); - $details = json_decode($json, true); - if (array_key_exists("loc", $details)){ - $serverLoc = $details["loc"]; + $locFile="getIP_serverLocation.php"; + $serverLoc=null; + if(file_exists($locFile)){ + require $locFile; + }else{ + $json = file_get_contents("https://ipinfo.io/json"); + $details = json_decode($json, true); + if (array_key_exists("loc", $details)){ + $serverLoc = $details["loc"]; + } + if($serverLoc){ + $lf=fopen($locFile,"w"); + fwrite($lf,chr(60)."?php\n"); + fwrite($lf,'$serverLoc="'.addslashes($serverLoc).'";'); + fwrite($lf,"\n"); + fwrite($lf,"?".chr(62)); + fclose($lf); + } } if ($serverLoc) { try {