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;
}
?>'