|
Posted by Tom Rogers on 03/07/05 08:20
Hi,
Monday, March 7, 2005, 3:57:50 PM, you wrote:
LG> Hello Tom,
LG> Sunday, March 6, 2005, 10:00:17 PM, you wrote:
TR>> Ok I see where is is going wrong, try this:
LG> Oooooh, very close. Although you have it at $document_root and all
LG> that needs to be added is '/include' like below:
LG> $include = $document_root . '/include';
LG> Otherwise it's one directory too far up because you're replacing the
LG> site root 'test' (in this case) with 'include'.
LG> We wanted: 'C:/Sambar/docs/test/include'
LG> Here's from your script.
LG> Site root:
LG> **********
LG> File name: C:/Sambar/docs/test/test.php
LG> Script: /test.php
LG> Document root: C:/Sambar/docs/test
LG> Base: test
LG> Include: C:/Sambar/docs/include
LG> OS: winnt
LG> Include: C:/Sambar/docs/include;.;C:\php5\pear
LG> **********
LG> Site root/folder1
LG> **********
LG> File name: C:/Sambar/docs/test/folder1/test.php
LG> Script: /folder1/test.php
LG> Document root: C:/Sambar/docs/test
LG> Base: test
LG> Include: C:/Sambar/docs/include
LG> OS: winnt
LG> Include: C:/Sambar/docs/include;.;C:\php5\pear
LG> **********
LG> Site root/folder1/folder2
LG> **********
LG> File name: C:/Sambar/docs/test/folder1/folder2/test.php
LG> Script: /folder1/folder2/test.php
LG> Document root: C:/Sambar/docs/test
LG> Base: test
LG> Include: C:/Sambar/docs/include
LG> OS: winnt
LG> Include: C:/Sambar/docs/include;.;C:\php5\pear
LG> **********
LG> After my change:
LG> File name: C:/Sambar/docs/test/folder1/folder2/test.php
LG> Script: /folder1/folder2/test.php
LG> Document root: C:/Sambar/docs/test
LG> Base: test
LG> Include: C:/Sambar/docs/test/include
LG> OS: winnt
LG> Include: C:/Sambar/docs/test/include;.;C:\php5\pear
LG> The other two folders moving back up the tree are the same result.
LG> So the final looks like:
LG> ************************
LG> if(isset($_SERVER["SCRIPT_FILENAME"])){
LG> $file_name =
LG> str_replace('\\','/',$_SERVER['SCRIPT_FILENAME']);
LG> echo "File name: $file_name<br>";
LG> $script = str_replace('\\','/',$_SERVER['SCRIPT_NAME']);
LG> echo "Script: $script<br>";
LG> $document_root = str_replace($script,'',$file_name);
LG> echo "Document root: $document_root<br>";
LG> $base = basename($document_root);
LG> echo "Base: $base<br>";
LG> //$include = str_replace($base,'include',$document_root);
LG> $include = $document_root . '/include';
LG> echo "Include: $include<br>";
LG> $os = strtolower(PHP_OS);
LG> echo "OS: $os<br>";
LG> $lk = ':';
LG> $org_include = ini_get("include_path");
LG> if(preg_match('/^win/i',$os)) $lk = ';';
LG> ini_set("include_path",$include.$lk.$org_include);
LG> echo "Include: ".ini_get("include_path")."<br>";
LG> }
LG> ************************
LG> It's a great effort and looks bulletproof. Can you shoot any holes in
LG> mine? A full explanation at:
LG> http://www.devtek.org/tutorials/dynamic_document_root.php
LG> I only throw mine back out there because it's shorter. I think it's
LG> solid, but only because I haven't broken it yet. :-)
LG> Both our solutions suffer the same problem. They have to be on every
LG> page that requires an include, because we can't rely on a new host
LG> having our code in their auto_prepend file
LG> **********
LG> function dynRoot()
LG> {
LG> $levels = substr_count($_SERVER['PHP_SELF'],"/");
LG> for ($i=0; $i < $levels - 1; $i++)
LG> {
LG> $relativeDir .= "../";
LG> }
LG> return $relativeDir;
LG> }
LG> include(dynRoot() . 'includes/somefile.php')
LG> **********
LG> Thanks again!
LG> Cheers,
LG> Leif Gregory
LG> --
LG> TB Lists Moderator (and fellow registered end-user)
LG> PCWize Editor / ICQ 216395 / PGP Key ID 0x7CD4926F
LG> Web Site <http://www.PCWize.com>
I do this for security as I have things in include that I don't want
to be avaiable directly to the browser Also you don't need a path for include
files you can just do:
include('somefile.php');
and it will be found regardless of where the script is located.
--
regards,
Tom
Navigation:
[Reply to this message]
|