...

View Full Version : cannot modify header information



InterbredMonkey
01-07-2010, 12:37 PM
I am trying to make a form to allow a user to sign into the webpage however I wanted to make the php check if the user had filled out all the details on the form and if not then display a message and then go back to the login page. I have put the header alteration in but it states that the header information has already been changed.

I think I know what is wrong as I am requesting information from the previous page before I can call the change of page but I dont know how I would do it any other way.

Any help would be greatly appreciated.

Code:

[code]
<?php
if(!empty($_REQUEST['email'])){$email = $_REQUEST['email'];}
else{$email = NULL; echo "<p><b>You have forgot to enter your email address. Please sign in again.</b>";};

if(!empty($_REQUEST['password'])){$password = $_REQUEST['password'];}
else{$password = NULL; echo "<p><b>You have forgot to enter your password. Please sign in again.</b>";};

$_REQUEST['signin'];

if($password && $email){
$dbcheck = mysql_query("select * from users where username = '$email'") or die(mysql_error());
}
else
{
sleep(5);
header('location: login.html');
};
?>
[code]

abduraooft
01-07-2010, 12:47 PM
See http://php.net/header
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
Do you have anything echoed or any html tags before that php code?

InterbredMonkey
01-07-2010, 01:22 PM
yeah I have HTML above it. I have taken it out and still the same. There is also an echo above the header statment.

The only issue is that before I wanted to re-direct the page I wanted to display a message to let the user know and this will not work.

abduraooft
01-07-2010, 01:29 PM
There is also an echo above the header statment.
Instead of echoing the error there, save it to a variable, say

$error="You have forgot to enter your email address. Please sign in again";
or

$error="You have forgotten to enter your password. Please sign in again"; and then change your header like

if(!isset($error)){
$dbcheck = mysql_query("select * from users where username = '$email'") or die(mysql_error());
header('location: success.html');
}
else
{
//echo error here

};
ie, post your form to the same page and echo the error, if any. Otherwise do the action and redirect to a new page.

JAY6390
01-07-2010, 01:53 PM
Better still, create a session variable and sacve the error message to that, then check for that session variable being set on your login page, and display it if it exists. Also with headers sent errors, check the error to see what line and file it says the output started. The line it started is where you need to find what is causing the output, and stop it. You should avoid echoing anything if possible if you have headers being sent

JAY6390
01-07-2010, 01:59 PM
Also, one other method if you want to show your error message for say 5 seconds then move to the login page is to use the meta refresh method

<meta http-equiv="refresh" content="5;url=http://www.yoursite.com/login.php">
5 is the number of seconds to wait before redirection

InterbredMonkey
01-07-2010, 02:50 PM
YATTA!!!

Thank you very very much everyone who has posted.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum