CGI/1.1 specification
- REMOTE_PORT is not listed in the CGI specification.
(However, Apache supplies it.)
- HTTP_USER_AGENT is only supplied when the HTTP client actually provided it.
- HTTP_REFERER is not listed in the CGI specification.
(However, Apache supplies it when the HTTP client provided it.)
Could it be that your web server doesn't provide these? If your third snippet returns it, you must have a typo somewhere.