I do not have a function to offer (maybe you should code it yourself!) but some hints:
Check the manual for the environment variables.
e.g: $REMOTE_ADDR is the clients IP
Now that you have the IP take a look into the manual and see the 'network functions'
Now you should be able to code it your own.
😉
Regards
FAB