Note that there are some explanatory texts on larger screens.

plurals
  1. POIncorrect port number returned by $_SERVER['server_port']
    primarykey
    data
    text
    <p><strong>Scenario:</strong> I'm working on a Apache web server with some PHP. I point my browser at <strong><a href="https://my.example.com/test.php" rel="nofollow noreferrer">https://my.example.com/test.php</a></strong> that has the following lines of code in it:</p> <pre><code>&lt;pre&gt; &lt;?php print_r($_SERVER); ?&gt; &lt;/pre&gt; </code></pre> <p>The value printed out for SERVER_PORT is <strong>80</strong>, not <strong>443</strong>. But if I go to <strong><a href="https://my.example.com:80/test.php" rel="nofollow noreferrer">https://my.example.com:80/test.php</a></strong> the web server (Apache) barfs (An error occurred during a connection to my.example.com:80. SSL received a record that exceeded the maximum permissible length. Error code: ssl_error_rx_record_too_long). If I go to <strong><a href="https://my.example.com:443/test.php" rel="nofollow noreferrer">https://my.example.com:443/test.php</a></strong> then the URL redirects to <strong><a href="https://my.example.com/test.php" rel="nofollow noreferrer">https://my.example.com/test.php</a></strong> with no errors or problems except that my PHP prints out that the server port is 80 instead of 443.</p> <p>Here's the relevant section from the <strong>conf.d/ssl.conf</strong> file (I removed what I believe are extraneous directives out and replaced the actual IP address with the word IP_ADDRESS):</p> <pre><code>Listen IP_ADDRESS:443 &lt;VirtualHost *:443&gt; ServerName my.example.com ServerAlias my DocumentRoot "/path/to/document_root/htdocs" Options +Indexes &lt;/VirtualHost&gt; </code></pre> <p>Here is the full print out of $_SERVER variable (with my server details redacted/changed to anonymous examples):</p> <pre><code>Array ( [HTTP_HOST] =&gt; my.example.com [HTTP_USER_AGENT] =&gt; Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.11) Gecko/20101012 Firefox/3.6.11 [HTTP_ACCEPT] =&gt; text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 [HTTP_ACCEPT_LANGUAGE] =&gt; en-us,en;q=0.5 [HTTP_ACCEPT_ENCODING] =&gt; gzip,deflate [HTTP_ACCEPT_CHARSET] =&gt; ISO-8859-1,utf-8;q=0.7,*;q=0.7 [HTTP_KEEP_ALIVE] =&gt; 115 [HTTP_CONNECTION] =&gt; keep-alive [HTTP_COOKIE] =&gt; PHPSESSID=randomstring_yes_I'm_that_paranoid [PATH] =&gt; /sbin:/usr/sbin:/bin:/usr/bin [SERVER_SIGNATURE] =&gt; Apache/2.2.3 (Red Hat) Server at my.example.com Port 80 [SERVER_SOFTWARE] =&gt; Apache/2.2.3 (Red Hat) [SERVER_NAME] =&gt; my.example.com [SERVER_ADDR] =&gt; IP_ADDRESS_1 [SERVER_PORT] =&gt; 80 [REMOTE_ADDR] =&gt; IP_ADDRESS_2 [DOCUMENT_ROOT] =&gt; /path/to/document_root/htdocs [SERVER_ADMIN] =&gt; admin@example.com [SCRIPT_FILENAME] =&gt; /path/to/document_root/htdocs/test.php [REMOTE_PORT] =&gt; 49178 [GATEWAY_INTERFACE] =&gt; CGI/1.1 [SERVER_PROTOCOL] =&gt; HTTP/1.1 [REQUEST_METHOD] =&gt; GET [QUERY_STRING] =&gt; [REQUEST_URI] =&gt; /test.php [SCRIPT_NAME] =&gt; /test.php [PHP_SELF] =&gt; /test.php [REQUEST_TIME] =&gt; 1292273758 ) </code></pre> <p>As you can see, the SERVER_PORT is 80 and $_SERVER['HTTPS'] isn't set. According to the PHP docs, I thought it was supposed to be set to a non-empty value if the PHP script is accessed via HTTPS (which is what I'm doing).</p> <p>Any idea what's going on? I'm just the web developer - I don't manage this server but my server admin tells me everything is working but I would like to know why $_SERVER['SERVER_PORT'] is returning 80 instead of 443 when viewing an HTTPS URL.</p> <p><strong>EDIT:</strong> I've edited the above example to illustrate my results in printing out the entire $_SERVER variable.</p> <p><strong>EDIT 2:</strong> Trying <a href="https://my.example.com:443/test.php" rel="nofollow noreferrer">https://my.example.com:443/test.php</a> as suggested in the comments below does the same thing - SERVER_PORT is 80 and HTTPS isn't set (specifically trying this URL redirects to <a href="https://my.example.com/test.php" rel="nofollow noreferrer">https://my.example.com/test.php</a>).</p> <p><strong>EDIT 3:</strong> OK, I've posted what I believe is the answer to this below (TL;DR: moving the SSL directives inside the VirtualHost directive and changing that directive to reference my site using it's IP address rather than a wildcard appears to have resolved the problem).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload