url from the root of the site

Asked

Viewed 494 times

0

Good afternoon, I would like to clear up a question I have for some time in PHP.

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

Is there a better solution to this problem, such as a link raiz/lang/pt.php that worked on any page in any directory. I’ve tried $_SERVER['DOCUMENT_ROOT'] but it doesn’t seem to work right on the host location, brings 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;
}
?>` 

1 answer

1

If the problem is just finding the host name:

$_SERVER["HTTP_HOST"]

When starting a link with "/" the browser 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 sets the browser to treat differently.

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

Browser other questions tagged

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