Jump to content


Photo

need help on uncontrollable Header function


  • Please log in to reply
4 replies to this topic

#1 kwstephenchan

kwstephenchan
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 02 April 2006 - 02:59 AM

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!!

#2 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 02 April 2006 - 03:12 AM

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

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 02 April 2006 - 04:21 AM

You should only be doing the check and the redirect if the form has been submitted. Change your code to be something like this:
<?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");
}
?>

Ken

#4 kwstephenchan

kwstephenchan
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 02 April 2006 - 02:12 PM

[!--quoteo(post=360808:date=Apr 2 2006, 12:21 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 2 2006, 12:21 PM) View Post[/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:
<?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");
}
?>

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

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 02 April 2006 - 07:16 PM

I would of coded your script the opposite way around like so:
<?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>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users