sysadmin42
Technical User
- May 6, 2005
- 138
I have a simple redirect script that helps me save some typing and also gives some nice added functionality for popping around various areas of my site:
The thing is, sometimes this will be called before page headers are sent and sometimes after. If I use the header("location: $url") after headers then I get the error- headers have already been sent.
I want this script to be generic. In the past, I've simply added another variable in the function, but I want to do away with this variable and auto-detect:
Do you know of a way to automatically detect whether headers have been sent or not?
"Computer illiterate is a dirty word."
Code:
function redirect($url,$clear=false) {
if (!$clear) { //if clear is true, then it will skip the session vars and all.
//check for session first
if ($_SESSION['redirect']!='') $url=$_SESSION['redirect'];
//url request supercedes session var
//use this for special overrides- allows for universal return pages
if ($_REQUEST['redirect']!='') $url=$_REQUEST['redirect'];
}
//replace special chars so that you get less errors
$url=str_replace('-','=',str_replace('+','&',$url));
unset($_SESSION['redirect']); // clear
if (substr($url,0,1)!="?") $url="?".$url;
//header("Location: $url");
echo "<script>window.location=\"$url\";</script>";
}
The thing is, sometimes this will be called before page headers are sent and sometimes after. If I use the header("location: $url") after headers then I get the error- headers have already been sent.
I want this script to be generic. In the past, I've simply added another variable in the function, but I want to do away with this variable and auto-detect:
Code:
function redirect($url,$clear=false,$js=false) {
...
if (!$js) use header redirect
else use javascript redirect
...
}
Do you know of a way to automatically detect whether headers have been sent or not?
"Computer illiterate is a dirty word."