0

Possible Duplicate:
Headers already sent by PHP

So my website is based off of a script that includes header, pages from a folder, then the footer. I now tried to bring in a login system, but the issue is that I keep getting headers already sent. This is the code I have to make it so that if the session isnt set, redirect.

<?php 

if(empty($_SESSION['user'])) 
{ 

    header("Location: login.php"); 

    die("Redirecting to login.php"); 
} 
?> 

Now am I right in saying, that the headers already sent issue, which you can see first hand here - http://www.cogameservers.com/ncms isn't this caused because the header content is already being displayed, causing headers already sent?

So I have a variable set as a $_GET variable called p for page, could I make a bunch of if else statements to make it so that if $_GET["p"] == home, then I execute the headers at the top of my header.php, being the page first called.

I'm sorry this may seem confusing, please leave requests on code you would like to see


index.php

<?php 
include($_SERVER['DOCUMENT_ROOT'].'/ncms/lib/php/_dc.php');
include($_SERVER['DOCUMENT_ROOT'].'/ncms/lib/php/_ncms_fns.php');

$script   = $_SERVER['SCRIPT_NAME'];
error_reporting(E_ALL);
include($_SERVER['DOCUMENT_ROOT'].'/ncms/default/header.php');
if($_GET["p"] == 'ncms' || !isset($_GET["p"])) {
    include($_SERVER['DOCUMENT_ROOT'].'/ncms/pages/home.php');
} else if(file_exists($_SERVER['DOCUMENT_ROOT'].'/ncms/pages/'.$_GET["p"].'.php')) {
    include($_SERVER['DOCUMENT_ROOT'].'/ncms/pages/'.$_GET["p"].'.php');    
} else {
    include($_SERVER['DOCUMENT_ROOT'].'/ncms/default/404.php'); 
}
include($_SERVER['DOCUMENT_ROOT'].'/ncms/default/footer.php');
?>

Thank you - necro.

Community
  • 1
  • 1
Necrohhh
  • 223
  • 2
  • 8
  • 13

5 Answers5

2

That is because you have error_reporting(E_ALL); and it will display a notice when a variable as $_GET doesn't exist this will cause the headers already sent notice.

replace with:

if(!isset($_GET["p"])) {
    include($_SERVER['DOCUMENT_ROOT'].'/ncms/pages/home.php');
} else if(file_exists($_SERVER['DOCUMENT_ROOT'].'/ncms/pages/'.$_GET["p"].'.php')) {
    include($_SERVER['DOCUMENT_ROOT'].'/ncms/pages/'.$_GET["p"].'.php');    
} else {
    include($_SERVER['DOCUMENT_ROOT'].'/ncms/default/404.php'); 
}

If $_GET['p'] doesn't exist you get that undefined index notice.

or you can change:

$_GET["p"] = isset($_GET["p"]) ? $_GET["p"] : '';
if($_GET["p"] == 'ncms' || $_GET["p"] == '') {

and add header after the code execution, no output should exist before headers.

I see that your problem still persists in common.php on line 85. as it outputs something. verify that

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
0

That happens when you try to send some data before sending the headers... Because you already sent them with the data. The redirect must come before anything else on the page. That includes your large Header text and all the errors too. This also includes any HTML (visible or not) that gets sent from the server to the browser. Any time the server sends the browser anything, it sends the headers and you can't send them again.

So move the code that redirects to be above all the "Header" text stuff and fix the other errors.

sachleen
  • 30,730
  • 8
  • 78
  • 73
  • **Correction: Now I receive this while above my actual text data, but below my stylesheet includes, jQuery includes, and other things of that nature go to http://www.cogameservers.com/ncms to see. – Necrohhh Sep 29 '12 at 05:51
  • It has to be before *everything* – sachleen Sep 29 '12 at 06:02
0

You should call session_start before anything is outputted . or your code may be having some white space before <?php

Arun Killu
  • 13,581
  • 5
  • 34
  • 61
0

If it seems you have no trailing space at the start of header.php, then it may be a BOM issue. If your editor is configured to save file as "utf-8 with bom" or "utf-16 with bom", these "Byte Order Mask" are considered as output by php.

Jerome WAGNER
  • 21,986
  • 8
  • 62
  • 77
-1

Remove the spaces before <?php statemnet and try again.

Sometimes it will give error messages if there are spaces before the <?php statement.

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
MAK
  • 773
  • 4
  • 9
  • 30