Jump to content

need help on uncontrollable Header function


kwstephenchan

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!!
Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.