Jump to content

Archived

This topic is now archived and is closed to further replies.

kwstephenchan

need help on uncontrollable Header function

Recommended Posts

Hi evereyone!
I got the following simple code. Everytime I pre-run it, it goes directly to the Header function without asking for input (it does if I commented out the Header function), can anyone help and tell me what Have I done wrong, please!
Ob_start()
<p>  </p>
<form name="form1" method="post" action="">
<p>login (email):
<input type="text" name="em">
<input name="pass1" type="password">
<input name="submitf" type="submit">
<input name="resetf" type="reset">
</p>
</form>
<p> </p>
<?php
// accept input for user name – in the format of email
$uname=$_POST["em"];
// determine redirection destination depending on email input by user
if ($uname=="123@123.com") $newloc="abc.php"; else $newloc="123.php";
// redirect to new location
header("location: $newloc");
?>
ob_end_flush()

Thanks and appreciated!!

Share this post


Link to post
Share on other sites
the header function has to be used before ANY output to the browser
and all this is output
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]<p> </p>
<form name="form1" method="post" action="">
<p>login (email):
<input type="text" name="em">
<input name="pass1" type="password">
<input name="submitf" type="submit">
<input name="resetf" type="reset">
</p>
</form>
<p> </p>[/quote]

I'm surprised you're not getting a "Cannot modify header" error

Share this post


Link to post
Share on other sites
You should only be doing the check and the redirect if the form has been submitted. Change your code to be something like this:
[code]<?php
if(!isset($_POST['submitf'])) { ?>
<form name="form1" method="post" action="">
<p>login (email):
<input type="text" name="em">
<input name="pass1" type="password">
<input name="submitf" type="submit">
<input name="resetf" type="reset">
</p>
</form>
<?php
}else {
// accept input for user name – in the format of email
    $uname=$_POST["em"];
// determine redirection destination depending on email input by user
    if ($uname=="123@123.com") $newloc="abc.php"; else $newloc="123.php";
// redirect to new location
    header("location: $newloc");
}
?>[/code]

Ken

Share this post


Link to post
Share on other sites
[!--quoteo(post=360808:date=Apr 2 2006, 12:21 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 2 2006, 12:21 PM) [snapback]360808[/snapback][/div][div class=\'quotemain\'][!--quotec--]
You should only be doing the check and the redirect if the form has been submitted. Change your code to be something like this:
[code]<?php
if(!isset($_POST['submitf'])) { ?>
<form name="form1" method="post" action="">
<p>login (email):
<input type="text" name="em">
<input name="pass1" type="password">
<input name="submitf" type="submit">
<input name="resetf" type="reset">
</p>
</form>
<?php
}else {
// accept input for user name – in the format of email
    $uname=$_POST["em"];
// determine redirection destination depending on email input by user
    if ($uname=="123@123.com") $newloc="abc.php"; else $newloc="123.php";
// redirect to new location
    header("location: $newloc");
}
?>[/code]

Ken
[/quote]


Hi Ken and Zanus.
Thanks for helping.
After I checked for submit status, it works.
Zanus, I did not get the modify error message, because I have put in the ob_start() at the very beginning for buffering. I believe this is only available after PHP4.
Stephen

Share this post


Link to post
Share on other sites
I would of coded your script the opposite way around like so:
[code]<?php
if(isset($_POST['submitf'])) {
    // accept input for user name – in the format of email
    $uname=$_POST["em"];
    // determine redirection destination depending on email input by user
    if ($uname=="123@123.com") $newloc="abc.php"; else $newloc="123.php";
    // redirect to new location
    header("location: $newloc");
}
?>
<form name="form1" method="post" action="">
<p>login (email):
<input type="text" name="em">
<input name="pass1" type="password">
<input name="submitf" type="submit">
<input name="resetf" type="reset">
</p>
</form>[/code]

Share this post


Link to post
Share on other sites

×

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.