Jump to content

Error while using HEADER() to direct to another page


karthikjayraj

Recommended Posts

Hi,

 

I found a code for  login page on web which would check against the database and login, I wanted to modify it so that once the password is verified the user will be directed to a new page with the session variables also active. I read about " header()" functionality which would achieve this so i added this to the code but my code seems to fail with the error

 

"Cannot modify header information - headers already sent by (output started at C:\xampp\phpMyAdmin\scripts\login1.php:7) in C:\xampp\phpMyAdmin\scripts\login1.php on line 10

"

 

The code for login1.php is as below- For quick understanding i have removed all the section related to DB validation ..so the below code must activate the next page "add_entry2.php" immediately after submit button is pressed. Please see below..

 

Thanks in advance.

 

 

 

 

 

 

 

<html>
<head>
<title>Login</title>
</head>
<body>
<?php
if (isset($_POST['submit'])) {if(!$_POST['uname'] | !$_POST['passwd']){die('You did not fill in a required field.');
    }	
header('Location: add_entry2.php');} else {// if form hasn't been submitted
?>
<h1>Login</h1>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table align="center" border="1" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td>
<input type="text" name="uname" maxlength="40">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="passwd" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
<?php
}
?>
</body>
</html>

Link to comment
Share on other sites

Yes I did and have checked for

 

1.whitespaces/unwanted lines but dint get through

2. My form will not post anything on the output after it has been submitted for the first time..what i mean is i am not posting anything before the Header() function.

 

OR if you think there is some other command where i can direct my page based on success of some if condition NOT click then please do let me know.

 

Thanks in advance.

Link to comment
Share on other sites

Thanks for your instant replies Pikachu.. :D

 

I changed the code to look like this below and it worked!!!!

 

 

<?php
if (isset($_POST['submit'])) {
if(!$_POST['uname'] | !$_POST['passwd'])
{print'You did not fill in a required field.';
}
header('Location: add_entry2.php');
}else{
echo "<form action=\"login1.php\" method=\"POST\">";
echo "Username: <input name=\"uname\" size=\"15\"><br />";
echo "Password: <input type=\"password\" name=\"passwd\" size=\"8\"><br />";
echo "<input type=\"submit\"  name=\"submit\" value=\"Login\">";
echo "</form>";}
?>

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.