php - Self-referential URLs -


What is the most reliable, common way of creating self-referenced URLs? In other words, I want to generate a portion of the url which is killing a user's browser. I am using PHP running under Apache.

Some complications:

  • Depending on $ _SERVER ["HTTP_HOST"] is dangerous because it appears directly from the HTTP host header, who can create is.

  • There may be virtual hosts or maybe there.

  • There is a port which can be specified using Apache's port command, but it can not be the port that the specified user, if it is load-balancer or Proxy is behind

  • Actually be a part of the URL. For example, 80 and 443 are usually omitted.

  • $ _SERVER ["HTTPS"] of PHP does not always give a trusted value, especially if you are behind the load - balancer or proxy.

  • Use Apache is a technical name specification, which affects the values ​​of SERVER_NAME and SERVER_PORT environment variables. We can assume that this is going on, if it helps.

The most reliable method is to provide it yourself.

The site should be coded to keep the hostname neutral, but to find out about the special configuration file, this file does not put source code for the codebase because it belongs to the Web server's configuration. The file is used to set things like host names and other webserver-specific parameters, you can change the load balancers, change ports, because you are saying that if an HTTP request hits that code , Then assume that you will accept it.

This trick also helps in development, coincidentally. : -)


Comments