0

For the router, I wanted to get the host name from the URI upon a server request. I know that I need to read it from the $_SERVER variable. But it seems that in the $_SERVER array there are multiple entries (at least two) for the host name.

Could you please tell me which value should I choose to read - the most reliable one?

For example, when I have an URI like this:

http://local.mvc/mycontroller/myaction

the $_SERVER array will have:

[HTTP_HOST] => local.mvc
[SERVER_NAME] => local.mvc

I need to obtain the value local.mvc.

Thank you for your time.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • If you choose to downvote my question, could you please motivate the choice, so that I can reedit my question and make it better? In this way all of us will benefit from a good question, good answers and a better website. Thank you. –  Nov 02 '17 at 07:00
  • You can use any of the above array key to get host name. Both are reliable. – Pratik Gadoya Nov 02 '17 at 07:12
  • Possible duplicate of [Best way to get hostname with php](https://stackoverflow.com/questions/1408996/best-way-to-get-hostname-with-php) – AZinkey Nov 02 '17 at 07:13
  • Thank you, @PratikGadoya. In the docs I saw that there are "structural" differences between the two values and that SERVER_NAME, in a not properly configured virtual host, can be spoofed. –  Nov 02 '17 at 07:20
  • Hello @AZinkey and thank you for the comment. I would like to ask you to remove the "duplicate" status of my question, because in my question I'm referring myself to the case of a website host name and all answers in your proposed link are referring to a function to read the machine name and/or to a server variable which is not anymore actual. I reedited my question to better reflect what I want to achieve. Thanks. –  Nov 02 '17 at 07:38

3 Answers3

0

SERVER_NAME is the name assigned to the given server in its configuration (be it i.e. apache.conf file and its ServerName directive or similar for other software), while HTTP_HOST value is obtained from the headers of HTTP request that comes from client (web browser usually). These two might differ if your server servers multiple domains (like shared server / virtual server hosting). Depending on the use case you may want to use either of these, however HTTP_HOST seems like better choice as it is always tells what user wanted to reach.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • Thank you for your time. I'm not sure what you exactly mean. Could you please extend a bit, eventually with a simple example? I would be very grateful. I have my perspective on what you say, but I may be wrong. Thank you. –  Nov 02 '17 at 07:51
  • Not sure what is unclear in my answer, but I edited it slightly. Hope this helps. – Marcin Orlowski Nov 02 '17 at 14:36
  • 1
    Hi again and sorry for the delay... The multiple virtual hosts situation was unclear for me. I couldn't "imagine" it myself. But now I do. Thank you for reediting your answer! –  Nov 02 '17 at 17:41
0

the $_SERVER variable has 'REQUEST_URI'.

just var_dump the server variable, and is should be there (or look at the documentation)

SmartCoder
  • 413
  • 2
  • 13
0

If you're using PHP5 or PHP7, try php_uname

To get the host name:

php_uname("n");
  • Hi. Thank you for your answer. I tried it and is not what I want. The `php_uname('n')` function returns the host name of the machine, not of the request URI as sent from a browser. And `php_uname()` in general _returns information about the operating system PHP is running on_. –  Nov 02 '17 at 16:54