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>

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.

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>";}
?>

Archived

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

×
×
  • 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.