Does Apache pass to PHP whether the port (:80) was explicitly requested?
Asked Answered
C

2

1

It doesnt matter whether a resource was requested via mydomain.com OR mydomain.com:80(which is how some bots/spiders make requests): HTTP_HOST always shows up as 'mydomain.com' and there is no way for PHP to know whether the request included a :80 (2). This leads me to believe that Apache passes the exact same request to PHP whether the port is specified or not (an it makes sense).

The problem: I have the NewRelic PHP agent installed on this machine, and it reports to the NewRelic service, via a PHP auto_prepend_file used to name the applications separately(1), both mydomain.com and mydomain.com:80. These two are the same application showing up under two different names in NewRelic control panel, which is undesirable. Since the application name is set via PHP, how could mydomain.com:80 possibly be passed to the NewRelic API when the code setting the name can't even see whether the request explicitly defines :80? NewRelic support tells me that my code is passing the :80 to their API, and that's just not possible(2). Does Apache pass to PHP whether the port (:80) was explicitly requested or not?

1) if I don't use this file, every vhost on the server will report under the same generic 'PHP Application' name

2) SERVER_PORT will always be 80, whether explicitly invoked or not (unless of course it's an https or non standard port request, which is not what's being discussed here). In other words, I cannot use SERVER_PORT to filter requests that have the port explicitly defined because whether the request does or does not have an explicitly defined port is invisible to PHP.

Apache 2.2.15, mod_fcgid 2.3.7, PHP 5.3.3, Linux 2.6.32.60-40 x64, CentOS 6.4

Coracle answered 1/10, 2013 at 18:47 Comment(0)
C
1

Browsers AND curl will strip the :80 from the host header when the port requested is the standard (port 80). Since I had not tested using a manually issued GET, I didn't see how could $_SERVER['HTTP_HOST'] include a port number. I found that it does, WHEN the port is included in the Host header (which makes sense, since the manual specifies that $_SERVER['HTTP_HOST'] is "Contents of the Host: header from the current request, if there is one.")

Section 14.23 of the HTTP spec states that the port # should be included in a request IF its not the default port of 80. But it doesn't say whether it's OK or not to include the port number when the port used IS the default.

In my case, the requests that do include the port number, even when using the default port, came from the Baidu Spider.

If you need to see this by yourself, you need to issue a manual GET request. Use Netcat or Telnet to send these commands:

$ telnet www.host.com 80
GET /host.php HTTP/1.1
Host: www.host.com:80
Connection: close

(add two CRLFs at the end)

Coracle answered 3/10, 2013 at 21:36 Comment(0)
S
1

HTTP_HOST is literally just the hostname, e.g. www.example.com, of the site you're accessing. If you want the port number that the request came in to, then there's $_SERVER['SERVER_PORT']

Shechem answered 1/10, 2013 at 18:52 Comment(5)
Yes, but the code setting the app name doesn't use SERVER_PORT. I've rephrased the question to be more specific. Does Apache pass to PHP whether the port (:80) was explicitly requested?Coracle
yes. That's what SERVER_PORT is for - the port that the request-being-processed came in on.Shechem
the port is always 80, whether explicitly invoked or not, so that doesn't help.Coracle
that's normal, since HTTP is by default port 80.Shechem
exactly. so the value of SERVER_PORT doesn't help at all. it's always the same, whether :80 was explicitly included in the request or not. i hope that is that clear enough now. thank you for your help.Coracle
C
1

Browsers AND curl will strip the :80 from the host header when the port requested is the standard (port 80). Since I had not tested using a manually issued GET, I didn't see how could $_SERVER['HTTP_HOST'] include a port number. I found that it does, WHEN the port is included in the Host header (which makes sense, since the manual specifies that $_SERVER['HTTP_HOST'] is "Contents of the Host: header from the current request, if there is one.")

Section 14.23 of the HTTP spec states that the port # should be included in a request IF its not the default port of 80. But it doesn't say whether it's OK or not to include the port number when the port used IS the default.

In my case, the requests that do include the port number, even when using the default port, came from the Baidu Spider.

If you need to see this by yourself, you need to issue a manual GET request. Use Netcat or Telnet to send these commands:

$ telnet www.host.com 80
GET /host.php HTTP/1.1
Host: www.host.com:80
Connection: close

(add two CRLFs at the end)

Coracle answered 3/10, 2013 at 21:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.