Par exemple, si vous naviguez sur un http://example.com/folder1/folder2/yourfile.php?var=blabla
(Note: si yourfile.php comprend otherfile.php puis les variables sera différent)
__FILE__ (on Hosting) === /home/xfiddlec/public_html/folder1/folder2/yourfile.php
__FILE__ (on Localhost) === C:\wamp\www\folder1\folder2\yourfile.php
$_SERVER['HTTP_HOST'] === example.com (or without WWW)
$_SERVER["PHP_SELF"] === /folder1/folder2/yourfile.php
$_SERVER["REQUEST_URI"] === /folder1/folder2/yourfile.php?var=blabla
$_SERVER["DOCUMENT_ROOT"] === /home/xfiddlec/public_html
//basename returns the last filename
basename('/folder1/folder2/yourfile.php') {i.e. basename(__FILE__) ==== yourfile.php
//dirname returns the part upper part, without filename [same as getcwd()]
dirname('/folder1/folder2/yourfile.php') {i.e. dirname(__FILE__) ==== /folder1/folder2
Plusieurs Exemples:
1) Echo URL (c'est à dire http://example.com/folder1/file.php?var=blabla)
<?php echo 'http://'. $_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];?>
2)Pour exiger (ou inclure) un fichier souhaité à partir de l'exécution du script (par exemple, le script courant example.com/folder1/folder2/folder3/phpfile.php est en cours d'exécution, et que vous souhaitez require
d'un fichier example.com/otherDirecory/somefile.php, puis utilisez:
<?php
$target_file_path_from_root = 'otherDirecory/somefile.php';
// First step: Current filepaths
$current_file = str_replace('\\','/',__FILE__);
// Second step: if the path contains the root path, lets remove it
if (stristr($current_file, 'public_html'))
{ $current_file=preg_replace('/(.*)public_html\//i','',$current_file); }
// Third step: get the desired number of upper dots
$target_file_path_FINAL = $target_file_path_from_root;
//count the deepness of directories of current filepath
for ($i=0; $i<substr_count($current_file, '/'); $i++ )
{ $target_file_path_FINAL = '../'. $target_file_path_FINAL; }
// now require $target_file_path_FINAL >>> ../../../otherDirecory/somefile.php
?>
pour wordpress, il existe déjà pré-définis des fonctions pour obtenir des plugins ou des thèmes de l'url.