Solved

php redirect header conditional querystring

Posted on 2012-03-30
3
538 Views
Last Modified: 2012-03-30
(I am a beginner in php)

I can set up a redirect that works.
<?php
header("Location: http://www.MyWebsite.org/SomePage.php");
## more code here 
?>

Open in new window

...but what I want to do is to redirect based on conditions in the code
and
fill-in the URL's querystring variables:
<?php
require_once "Mail.php";
include_once "MyConnect.php";

## create and assign default values to 3 variables:
$successful=FALSE;
$redirt=99;
$redirm=99;

## processing code here
## variables would be the querystring values

if(blablabla)
	{
	$successful=FALSE;
	$redirt=42;
	$redirm=31;
	}
else
	{
	if(something)
		{
		$successful=TRUE;
		$redirt=4;
		$redirm=2;
		}
	else
		{
		$successful=TRUE;
		$redirt=28;
		$redirm=13;
		}
   }

## ok, now, use the variables in the querystring AND use the correct header redirect destination:

if(!$successful)
{
header("Location: http://www.MyWebsite.org/SignUpPage.php?paratitle=".$redirt."&msgbody=".$redirm."");
exit();
}
else
{
header("Location: http://www.MyWebsite.org/MessagePage?paratitle=".$redirt."&msgbody=".$redirm."");
exit();
}

Open in new window


but I am getting the dreaded error:
"Warning: Cannot modify header information - headers already sent by..."

I understand that I cannot send anything to a browser before calling the "header" function. What I think that means in that I cannot use "echo" anywhere on the page. Or maybe I can have an echo on the page, but if it is invoked, the redirect will not work and I'll get the error.

My page is a processing page, all php, no HTML.  I know that MyConnect.php is strictly php, no HTML, but I am not sure about the Mail.php file (maybe that is the problem.)

So, is this doable? If yes, is my coding problem obvious?

Thanks!

Dennis
0
Comment
Question by:dtleahy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 15

Accepted Solution

by:
StingRaY earned 450 total points
ID: 37786821
It's not only HTML that may cause such warning, but "new line" character also does.
You can use output buffering to avoid this problem.

<?php
ob_start(); // start buffering
require_once "Mail.php";
include_once "MyConnect.php";

## create and assign default values to 3 variables:
$successful=FALSE;
$redirt=99;
$redirm=99;

## processing code here
## variables would be the querystring values

if(blablabla)
	{
	$successful=FALSE;
	$redirt=42;
	$redirm=31;
	}
else
	{
	if(something)
		{
		$successful=TRUE;
		$redirt=4;
		$redirm=2;
		}
	else
		{
		$successful=TRUE;
		$redirt=28;
		$redirm=13;
		}
   }

## ok, now, use the variables in the querystring AND use the correct header redirect destination:

ob_end_clean(); // end buffering and empty the buffer

if(!$successful)
{
header("Location: http://www.MyWebsite.org/SignUpPage.php?paratitle=".$redirt."&msgbody=".$redirm."");
exit();
}
else
{
header("Location: http://www.MyWebsite.org/MessagePage?paratitle=".$redirt."&msgbody=".$redirm."");
exit();
}

Open in new window

0
 
LVL 14

Assisted Solution

by:Scott Madeira
Scott Madeira earned 50 total points
ID: 37786924
One thing to try would be to go into Mail.php and MyConnect.php and remove the ?>  from the end of each file.  If there is whitespace after the ?> in the file you will get this error.

Also, if there is any output from those files echoed to the screen you will get that problem too.
0
 

Author Comment

by:dtleahy
ID: 37787211
Thanks to StingRay and smadeira for the rapid replies.

StingRay nailed it:
ob_start(); // start buffering
worked perfectly. Thanks! I'll have to read-up on the ob_start() function, and see what it does.

I'll award 450 of the 500 points to StingRay, and 50 to smadeira. So,smadeira, the php closing tag is optional?

The Mail.php file is the one that would be on the server environment when the hosting company loaded php. I assume it is all php functions, but you never know. I was really hoping that I did not have to get a copy of Mail.php from the server environment, and possibly recreate a version with no output.

Thanks again!

Dennis
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Powershell PGP and SFTP 1 49
MySQL - Adding an incremented value to a duplicate string 30 53
Can't find an open element in HTML 5 55
Writing reports to a script 7 34
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question