What is the most reliable, general way to build a self-referencing URL? In other words, I want to generate the http://www.site.com [: port] part of the URL that the user's browser hits. I am using PHP running under Apache.
A few complications:
Relying on $ _SERVER ["HTTP_HOST"], this is dangerous because it looks like it is directly from the HTTP Host header that someone can fake.
There may or may not be virtual hosts.
There may be a port specified using the Apache Port directive, but it may not be the port specified by the user if it is located behind a load balancer or proxy.
A port cannot be part of a URL. For example, 80 and 443 are usually omitted.
PHP $ _SERVER ["HTTPS"] does not always give a reliable value, especially if you are behind a load balancer or proxy.
Apache has a UseCanonicalName directive that affects the values of the SERVER_NAME and SERVER_PORT environment variables. We can assume that this is included if it helps.
The most reliable way is to provide it yourself.
, . , -. , , -. , .., , HTTP- , , .
, .: -)
, - :
$protocol = 'http'; if ( (!empty($_SERVER['HTTPS'])) || ($_SERVER['HTTPS'] == 'off') ) { $protocol = 'https'; if ($_SERVER['SERVER_PORT'] != 443) $port = $_SERVER['SERVER_PORT']; } else if ($_SERVER['SERVER_PORT'] != 80) { $port = $_SERVER['SERVER_PORT']; } // Server name is going to be whatever the virtual host name is set to in your configuration $address = $protocol . '://' . $_SERVER['SERVER_NAME']; if (!empty($port)) $address .= ':' . $port $address .= $_SERVER['REQUEST_URI']; // Optional, if you want the query string intact if (!empty($_SERVER['QUERY_STRING'])) $address .= '?' . $_SERVER['QUERY_STRING'];
, PHP.
, - url - . $_SERVER ['HTTP_HOST'] , .
define('SITE_URL', $_SERVER['HTTP_HOST']);
:
define('SITE_URL', 'http://foo.bar.com:8080/');
$_ SERVER [ "HTTP_HOST" ], , , , .
, , , , .
, $_SERVER ['HTTP_HOST'] , DNS. , , , IP-.
http://www.php.net/manual/en/function.gethostbyname.php
Peusudo :
define('SITEHOME', in_array(gethostbyname($_SERVER['HTTP_HOST']), array(... valid IP's))) ? $_SERVER['HTTP_HOST'] : 'default_hostname';
why { , http:///host: port/, URL-} URL-
http://xxx:yy/zzz/fff/
..//whatever.jpg { http://xxx: yy/zzz/graphics/whatever.jpg
/zzz/graphics/whatever.jpg { }
Source: https://habr.com/ru/post/1702388/More articles:How to show ShowDialog () from MDIChild form correctly? - .netRequest Tracker on Rails? - ruby | fooobar.comСохранять идентификатор пользователя в Principal или Identity? ASP.Net/OpenID - openidPartial Project Issues Visual Studio Database Pro - visual-studio-dbproVB6 - How to automatically break command line compatibility (or msbuild) - compatibilityJavascript onchange event preventing onsubmit event in HTML form? - javascriptOnsubmit event, silently killed onchange - javascriptC #: How to call a method when forcing different sorting? - reflectionHow to enter DateTime value in VS QuickWatch window? - debuggingString parsing error in C "left operand must be l-value" - cAll Articles