url from the root of the site

0

Good afternoon, I would like to clarify a doubt that I have for some time in PHP.

For example, in the case below, I have the site translation system, but it only works for the directory it is in, ie it only works if it is included in pages in a directory, if it is in a subdirectory, it would to make a new translation system file with the links starting in ../ .

There is a better solution to this problem, like a link raiz/lang/pt.php that works on any page in any directory. I've tried $_SERVER['DOCUMENT_ROOT'] but it does not seem to work right on the local host, bring me the location fo file (c: / xaamp / htdocs /).

<?php
//Detecao se não existir cookie
if(!isset($_COOKIE["lang"])){
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){

    case "pt":
        //echo "PAGE PT";
        $expire=time()+60*60*24*30;
        setcookie("lang", "pt", $expire);
        $_COOKIE["lang"] = "pt";
        break;

    case "en":
        //echo "PAGE EN";
        $expire=time()+60*60*24*30;
        setcookie("lang", "en", $expire);
        $_COOKIE["lang"] = "en";
        break;

    default:
        //echo "PAGE EN - Setting Default";
        $expire=time()+60*60*24*30;
        setcookie("lang", "en", $expire);
        $_COOKIE["lang"] = "en";
        break;
}
}
//Alteração linguagem por link

if(isset($_GET["lang"])){
    $lang = $_GET["lang"];
    switch ($lang){

        case "pt":
             //echo "PAGE PT";
            $expire=time()+60*60*24*30;
            setcookie("lang", "pt", $expire);
            $_COOKIE["lang"] = "pt";
             break;

        case "en":
            //echo "PAGE EN";
            $expire=time()+60*60*24*30;
            setcookie("lang", "en", $expire);
            $_COOKIE["lang"] = "en";
             break;

        default:
            //echo "PAGE EN - Setting Default";
            $expire=time()+60*60*24*30;
            setcookie("lang", "en", $expire);
            $_COOKIE["lang"] = "en";
            break;
}
}


// No caso de existir cookie

$lang = $_COOKIE["lang"];

switch ($lang){

    case "pt":
        //echo "PAGE PT";
        //include
        require_once("lang/pt.php");
        break;

    case "en":
        //echo "PAGE EN";
        //include
        require_once("lang/en.php");
        break;

    default:
        //echo "PAGE EN - Setting Default";
        //include
        require_once("lang/en.php");
        break;
}
?>' 
    
asked by anonymous 02.08.2015 / 17:01

1 answer

1

If the problem is just find the host name:

$_SERVER["HTTP_HOST"]

When you start a link with "/" the browser, it automatically uses the current host. Example: Being on the page http://www.exemplo.com/pagina1.php , the link:

<a href="/dir/minhapagina.php">link</a>

will lead to the page http://www.exemplo.com/dir/minhapagina.php , unless the page itself configures the browser to handle differently.

Other variables with useful information can be found with the phpinfo(); command on a php page.

    
02.08.2015 / 18:40