WordPress cache prevents php script to return correct client ip – how can I bypass my request to be cached

I’ve written a wordpress plugin that uses PHP to determine the client ip at some point.

I’ve been using the following function:

Read More
function get_client_ip_server() {
     $ipaddress = '';
     if ($_SERVER['HTTP_CLIENT_IP'])
         $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
     else if($_SERVER['HTTP_X_FORWARDED_FOR'])
         $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
     else if($_SERVER['HTTP_X_FORWARDED'])
         $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
     else if($_SERVER['HTTP_FORWARDED_FOR'])
         $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
     else if($_SERVER['HTTP_FORWARDED'])
         $ipaddress = $_SERVER['HTTP_FORWARDED'];
     else if($_SERVER['REMOTE_ADDR'])
         $ipaddress = $_SERVER['REMOTE_ADDR'];
     else
         $ipaddress = 'UNKNOWN';

     return $ipaddress;
}

The script works just fine, but sometimes I’m running into issues when I have enabled WP Rocket as caching module for WordPress.

It’s a bit tough to reproduce, but at a certain point, the script begins to deliver the same ip address all the time instead of a client ip.
After resetting the wp cache, things go back to normal.

I’ve been in contact with the wp rocket support, but they couldn’t provide me a satisfying answer.

what can I do, so the result won’t be cached? Currently the function is directly called by the plugin script.

I could think about rewriting it into an api call and to call it dynamically. But are there some other methods I could try?

Related posts

Leave a Reply