Skip to content

Commit 91f864f

Browse files
committed
Added possibility to cache IP country lookup results
1 parent 21ce86c commit 91f864f

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Installing this script to your website is very easy, if your website runs on PHP
3838

3939
Just download the helpukraine.php file, put it in the root folder of your web server. Then add the following line to the beginning (right after the first line beginning with `<?php`) of your index.php or whatever PHP file you're using to deliver your homepage:
4040

41-
`include("helpukraine.php");`
41+
`include_once("helpukraine.php");`
4242

4343
That's it. Nothing else needed. The special web page will appear in all the following situations:
4444

@@ -58,6 +58,8 @@ Thanks to the True North PHP Hackathon in November of 2014, an online tool calle
5858

5959
Note that the free API used by default to identify the client's country by IP has a limit of 45 calls per minute. If your website receives more traffict than that 45 visits per minute, then you might want to consider replacing the default API call in the `helpukraine_get_ip_country` function with another one that does not have such a low limit.
6060

61+
It's also a good idea to create a writable directory (`geoipcache` by default) to allow the script to cache each client's country to avoid overuse of the API, which could also slow down your website, especially if you're using a CMS that would include the script several times per request.
62+
6163

6264
### Feel free to make or suggest changes
6365

helpukraine.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
// For more information visit https://24usw.com/helpukraine
1717
//
1818

19+
CONST GEOIP_CACHE = "./geoipcache/"; // Set this to a writable directory path to allow caching IP country lookups
20+
1921
if (($_GET["helpukraine"] ?? 1) and (($force_help_ukraine ?? 0) or ($_GET["helpukraine"] ?? 0)
2022
or (basename($_SERVER['REQUEST_URI']) == basename(__FILE__))
2123
or in_array(helpukraine_get_ip_country(helpukraine_get_ip()), ["RU", "BY"])))
@@ -81,9 +83,18 @@ function helpukraine_get_ip()
8183

8284
function helpukraine_get_ip_country($ip)
8385
{
86+
// Try to use cache to avoid API overuse
87+
if ($country = @file_get_contents(GEOIP_CACHE . $ip)) return $country;
88+
8489
// This API is limited to 45 requests per minute
8590
$json = get_url("http://ip-api.com/json/$ip?fields=countryCode");
8691
$data = json_decode($json, true);
92+
if (isset($data["countryCode"]))
93+
{
94+
// Try to cache result to avoid API overuse
95+
if (!is_dir(GEOIP_CACHE)) @mkdir(GEOIP_CACHE, 0777, true);
96+
@file_put_contents(GEOIP_CACHE . $ip, $data["countryCode"]);
97+
}
8798
return ($data["countryCode"] ?? "error");
8899

89100
// Subscription-based alternative with free 15,000 requests per hour: https://freegeoip.app

0 commit comments

Comments
 (0)