Hello,
The script might not be at fault, but the ip itself. If the user is behind a firewall, proxy or simply has a dynamic ip (like most of my country has) the script might not know what ip it gets or even get a denial. A simple solution would be to get the ISP's ip first and backtrace towards the user's ip.
U do have a script to help with (even tho it does not show the flags), but i'd like to take a look at yours first, to see if i can fix it.
I have more than 10 years experience with php (which include Codeigniter and Laravel) and mysql and 8 years experience with javascript, jquery, ajax, json, html, css and bootstrap.
Cheers