Catch the subdomain with PHP

Asked

Viewed 730 times

0

How do I get the subdomain name with PHP? I tried it the way below, but it returns me the main domain:

$caminhoAbsoluto = $http . $_SERVER['SERVER_NAME'] . "/";

For example, I have the following subdomain: http://subdiretorio.site.com.br, with the above code, is bringing in as follows:

 http://site.com.br

and would like to take:

http://subdiretorio.site.com.br
  • I believe the variable $_SERVER['HTTP_HOST'] return it complete, no?

4 answers

1

If virtualhost is well configured then SERVER_NAME should work well, but I believe your case is another way, such as proxy reverse or else the hosts for your applications are solved by header Host: at HTTP.

That is the value of SERVER_NAME is not changed as the variables SERVER_ are usually fixed values of configurations.

So to solve this you will have to use the HTTP_HOST, because the variables with prefix HTTP_ are based on headers.

The SERVER_NAME probably not this "configured" for each specific subdomain, ie the process is dynamic and not "fixed" within Virtualhost, for example if each subdomain was defined with ServerName and ServerAlias Virtualhost would work properly, example in Apache:

<VirtualHost *:80>
    ServerName dominio.com
    ServerAlias foo.dominio.com
    ServerAlias bar.dominio.com
    ServerAlias baz.dominio.com

    DocumentRoot /public
</VirtualHost>

But probably your settings are "dynamic", i.e., your subdomains are solved by the header itself, so in this case the variable SERVER_NAME is only populated with the main host alias, since the other shall not exist in the configurations, then the way is to use $_SERVER['HTTP_HOST'] that will take the value of Host:, as in the example when requesting the page in your browser this will occur:

GET /foo/bar/baz HTTP/1.1
Host: subdominio.dominio.com
Connection: keep-alive

Then the value of Host: which is in the example subdominio.dominio.com, will be populated for the $_SERVER['HTTP_HOST'], being like this:

$caminhoAbsoluto = $http . $_SERVER['HTTP_HOST'] . "/";

Note: I suppose the $http you already have, to know if it is HTTP or HTTPS, if you have not yet do this only:

 $http = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';

 $caminhoAbsoluto = $http . $_SERVER['HTTP_HOST'] . "/";

1

Use the variable $_SERVER['HTTP_HOST'], it will return the full host, including subdomain. Soon using the following command, you will get the result you expected:

echo 'http://' . $_SERVER['HTTP_HOST']; // Será impresso: http://subdiretorio.site.com.br

And to extract only the subdomain from the string, use the simple code snippet below:

echo explode('.', $_SERVER['HTTP_HOST'])[0]; // Será impresso: subdiretorio

0

It would be something like:

echo $caminhoAbsoluto = $_SERVER['HTTP_HOST'] . "/";

If you just want the domain use the array_shift:

$var = explode('.', $_SERVER['HTTP_HOST']);
echo array_shift($var);
  • Dear 13dev, this use of explode may even work in php5.3, 5.4 and cia, but in php7 it will surely fail because array_shift expects a reference, causing something like PHP Notice: Only variables should be passed by reference, of course in general PHP ignores and is only a Notice, but still the goal is to use reference and not values in functions that wait references: http://php.net/manual/en/language.references.php

  • I hadn’t even noticed, but I’ve already edited the reply, thanks for the warning!

-1

Try these functions to see if it solves your problem:

<?php

// COMO COMPLEMENTO PARA PESQUISA DE PROTOCOLO HTTP OU HTTPS
// Apenas para fins de conhecimento
if( !function_exists('server_protocol') ){
    function server_protocol() {
        $protocol = $_SERVER['SERVER_PROTOCOL'];
        if ( ! in_array( $protocol, array( 'HTTP/1.1', 'HTTP/2', 'HTTP/2.0' ) ) ) {
            $protocol = 'HTTP/1.0';
        }
        return $protocol;
    }
}

echo server_protocol() . "<br>";

#################################################################

// ENDERECO COMPLETO DO SITE COM PROTOCOLO, ENDERECO e /
if( !function_exists('urlbase') ) {
    function urlbase() {
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $query = $_SERVER['PHP_SELF'];
        $path = pathinfo( $query );
        $url = $protocol . $_SERVER['HTTP_HOST'] . "/";
        return $url;
    }
}

echo urlbase() . "<br>";

#################################################################

?>
  • Serves for any directory, being also a subdirectory, will return the full URL.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.