note 33914 deleted from function.require-once by sniper

From: Date: Tue, 04 Nov 2003 00:45:24 +0000
Subject: note 33914 deleted from function.require-once by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-59820@lists.php.net to get a copy of this message
Note Submitter: brock underscore weaver at msn dot com ---- 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!

« previous php.notes (#59820) next »