note 35873 added to function.include

From: Date: Wed, 17 Sep 2003 19:37:29 +0000
Subject: note 35873 added to function.include
Groups: php.notes 
Request: Send a blank email to php-notes+get-56653@lists.php.net to get a copy of this message
If you've worked with wierd and crazy sites like I have, you've most probably come across situations where you must use the same include in several pages. On one particular site, I faced a situation in which I had to include the same file in all my pages depending on the first directory. i.e. : http://mysite/dir1/ http://mysite/dir2/ In this case, using a root relative include will not work, so I "threw-up" this bit of code. I've fixed it up a bit, but probably there's lot's of room for improvment: <? $includeName="includeName.php"; # # Here I get the script location # $script = $_SERVER["SCRIPT_NAME"]; # # Define here the number of directories from the # root the include is included. # let's say you are here on dir4: # http://domain/dir1/dir2/dir3/dir4 # and your include is in dir2, then you # would set $numDir=2 $numDir = 0; $bars = count(explode("/",$script))-($numDir+2); $path=""; if ($bars>0){ for ($i=0;$i<$bars;$i++) $path.="../"; } include ($path.$includeName); ?> ---- Manual Page -- http://www.php.net/manual/en/function.include.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35873 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35873&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35873&report=yes

« previous php.notes (#56653) next »