Another way to redirect?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Milan
Forum Commoner
Posts: 97
Joined: Wed May 17, 2006 6:08 pm

Another way to redirect?

Post by Milan »

I have a line
header("Location: ". "../buyer4d.php?result=canceled&session=".$_GET['session']);

to return the buyer to the original page is he canceled his order but when paypal tries to return to it it gives me error:

Warning: Cannot modify header information - headers already sent by (output started at c:\program files\apache group\apache\htdocs\pickageek\secrfld\paypal.class.php:2) in c:\program files\apache group\apache\htdocs\pickageek\secrfld\annpaypal.php on line 60

is there another way to redirect? or trace the problem in this code?
thanks!
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

You could always have a 'redirecting' page, that does all the processing and whatnot, and then once done, just writes out a meta-refresh tag to the page you want to return to. Like the typical "Thank you, redirecting in 5 seconds", type thing.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Another way to redirect?

Post by RobertGonzalez »

Milan wrote:I have a line
header("Location: ". "../buyer4d.php?result=canceled&session=".$_GET['session']);

to return the buyer to the original page is he canceled his order but when paypal tries to return to it it gives me error:

Warning: Cannot modify header information - headers already sent by (output started at c:\program files\apache group\apache\htdocs\pickageek\secrfld\paypal.class.php:2) in c:\program files\apache group\apache\htdocs\pickageek\secrfld\annpaypal.php on line 60

is there another way to redirect? or trace the problem in this code?
thanks!
Couple of things...
1) Use full URL's using the header() function.
2) The error you are getting is because there was output sent to the browser already. Make sure there is nothing sent to the browser before calling header().
3) You can always use meta redirection in your HTML or a JavaScript redirect if you want. I usually use meta redirection as a fallback to servers that may not support the header() redirect.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

servers that may not support the header() redirect
Have you encountered any? 8O
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I borrowed this snippet from the phpBB codebase. I have never put PHP code on a WebSTAR or Xitami server. Not sure about Microsoft.

Code: Select all

<?php
// Redirect via an HTML form for PITA webservers
function redirect($url)
{
	if (@preg_match('/Microsoft|WebSTAR|Xitami/', getenv('SERVER_SOFTWARE')))
	{
		header('Refresh: 0; URL=http://www.mysite.com/'. $url);
		echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><meta http-equiv="refresh" content="0; url=http://www.mysite.com/' . $url . '"><title>Redirect</title></head><body><div align="center">If your browser does not support meta redirection please click <a href="http://www.mysite.com/' . $url . '">HERE</a> to be redirected</div></body></html>';
		exit;
	}

	// Behave as per HTTP/1.1 spec for others
	header('Location: http://www.mysite.com/' . $url);
	exit;
}
?>
Post Reply