A well-behaved load balancer should place one or more IP addresses in the X_FORWARDED_FOR header. Your code is checking HTTP_FORWARDED_FOR and two other variables that aren't quite right.
Not all load-balancers are well behaved, and some can be configured to include that header if it is not currently in there.
If there are multiple forwarding points, there will be multiple IP addresses in the header. In that case, the first one listed is the client IP
X-Forwarded-For: client, proxy1, proxy2