-1

Possible Duplicate:
“Warning: Headers already sent” in PHP

I am 100% certain that i'm not outputting any text before setting the headers in this script, but i get this error.

warning: Cannot modify header information - headers already sent by (output started at /home/x/public_html/index.php:4) in /home/x/public_html/core/functions.php on line 25

top of Index.php

<?php
require_once "core/functions.php";
$actions = new Actions();
$actions->isUserLogged($_SERVER['REMOTE_ADDR'], $_COOKIE['cData']);
?>
<!DOCTYPE html>...

core/functions.php

<?php

include_once('connection.php');

class Actions {

private $db;

public function __construct() {

    $this->db = new Connection();
    $this->db = $this->db->dbConnect( mysql info );

}

public function isUserLogged($currentIP, $cData) {
    if(!empty($cData)){ //if cookie is not empty
        $getData = $this->db->query("SELECT loggedIP FROM registery WHERE session='".$cData."'")->fetchColumn(); //finding entry in registery.
        $loggedIP = $getData['loggedIP'];
        if($currentIP == $loggedIP) {return true;} //if that loggedIP and currentIP matces -> proceed.
         else { return setcookie('cData','',time()-31536000); //this is where the error points.
          return header("location: login.php");} // else unset cookie and redirect to login.

    } else {    
         return header("location: login.php");
    }


    }   
}
?>

connection.php

<?php
error_reporting(E_ALL);

class Connection {

public function dbConnect($server, $username, $password, $db){

    return new PDO('mysql:host='.$server.';dbname='.$db, $username, $password);

}

}


?> 

if anyone would be so kind to take some time to find my mistake i would appreicate it!

SOLUTION Baba suggested to use "ini_set("display_errors","On");" and that magically solved the problem... i wonder why ty all! :-)

Community
  • 1
  • 1
  • 1
    Do you have any errors showing? Try commenting out the display errors line. – Cups Nov 10 '12 at 19:44
  • 1
    Keep in mind that any output at all will trigger this error. This includes whitespace (spaces, CRLF, etc.), and byte order marks (make sure that if your file is encoded in Unicode, it doesn't have a byte order mark). – Ynhockey Nov 10 '12 at 19:44
  • I think you use session_start, cookie_set or header *after* you print your html code. When you print html code, you send a header that says "200 OK, content type text/html" and with those function you want to modify this header, and it's impossible. – artragis Nov 10 '12 at 19:48
  • i can't see any space that should be there and i have saved the file as utf-8 without BOM. im out of ideas – Anders Fejerskov Nov 10 '12 at 19:55
  • A space probably after the closing tag in `connection.php`. Remove the closing tag. – air4x Nov 10 '12 at 19:56
  • not even, i have removed end tags of both connection.php and functions.php check for any spaces before or after end tags, searched for any space that should be, can't find anything – Anders Fejerskov Nov 10 '12 at 20:01
  • Try this [W3-checker](http://validator.w3.org/i18n-checker/), maybe it helps to detect problems with a BOM header. How could you test, that there is no BOM header, could you look at the raw file (with a hex editor for example)? – martinstoeckli Nov 10 '12 at 20:20
  • with notepad ++ you can format the file to utf-8 and utf-8 without BOM – Anders Fejerskov Nov 10 '12 at 20:26

3 Answers3

1

Not sure but you are dealing with empty space in connection.php

See

 <?php
^---------------------------   Most likely the cause  
error_reporting(E_ALL);

You should also try to display PHP errors in case it was switched off

error_reporting(E_ALL);
ini_set("display_errors","On");   
Baba
  • 94,024
  • 28
  • 166
  • 217
0

This might be a UTF-8 byte order mark. If this is the case, change the encoding to "utf-8 without BOM".

EDIT: how about a space here (connection.php, last row):

    ?> 
    --^------- there
cypher
  • 6,822
  • 4
  • 31
  • 48
0

That's quite a problem sometimes. I had it several times and decided to use javascript instead. Example:

echo '<script type="text/javascript">window.location ="login.php";</script>';

Maybe this will work for you also, although it is not the solution you are looking for.

Hope this helps.

Niko Sams
  • 4,304
  • 3
  • 25
  • 44
Felipe Alameda A
  • 11,791
  • 3
  • 29
  • 37