penders - 7:18 pm on Jan 17, 2012 (gmt 0)
Does $_SERVER['REMOTE_ADDR'] return an IP address for you?
I have experienced the situation where REMOTE_ADDR was not set (or was empty) - I think this was down to the server configuration and the users accessing it over a closed intranet. In this case I had to fall back to HTTP_X_FORWARDED_FOR (which can contain multiple (comma separated) IP addresses if going through multiple proxies). However, if REMOTE_ADDR is set and is a valid IP address then this should take priority as I believe these other headers could be faked, if you are on the world-wide-web.
The script you have posted above looks as if you are allowing REMOTE_ADDR to be overridden if another header is set - this looks dangerous to me, particularly if you are serving content based on this IP address.