Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to detect if headers have been sent

Status
Not open for further replies.

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:
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."
 
was that all? i did a whole bunch of googling on this one! Well, thanks!

"Computer illiterate is a dirty word."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top