Ask a Question related to PHP Notes, Design and Development.
-
brock@rack1.php.net #1
note 33914 added to function.require-once
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 -- [url]http://www.php.net/manual/en/function.require-once.php[/url]
Edit Note -- [url]http://master.php.net/manage/user-notes.php?action=edit+33914[/url]
Delete Note -- [url]http://master.php.net/manage/user-notes.php?action=delete+33914&report=yes[/url]
Reject Note -- [url]http://master.php.net/manage/user-notes.php?action=reject+33914&report=yes[/url]
brock@rack1.php.net Guest
-
note 33847 added to function.require-once
In addition to priebe's note about the "cannot redeclare" error: one will also get this error if a class is defined both in a file and in a second... -
note 33827 added to function.function-exists
This can be used to conditionally define a user function. In this sense, it can act as a sort of inline include_once(). For example, suppose you... -
note 33727 added to function.is-int
To Logan: There's also a simple non-regexp way to convert a (form) value into an integer if it consists of numbers only - although with a trap... -
note 33714 added to function.register-tick-function
please can anyone help me to discover if this function can be used to make a chat ---- Manual Page --... -
note 33575 added to function.register-shutdown-function
If your script exceeds the maximum execution time, and terminates thusly: Fatal error: Maximum execution time of 20 seconds exceeded in - on line...



Reply With Quote

