As I explained before, you use $_SERVER
variable, you should not expect all browsers / systems to return the same thing, because some of them sometimes omit information, but what I'm saying is, use this variable only to find out which is the root directory you are currently using or will use, directly using this variable or relying only on the path it returns.
In the console / terminal command, of course it will not return the path, but if you run from a browser you can see this path, which is a path defined in the configuration file server.
In a recent linux system, you will get /var/www/meu_site
, using $_SERVER['DOCUMENT_ROOT']
.
Based on this, you can simply define all possible paths for your application, in a single configuration file, which will only be called once.
// init.php
// Ficheiro de inicialização de configurações gerais
define('DS', DIRECTORY_SEPARATOR);
define('ROOT',$_SERVER['DOCUMENT_ROOT']);
define('SITE_ROOT',ROOT.DS.'meu_site');
# 2º alternativa
# define('SITE_ROOT',DS.'var'.DS.'www'.DS.'meu_site');
define('LIB_CLASS',SITE_ROOT.DS.'classes');
define('LIB_INCLUDES',SITE_ROOT.DS.'includes');
So, just add this file to the script:
require_once '/includes/init.php';
// Noutros ficheiros que possuam a configuração de inicialização
print "<h3>Diretórios:</h3>";
print SITE_ROOT.DS.'teste.php';
//require_once SITE_ROOT.DS.'teste.php';
print "<br/>";
print LIB_CLASS.DS.'class.teste.php';
//require_once LIB_CLASS.DS.'class.teste.php';
print "<br/>";
print LIB_INCLUDES.DS.'testeValidar.php';
//require_once LIB_INCLUDES.DS.'testeValidar.php';
The paths will look like this:
/var/www/meu_site/teste.php
/var/www/meu_site/classes/class.teste.php
/var/www/meu_site/includes/testeValidar.php
For windows, you're obviously going to get different paths and
different tabs too, but that's the idea.