Conditional Re-direction - Impossible?

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

GarthThe1st
Forum Newbie
Posts: 6
Joined: Tue Sep 04, 2007 1:03 am
Location: England

Conditional Re-direction - Impossible?

Post by GarthThe1st »

1st set context: I am a PHP newbie. :oops:

State problem: I want the browser to display a page that contains text content, menus that change the content, images, Google adsense ads and a subscription form. If the user fills in the subscription form and clicks Submit, the form's action is to return to the same page - where the php field validation code runs. Then comes the problem, because if the fields are all validated, I need then to re-direct the browser to a new page on a different domain (where resides an autoresponder) with a $url="http://newdomain/page?querystring".

The only URL re-direction I have been able to find in PHP is header(Location: $url). But this simply gives a "headers already sent" error if used as above. However, others have advised me that turning on output buffering will solve this.

I have a problem understanding this advice. Surely I would have to flush the buffer for the user to be able to see the page - and once I do that, the headers have already been sent. So if the user completed the form, my re-direction would fail with the "headers already sent" error.

Am I attempting the impossible in PHP? (If I were on a Windows server with ASP, it would be so easy - response.redirect(url) and the job's done!)

If I'm stupid, tell it to me how it is.....
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

:) You should use header() before any output. You can run the validation then redirect if pass:

Code: Select all

if($_POST['form_fields']) {
 if(validate rule) {

   header('Location:....')
 }

}
<HTML>
<HEAD>
  <TITLE>
   ......
vinoth
Forum Contributor
Posts: 113
Joined: Thu Aug 02, 2007 3:08 am
Location: India
Contact:

Post by vinoth »

hi GarthThe1st

I think header () will satisfies your needs..
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

miro_igov wrote::)

Code: Select all

if($_POST['form_fields']) {
 if(validate rule) {

   header('Location:....')
 }

}
<HTML>
<HEAD>
  <TITLE>
   ......
Just a quick tip... Always insert "exit;" immediately after any header redirection call (otherwise redirection waits until code completes which could have nasty side effects if you are not careful which would be hard to debug).
vinoth
Forum Contributor
Posts: 113
Joined: Thu Aug 02, 2007 3:08 am
Location: India
Contact:

Post by vinoth »

The header will redirect the page to some other location
Then why we need "exit" in the same page..

It may working fine without "exit" also..
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

vinoth wrote:The header will redirect the page to some other location
Then why we need "exit" in the same page..

It may working fine without "exit" also..
If the code after the redirect also sets a header, another redirect for example, things might go very strange indeed. Plus, if the content produced after the header isn't ever going to be seen then it's a waste of CPU time generating it and a waste of bandwidth sending it to the user.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Even worst possibilities exist.. Adding extra rows to databases etc...
GarthThe1st
Forum Newbie
Posts: 6
Joined: Tue Sep 04, 2007 1:03 am
Location: England

Post by GarthThe1st »

Ah-hah! Thanks, guys. Now I understand a bit better.

I feel good because I got the redirection to happen. I feel bad because it went to the wrong place. Instead of making the URL an absolute address, it made it relative.

The page I'm working on is in thisdomain and my $url contained "http://www.newdomain/page.html?first=one&second=two".

Upon submitting the form data, I was taken to "http://www.thisdomain/http://www.newdom ... second=two".

I got an error. Is there some other setting that would affect absolute/relative ?
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Post your code. If you put http:// in the location it is absolute, without http:// is relative ;)
GarthThe1st
Forum Newbie
Posts: 6
Joined: Tue Sep 04, 2007 1:03 am
Location: England

Post by GarthThe1st »

Here's the code:

<?php

Code: Select all

/* bool */ function isitvalid2($email)
{
	return eregi("^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$", $email);
}

if(isset($_POST['go'])) {
	if(trim($_POST['sendName']) == "") {
		$error[] = "You did not enter your  first name.";
	}

	$_POST['sendEmail'] = trim($_POST['sendEmail']);

	if(!isitvalid2($_POST['sendEmail'])) {
		$error[] = "Your email address appears to be invalid.";
		unset($_POST['sendEmail']);
	}

	if($error) {
		echo "<center>\n";
		foreach($error as $oneError) {
			echo "$oneError<br>\n";
		}
		echo "</center>\n";

		// echo show_subform();

	} else { // form is set and no error
		$yF = $_POST['sendName'];
		$yE = $_POST['sendEmail'];
		$yS = $rooturl;
		$yH = true;  
		$xAction = "S";
		$xCMP = "1004";
		$xURL = "www.source-here.com";
		$xRedirect = $rooturl;

		//QueryString Redirect to EmailMover Installation
		$myurl = "http://www.source-here.com/EmailMover/EMsignup.asp?xAction=S&xE=" . $yE . "&xF=" . $yF . "&xS=" . $yS . "&xH=" . $yH . "&xCMP=" . $xCMP . "&xURL=" . $xURL . "xRedirect=" . $xRedirect;
		$myurl = urlencode($myurl);
		if (!headers_sent()) {
			header("Location: $myurl");
			exit();
		}
	}

}
?>
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Looks fine, what error actually you get?
GarthThe1st
Forum Newbie
Posts: 6
Joined: Tue Sep 04, 2007 1:03 am
Location: England

Post by GarthThe1st »

The browser does not go to the value of $myurl - which is:
http%3A%2F%2Fwww.source-here.com%2FEmailMover%2FEMsignup.asp%3FxAction%3DS%26xE%3Dbob%40telemagic-uk.com%26xF%3DBob%26xS%3D%26xH%3D1%26xCMP%3D1004%26xURL%3Dwww.source-here.comxRedirect%3D

It goes to:

http://cookinghappiness.com/chinese/http%3A%2F%2Fwww.source-here.com%2FEmailMover%2FEMsignup.asp%3FxAction%3DS%26xE%3Dbob%40telemagic-uk.com%26xF%3DBob%26xS%3D%26xH%3D1%26xCMP%3D1004%26xURL%3Dwww.source-here.comxRedirect%3D

(http://cookinghappiness.com/chinese/index.php is the current location.)

I've tested this with a simpler script and got the same result - relative not absolute. It's not like it says in the manual.

Thanks for your help.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

You need to urldecode() the url.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Actually, try not urlencoding the entire thing, only the parts after the base URL. And try echoing out the URL just before the header call (for testing, without actually calling the header call) to see where the header is about to redirect to.

Lastly, what server software are you running (Apache, IIS, Lighttpd, etc)?
GarthThe1st
Forum Newbie
Posts: 6
Joined: Tue Sep 04, 2007 1:03 am
Location: England

Post by GarthThe1st »

That's it!

Removing the urlencode() in its entirety worked. I also tried (as suggested) urlencoding only the part after the domain root but that did not work.

The server is Apache - hosted by Kiosk.

Thanks for all the help and advice, guys - really appreciated.
Post Reply