{"id":814,"date":"2017-03-18T17:54:19","date_gmt":"2017-03-18T12:24:19","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=814"},"modified":"2017-03-29T14:48:48","modified_gmt":"2017-03-29T09:18:48","slug":"http_host-vs-server_name","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/http_host-vs-server_name\/","title":{"rendered":"PHP-HTTP_HOST vs. SERVER_NAME"},"content":{"rendered":"<h4 id=\"server_name\"><span style=\"color: #ff6600;\"><strong>SERVER_NAME:<\/strong><\/span><\/h4>\n<ul>\n<li>The name of the server host under which the current script is executing.<\/li>\n<li>If the script is running on a virtual host, this will be the value defined for that virtual host.<\/li>\n<li>SERVER_NAME comes from the server&#8217;s VirtualHost definition and is therefore considered more reliable.<\/li>\n<li>It can, however, also be manipulated from outside under certain conditions related to how your web server is set up.<\/li>\n<\/ul>\n<h4 id=\"http_host\"><span style=\"color: #808000;\"><strong>HTTP_HOST:<\/strong><\/span><\/h4>\n<ul>\n<li>Contents of the Host: header from the current request, if there is one.<\/li>\n<li>HTTP_HOST is the target host sent by the client. It can be manipulated freely by the user.<\/li>\n<li>It&#8217;s no problem to send a request to your site asking for a HTTP_HOST<\/li>\n<li>HTTP_HOST vs SERVER_NAME:<\/li>\n<li>Imagine your web server has a default host set up as follows:<\/li>\n<li>UseCanonicalName Off<br \/>\nServerName example.org<\/li>\n<li>The ServerName directive might seem like the only thing that affects $_SERVER[&#8216;SERVER_NAME&#8217;], but is this a safe assumption?<\/li>\n<li>To determine what affect the Host header has, if any, create an index.php in the document root of the default host with the following code<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php  <br\/>echo &quot;HTTP_HOST [{$_SERVER[&#039;HTTP_HOST&#039;]}]\\n&quot;; <br\/>echo &quot;SERVER_NAME [{$_SERVER[&#039;SERVER_NAME&#039;]}]&quot;; <br\/> ?&gt;<\/code><\/pre> <\/div>\n<p>You can test several different values for Host easily enough with telnet:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">telnet example.org 80<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>Here are a few tests and corresponding results. For each test, we show the exact request and the content of the response.<\/p>\n<p><strong>1. No Host, HTTP\/1.0<\/strong><\/p>\n<h4 id=\"request\"><span style=\"color: #808000;\"><strong>Request:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">GET \/ HTTP\/1.0<\/code><\/pre> <\/div>\n<h4 id=\"result\"><span style=\"color: #800080;\"><strong>Result:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">HTTP_HOST []<br\/>SERVER_NAME [example.org]<\/code><\/pre> <\/div>\n<p><strong>Empty Host, HTTP\/1.0<\/strong><\/p>\n<h4 id=\"request-2\"><span style=\"color: #ff6600;\"><strong>Request:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">GET \/ HTTP\/1.0<br\/>Host:<\/code><\/pre> <\/div>\n<h4 id=\"result-2\"><span style=\"color: #808000;\"><strong>Result:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">HTTP_HOST []<br\/>SERVER_NAME []<\/code><\/pre> <\/div>\n<p>With an empty Host, SERVER_NAME is empty.<\/p>\n<p><strong>Empty Host, HTTP\/1.1<\/strong><\/p>\n<h4 id=\"request-3\"><span style=\"color: #808000;\"><strong>Request:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\"> GET \/ HTTP\/1.1<br\/> Host:<\/code><\/pre> <\/div>\n<h4 id=\"result-3\"><span style=\"color: #ff6600;\"><strong>Result:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">HTTP_HOST []<br\/>SERVER_NAME []<\/code><\/pre> <\/div>\n<h4 id=\"request-4\"><span style=\"color: #808000;\"><strong>Request:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">GET \/ HTTP\/1.1<br\/>Host: &lt;script&gt;alert(&#039;XSS&#039;)&lt;\/script&gt;<\/code><\/pre> <\/div>\n<h4 id=\"result-4\"><span style=\"color: #800080;\"><strong>Result:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">HTTP_HOST [&lt;script&gt;alert(&#039;XSS&#039;)&lt;\/script&gt;]<br\/>SERVER_NAME [&lt;script&gt;alert(&#039;XSS&#039;)&lt;\/script&gt;]<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>With a non-empty Host, SERVER_NAME is the HTML-escaped host value.<\/p>\n<p>SQL Injection Host, HTTP\/1.1<\/p>\n<h4 id=\"request-5\"><span style=\"color: #808000;\"><strong>Request:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">GET \/ HTTP\/1.1<br\/>Host: chris&#039; --<\/code><\/pre> <\/div>\n<h4 id=\"result-5\"><span style=\"color: #ff6600;\"><strong>Result:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">HTTP_HOST [chris&#039; --] <br\/>SERVER_NAME [chris&#039; --]<\/code><\/pre> <\/div>\n<ul>\n<li>As you can see by the results Under certain circumstances, the Host header can affect $_SERVER[&#8216;SERVER_NAME&#8217;].<\/li>\n<li>The ServerName directive is used when the Host header is absent, and apparently $_SERVER[&#8216;SERVER_NAME&#8217;] is escaped with something like htmlentities().<\/li>\n<li>Sometimes, it&#8217;s hard to tell whether a particular element in $_SERVER can be affected by the HTTP request (ask Sean about PHP_SELF)<\/li>\n<li>so I find it easier to treat everything from $_SERVER just as if it were something like $_GET or $_POST.<\/li>\n<li>SERVER_NAME instead of HTTP_HOST<\/li>\n<li>We found a problem with our configuration (nginx and php-fpm).<\/li>\n<li>When you define domain in your plugin you are using SERVER_NAME as its value.<\/li>\n<li>SERVER_NAME with this configuration is not the same as HTTP_HOST, so the plugin is not working.<\/li>\n<li>We have changed it to get the SERVER_NAME from HTTP_HOST that it\u2019s what the user is really loading in its browser.<\/li>\n<li>With this line in wp-config.php it\u2019s working well<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$_SERVER[&#039;SERVER_NAME&#039;] = $_SERVER[&#039;HTTP_HOST&#039;];<\/code><\/pre> <\/div>\n<h4 id=\"get-host-name-or-server-name-in-php\"><span style=\"color: #800080;\"><strong>Get host name or server name in PHP:<\/strong><\/span><\/h4>\n<ul>\n<li>$_SERVER[&#8216;HTTP_HOST&#8217;] give you host infomration obtained from the HTTP request header and this is what the client actually used as \u201ctarget host\u201d of the request.<\/li>\n<li>$_SERVER[&#8216;SERVER_NAME&#8217;] normally returns the same result as $_SERVER[&#8216;HTTP_HOST&#8217;], but is defined in server config.<\/li>\n<li>However, if you server is running behind the proxy, then should use $_SERVER[&#8216;HTTP_X_FORWARDED_HOST&#8217;] and<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$_SERVER[&#039;HTTP_X_FORWARDED_SERVER&#039;]\u00a0in place of\u00a0$_SERVER[&#039;HTTP_HOST&#039;]\u00a0and\u00a0$_SERVER[&#039;SERVER_NAME&#039;]. <\/code><\/pre> <\/div>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$host_name = isset($_SERVER[&#039;HTTP_X_FORWARDED_HOST&#039;]) ?  <br\/>              $_SERVER[&#039;HTTP_X_FORWARDED_HOST&#039;] : $_SERVER(&quot;HTTP_HOST&quot;);  <br\/>$server_name = isset($_SERVER[&#039;HTTP_X_FORWARDED_SERVER&#039;]) ?   <br\/>              $_SERVER[&#039;HTTP_X_FORWARDED_SERVER&#039;] : $_SERVER(&quot;SERVER_NAME&quot;); <\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>SERVER_NAME: The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host. SERVER_NAME comes from the server&#8217;s VirtualHost definition and is therefore considered more reliable. It can, however, also be manipulated from outside under [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[1503,1505,1511,1509,1507,1512,1502,1504,1508,1506,1510],"class_list":["post-814","post","type-post","status-publish","format-standard","hentry","category-php","tag-am-i-understanding-the-man-pages-correctly","tag-how-reliable-is-http_host","tag-httpheader-net-as-http_host","tag-incorrect-string-length-of-_serverserver_name","tag-is-there-a-good-alternative-to-_serverserver_name","tag-make-apache-send-a-false-server_name-to-php","tag-php-_serverhttp_host-vs-_serverserver_name","tag-php-redirect-based-on-http_hostserver_name-within-same-domain","tag-php-_server-variables-_serverhttp_host-vs-_serverserver_name","tag-server-variable-is-not-recognizable-in-other-pages","tag-what-does-this-statement-returns-_serverserver_protocol"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/814","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=814"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/814\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}