Headers Already Sent [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Headers Already Sent [SOLVED]

Post 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?
Last edited by the9ulaire on Fri Feb 29, 2008 3:20 pm, edited 1 time in total.
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Re: Headers Already Sent

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Headers Already Sent

Post by Christopher »

Output is not just echos. Did you check for things like spaces or newlines after "?>" ?
(#10850)
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Re: Headers Already Sent

Post 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?
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Re: Headers Already Sent

Post by the9ulaire »

Ah, yes, yes, I found the problem. Stupid me!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Headers Already Sent

Post 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).
(#10850)
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Re: Headers Already Sent [SOLVED]

Post 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.
Post Reply