PDA

View Full Version : cannot add header information error



DoubleV
05-09-2003, 06:09 PM
on this particular server ever php page has to have
#!/usr/local/bin/php4
as its first line.

my php page has a redirect in it, but it does not execute. instead, i get the following:
Warning: Cannot add header information - headers already sent by (output started at /cgi/login.php4:4) in /cgi/login.php4 on line 152

line 152 is the one containing the redirect
header("Location:$location");

what's the workaround?
something like
header(current header here + "Location:$location");
?

Spookster
05-09-2003, 06:54 PM
That error means you have already outputted something to the browser before you tried to send the header. You cannot output anything to the browser before sending a header. That means you cannot echo() or print() anything before sending a header. Also make sure you don't have spaces before your <?php because that will output spaces to the browser.

DoubleV
05-09-2003, 07:16 PM
Spookster,
here's my page:


#!/usr/local/bin/php4

<head>
<title>Please Log In</title>
javascript and stylesheet go here
</head>

<body>

<?php

$pg = '<form name="frm" method="post" onKeyUp="highlight(event)" onClick="highlight(event)" onSubmit="return validate();" action="' . $_SERVER['PHP_SELF'] . '">' .
'Please sign in:<br><br>' .
'<table border="0">' .
'<tr>' .
'<td>Email: </td>' .
'<td><input type="text" name="email" value="' . $_POST['email'] . '">' .
'</tr>' .
'<tr>' .
'<td>Password: </td>' .
'<td><input type="password" name="pwd">' .
'</tr>' .
'<tr>' .
'<td colspan="2" align="right"><input type="submit" value="Submit" class="submit">' .
'</table>' .
'</form>';


if (!$_POST['email'] || !$_POST['pwd']) {
echo($pg);
}
else {
if(($_POST['email'] == "") || ($_POST['pwd'] == "")) {
echo('<span style="color:red">You did not fill in all the fields.</span><br/><br/>' .
$pg);
}
else {
if($_POST['pwd'] != "hello") {
echo('<span style="color:red">Please enter a valid password.</span>' .
$pg);
}

else {
send_mail();
$location = "hello.htm";
header("Location:$location");
}

}
}

function send_mail() {

$txt = "The following peraon had just accessed the restricted directory: " . $_POST['email'];


// $headers = "From: " . $_POST['Fname'] . " " . $_POST['Lname'] . "<" . $_POST['Email'] . ">\n" .

$headers = "From: " . $_POST['email'] . "<email here>\n" .
"X-Mailer: PHP\n" . //mailer
"Return-Path: " . $_POST['email'];

@mail('my email here','Your directory was accessed',$txt,$headers);

}

?>

</body>
</html>


i am not writing anything to a browser in the else portion containing teh redirect. am i not getting the point?

Nightfire
05-09-2003, 07:24 PM
Can't have the below, before the header

<head>
<title>Please Log In</title>
javascript and stylesheet go here
</head>

<body>

DoubleV
05-09-2003, 07:29 PM
so what should i do?
what should i modify for the whole thing to work?
move that into <?php part?

Nightfire
05-09-2003, 10:06 PM
A simple way would be to do this


<?php
$htmlheaders = "
<html>
<head>
<title>Please Log In</title>
javascript and stylesheet go here
</head>

<body>";

$pg = '<form name="frm" method="post" onKeyUp="highlight(event)" onClick="highlight(event)" onSubmit="return validate();" action="' . $_SERVER['PHP_SELF'] . '">' .
'Please sign in:<br><br>' .
'<table border="0">' .
'<tr>' .
'<td>Email: </td>' .
'<td><input type="text" name="email" value="' . $_POST['email'] . '">' .
'</tr>' .
'<tr>' .
'<td>Password: </td>' .
'<td><input type="password" name="pwd">' .
'</tr>' .
'<tr>' .
'<td colspan="2" align="right"><input type="submit" value="Submit" class="submit">' .
'</table>' .
'</form>';


if (!$_POST['email'] || !$_POST['pwd']) {
echo($pg);
}
else {
if(($_POST['email'] == "") || ($_POST['pwd'] == "")) {
echo('<span style="color:red">You did not fill in all the fields.</span><br/><br/>' .
$pg);
}
else {
if($_POST['pwd'] != "hello") {
echo('<span style="color:red">Please enter a valid password.</span>' .
$pg);
}

else {
send_mail();
$location = "hello.htm";
header("Location:$location");
}

}
}

function send_mail() {

$txt = "The following peraon had just accessed the restricted directory: " . $_POST['email'];


// $headers = "From: " . $_POST['Fname'] . " " . $_POST['Lname'] . "<" . $_POST['Email'] . ">\n" .

$headers = "From: " . $_POST['email'] . "<email here>\n" .
"X-Mailer: PHP\n" . //mailer
"Return-Path: " . $_POST['email'];

@mail('my email here','Your directory was accessed',$txt,$headers);

}

echo $htmlheaders;
?>

</body>
</html>


<edit>Just noticed that if it's done my way, all your stuff will be before the body tag... but hopefully you understand where html and stuff should be... before the header