getAllServer(); $value = isset( $server_data['REMOTE_ADDR'] ) ? $server_data['REMOTE_ADDR'] : null; } else { // Convert header name to proper format (remove HTTP_ prefix for getHeader method). $header_name = str_replace( 'HTTP_', '', $header ); $value = $request->getHeader( $header_name ); } if ( empty( $value ) ) { continue; } // Validate and return the first valid IP found. $value = trim( $value ); $validated_ip = self::validate_ip( $value ); if ( '0.0.0.0' !== $validated_ip ) { return $validated_ip; } } // Fallback to '0.0.0.0' if no valid IP found. return '0.0.0.0'; } /** * Validate and sanitize IP address. * * @param string $ip The IP address to validate. * * @return string Validated IP address or '0.0.0.0' if invalid. */ private static function validate_ip( $ip ) { if ( empty( $ip ) ) { return '0.0.0.0'; } // Validate IP address using filter_var. $validated_ip = filter_var( $ip, FILTER_VALIDATE_IP ); if ( false === $validated_ip ) { return '0.0.0.0'; } return $validated_ip; } }