note 33914 added to function.require-once
| From: | brock at rack1 dot php dot net | Date: | Thu, 10 Jul 2003 21:24:53 +0000 |
| Subject: | note 33914 added to function.require-once | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-51870@lists.php.net to get a copy of this message | ||
Having problems with relativity? Prefer to use root-relative over document-relative includes? This
script should help.
This function will treat all paths beginning with "/" as root-relative, and allow
"../" to be used for parent pathing.
<?
// filename: util.php
function path_calc($dest,$ssl=false){
$toUrl = ($ssl ? 'https://' : 'http://').$_SERVER['HTTP_HOST'];
if (strpos($dest,0) == "/"){
// root-relative
$toUrl .= $dest;
} else if (strpos('http',0) == 0){
// absolute
$toUrl = $dest;
} else {
// document-relative
// traverse up the directory tree as needed
$parent = dirname($dest);
while(strpos($dest,"../") == 0){
$parent = dirname($parent);
$dest = substr($dest,3);
}
$toUrl .= $dest;
}
return($toUrl);
}
function redirect($dest,$ssl=false){
header("Location: ".path_calc($dest, $ssl));
exit();
}
?>
No more messing with _SERVER variables! just do something like
<?
require_once('util.php')
if (!isset($_SESSION['authorized'])){
redirect('/login.php'); //note this will work from anywhere in your site
}
?>
Granted, the root-relativity does not work for the inclusion of the util.php file, but for all
others (including .gif, .jpeg, .css, .js, etc) you can use root-relativity. Example:</p>
<?
require_once('../../includes/util.php');
?>
<html>
<body>
<img src="<?=path_calc('/images/logo.gif')?>">
</body>
</html>
Hope that helps!
----
Manual Page -- http://www.php.net/manual/en/function.require-once.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33914
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33914&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33914&report=yes