Get Subdomain with PHP

0

How do I get the name of the subdomain with PHP? I tried the form 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 code above, is as follows:

 http://site.com.br

and would like to get:

http://subdiretorio.site.com.br
    
asked by anonymous 13.08.2018 / 16:38

4 answers

1

Use the $_SERVER['HTTP_HOST'] variable, it will return the complete host, including subdomain. Soon if you use 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 following simple code snippet:

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

If virtualhost is well configured then SERVER_NAME should work fine, but I believe that your case is another medium, such as proxy-reverse, or the hosts for your applications are resolved by the header Host: in HTTP. p>

In other words, the value of SERVER_NAME is not changed, because SERVER_ variables are usually fixed values of settings.

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

The SERVER_NAME is probably not "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 correctly, example in Apache:

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

    DocumentRoot /public
</VirtualHost>

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

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

Then the value of Host: that is in the example subdominio.dominio.com , will be populated to $_SERVER['HTTP_HOST'] , thus:

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

Note: I assume that $http you already have, to find out if it's HTTP or HTTPS, if you do not have it just do this:

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

 $caminhoAbsoluto = $http . $_SERVER['HTTP_HOST'] . "/";
    
13.08.2018 / 22:36
0

It would be something like:

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

If you just want the subdomain use the array_shift : array_shift : / p>

$var = explode('.', $_SERVER['HTTP_HOST']);
echo array_shift($var);
    
13.08.2018 / 22:35
-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>";

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

?>
    
13.08.2018 / 23:00