My creak changes its job depending on which site it is located on. If it runs on mydomain.com, then I want $q
in my 'mydomain.com' variable. And if localhost, then, accordingly, 'localhost'. Thank!
- 2see SERVER_NAME - Sergiks
- oneThank you - jackair
1 answer
You can find out the current host name from the $_SERVER['SERVER_NAME']
environment variable.
$q = $_SERVER['SERVER_NAME']; switch($q) { case 'mydomain.com' : // Продакшен среда break; default : // Локальная разработка }
You can set your own environment variable if the standard SERVER_NAME
environment variable is not suitable for some reason. For example, your engine is used on several hosts and you cannot place the host name directly in the code. In this case, you can define environment variables with neutral values that do not depend on the domain name (dev, prod, staging, test, etc.). To do this, you should set the following at the virtual host or location-section level responsible for processing php files directive setting the environment variable
nginx
location ~ \.php$ { ... fastcgi_param APP_HOST prod; ... }
apache
<VirtualHost hostname:80> ... SetEnv APP_HOST prod ... </VirtualHost>
As a result, the environment variable APP_HOST
will be available in the PHP script, which can be accessed through $_SERVER['APP_HOST']
(the value is 'prod').