Jump to content

_SERVER variables - choosing most appropriate


fedoracore
 Share

Recommended Posts

AT ISSUE HERE:inside ./MyWorkshop/Template.php i wish to link to ./MyWorkshop/css/style.cssin a manner which will be universal, continuing to link to ./MyWorkshop/css/style.css -- even inside of mkdir ./chapter06 ... (a new directory) such as:./MyWorkshop/chapter06/MySQL_examples/File-From-Template.php./MyWorkshop/chapter06/javascript/File2-From-Template.php and so on.i've got a script i use to figure file-names, but i can't think how to "warp it" into giving me what i want in this situation-- yet, the info seems to be there:

<?php$process = $_SERVER['PHP_SELF'];$patharray = pathinfo($process);$thispage = rtrim($patharray['basename'], $patharray['extension']);$thispage_rtrim = rtrim($thispage, '.');if(!defined('OFFSETPATH')) {$offsetpath = '../';} else {$offsetpath = OFFSETPATH;// edited 22:02 april11 2007}$StyleSheetFile = $offsetpath.'css/stylesheet.css';?>

but -- i don't want to have to fool around w/ that OFFSETPATH stuff. this is fine for a pre-established directory structure where i can define('offsetpath') where needed, but here i'd like to try something new. ideas? thanks.

Link to comment
Share on other sites

First, just to point out, you have the constant name in quotes. That means that it will use the string "OFFSETPATH", not the value of the constant.For most of my applications, I have a global variable that holds the base install location of the application, and I use that to link to any other files. I find that is the easiest for portability. If you don't want to do something like that, then you can put a uniquely named file in the base folder, like base.dir or something, and just recurse backwards until you find it, then work your way back up to whatever file you need.

Link to comment
Share on other sites

just recurse backwards until you find it...
right! that's what i'm talking about (i think)... except it's the recursion part that i don't know how to get. how would i "point" to that base dir file, and then "count" those directory offsets, determining how many of the "../" i need to add?there's a cool example at php.net (which ive mod'd just a bit cause it didn't work, not unlike my typo above ;-)
<?php// here $path becomes the REQUEST_URI variable// **(see "quote" below for cite) // ** note this is MY mod from the example available at ./reserved.variables.php (the Manual)// more info at http://www.php.net/manual/en/reserved.variables.phpfunction path_to_root($path) {   $pathinfo = pathinfo($path);     $deep = substr_count($pathinfo['dirname'], "/");     $path_to_root = "./";  	 for($i = 1; $i <= $deep; $i++) {  	   $path_to_root .= "../";	  	 }     return $path_to_root;}$mysterypath = path_to_root($_SERVER["REQUEST_URI"]);// NOTE my mod to the func provided at the Manualecho $mysterypath;?>

..so this gives me some very useful info-- however, something in my brain can't put this to work for my problem above. it seems to be what i need-- but i'm like-- writer's block or something! you can see i've got the info... :)

** ...symons at home dot com24-Aug-2001 06:54
EDIT:okay, for example: using the above path_to_root() func, i get this output:./../../../../../my file is in:_http://localhost/~myuser/workshop/wsbook/ch04_mynotes/generic-testing-php/offset-testing.php_so, indeed-- the "six offsets" is correct-- but there's no way to use that generic info to help me if i want to keep a file in ./wsbook/ a file which i will "always reference" for the proper offset info? i dunno-- i'm probably taking it way too far. what i'd like is to have a "TEMPLATE" that i could use throughout this "study" i'm doing-- and each time i use the template, i'd like for it to have CSS styling. so this is why i'm trying to do it -- but also for the fact that sometime in the future, this knowledge could come in handy. (i.e. surely WordPress must use some sort of "constant" like this, no? ... maybe not, as i suppose the dir structure doesn't change...) sorry about all this text. it's been bugging me for a while though-- like this is the one thing about path info that i can't seem to "get".EDIT 2:i think the bottom line is it can't be done. if it could be done, then no one would have any problems w/ breadcrumbs and all that for navigation. the bottom line is there must remain some amount of developer "intervention" to modify each file to accomodate for its directory "depth". maybe it can be done-- but probably only through a user-defined function. i dunno...i am curious about the "base.dir" thing, J.S.G. if you care to share what you mean about that.
Link to comment
Share on other sites

check it out-- for now, i've decided that the following is probably just as good as anything else for this project (though i'm still displeased about being stumped on the path stuff)

<?phpif(file_exists("offset.php")){include 'offset.php';} else {define('OFFSET','../');}if(file_exists("db.inc")){include 'db.inc';} else {$dbincNeeded = "the db.inc file isn't here!";}if(file_exists("error.inc")){include 'error.inc';} else {$errincNeeded = "the error.inc file isn't here!";}$cssAdjusted = XRO.$StyleSheetPath;?><!-- SNIP --><body><?phpif(isset($dbincNeeded)){	echo "<h1>".$dbincNeeded."</h1>";}if(isset($errincNeeded)){	echo "<h1>".$errincNeeded."</h1>";}?>

at least i'll know what's wrong w/ stuff that way-- as i'm terribly forgetful about stuff (ha! i could totally have gotten through about 5 chapters just now, huh?)8-)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...