dtleahy
asked on
php redirect header conditional querystring
(I am a beginner in php)
I can set up a redirect that works.
and
fill-in the URL's querystring variables:
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
I can set up a redirect that works.
<?php
header("Location: http://www.MyWebsite.org/SomePage.php");
## more code here
?>
...but what I want to do is to redirect based on conditions in the codeand
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();
}
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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