Page 1 of 1
Headers Already Sent [SOLVED]
Posted: Wed Feb 27, 2008 4:26 pm
by the9ulaire
I use a page to transact various things. One of the functions is to login a user. After I set the session info, I redirect them using:
Code: Select all
function redirect($url) {
if (!headers_sent()) {
header('Location: http://' . $_SERVER['HTTP_HOST'] .
dirname($_SERVER['PHP_SELF']) . '/' . $url);
} else {
die('Could not redirect; Headers already sent (output).');
}
}
When I test this on my computer, it worked fine, but when I put it on my web server, the headers are already sent.
Ideas?
Re: Headers Already Sent
Posted: Wed Feb 27, 2008 4:34 pm
by the9ulaire
And before I'm told to check for echo, print, etc, here's my code processing it:
Code: Select all
<?php
session_start();
require("conn.php");
require("http.php");
if (isset($_REQUEST['action'])) {
switch ($_REQUEST['action']) {
case 'Login':
if (isset($_POST['user_email'])
and isset($_POST['user_pass'])) {
$sql = "SELECT * FROM users " .
"WHERE email = '" . $_POST['user_email'] . "' " .
"AND password = '" . $_POST['user_pass'] . "' " .
"AND activated = 1";
$result = mysql_query($sql) or die("Could not process user; " . mysql_error());
$row = mysql_fetch_array($result);
if (mysql_num_rows($result) == 1) {
session_start();
$_SESSION['user_id'] = $row['user_id'];
$_SESSION['user_name'] = $row['name'];
$_SESSION['user_pass'] = $row['password'];
$_SESSION['activated'] = $row['activated'];
$_SESSION['blocked'] = $row['blocked'];
if ($_SESSION['activated'] != 1) {
redirect('login.php?log=noaccess');
}
if ($_SESSION['blocked'] != 0) {
redirect('login.php?log=noaccess');
}
redirect('control_panel.php');
} else {
redirect('index.php?log=inv');
}
} else {
redirect('index.php?p=7&log=inv');
}
break;
conn.php just contains information to connect to the database. http.php includes the previous function.
Re: Headers Already Sent
Posted: Wed Feb 27, 2008 4:39 pm
by Christopher
Output is not just echos. Did you check for things like spaces or newlines after "?>" ?
Re: Headers Already Sent
Posted: Wed Feb 27, 2008 4:55 pm
by the9ulaire
arborint wrote:Output is not just echos. Did you check for things like spaces or newlines after "?>" ?
Yeah, looking around the web I saw that. Nothing after the final ?>
The code above doesn't contain all of the switches. Actually, what's weird, is on my server, all of my redirects don't work. All of them used to work when testing on my own computer, but on my server, they don't work. Is there some setting that could be affecting this?
Re: Headers Already Sent
Posted: Wed Feb 27, 2008 7:56 pm
by the9ulaire
Ah, yes, yes, I found the problem. Stupid me!
Re: Headers Already Sent
Posted: Wed Feb 27, 2008 8:08 pm
by Christopher
You are not the first to have that problem.

One thing that may help is to remove the ?> a the end of all your PHP files. According to Zend they are not required (EOF does the same thing).
Re: Headers Already Sent [SOLVED]
Posted: Fri Feb 29, 2008 3:22 pm
by the9ulaire
Also, just in case someone else runs into this, I'll state what fixed it.
I had comments in my conn.php files after the info.