Jump to content

Where to exit after Redirect


doubledee

Recommended Posts

as long as it wont fire any unexpected code, then there should be no problems.

 

Well, I've always been confused about this...

 

How can you "exit" a script if you have already left it via redirect?!  :shrug:

 

It seems like it is too late at that point, right?!

 

 

Debbie

 

Link to comment
Share on other sites

the header function sends the header. if the browser wants to obey that header is another question. it is possible to turn off the redirect headers for most browsers. don't rely on your visitors hardware.

 

So this should work, right?

 

if ($articleFound){
	// Redirect to Article.
	header("Location: " . BASE_URL . $_SESSION['returnToPage'] . "#boxComments");

}else{
	// Redirect to Last Page.
	if (isset($_SESSION['returnToPage']) && ($_SESSION['returnToPage'] !== '/account/create.php')){
		// Return to Last Page.
		header("Location: " . BASE_URL . $_SESSION['returnToPage']);
	}else{
		// Go to Home Page.
		header("Location: " . BASE_URL . "/index.php");
	}
}//End of DETERMINE REDIRECT TYPE

// End script.
exit();

 

 

Thanks,

 

 

Debbie

 

Link to comment
Share on other sites

How can you "exit" a script if you have already left it via redirect?!  :shrug:

 

Sending a location header for a redirect has no effect on how the script runs.  All it does is queue up another header to be sent when the script does exit (or start sending output).  PHP will happily continue to execute your script beyond the header call. As such, it is generally advisable to put an exit after your redirects to tell PHP to stop there rather than keep running the script.  If you don't you may have vulnerabilities in your code that you don't notice because things might "appear" to work successfully.

 

That said, putting your exit after a bunch of if statements that all send various redirects is fine.  You just need to have it somewhere to prevent the remainder of the script from running and possibly doing bad things.

 

Link to comment
Share on other sites

How can you "exit" a script if you have already left it via redirect?!  :shrug:

 

Sending a location header for a redirect has no effect on how the script runs.  All it does is queue up another header to be sent when the script does exit (or start sending output).  PHP will happily continue to execute your script beyond the header call. As such, it is generally advisable to put an exit after your redirects to tell PHP to stop there rather than keep running the script.  If you don't you may have vulnerabilities in your code that you don't notice because things might "appear" to work successfully.

 

That said, putting your exit after a bunch of if statements that all send various redirects is fine.  You just need to have it somewhere to prevent the remainder of the script from running and possibly doing bad things.

 

Okay, sounds good.

 

Thanks!

 

 

Debbie

 

Link to comment
Share on other sites

You seem to have this sorted but I figured I would leave this anyway. I like to use a function like the one below to handle redirects so that I know the script is always terminated. Plus, the shorthand is nice.

function redirect($url, $type = 'location', $delay = 3)
{
if ($type == 'refresh') {
	header("refresh:$delay;url=$url");
} else {
	header("location:$url");
}

exit;
}

// usage
redirect('http://google.com'); // sends "location" header

redirect('http://google.com', 'refresh', 1); // sends "refresh" header with a 1 second delay

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.