1

Possible Duplicate:
Headers already sent by PHP

I'm having issues with a login form for my website. At the top of the login.php form I have this

<?php 
if (isset($_SESSION['username'])){
header("Location: http://myurl/"); die;}?>
<?php require 'includes/dbconnect.php' ; 
?> 
<?php require 'includes/header.php';  ?>

and when login is successful I redirect them to

header('Location: http://myrul/');

header.php has

<?php session_start(); ?>

at the top.

When I tried to login, I get this error message:

Warning: Cannot modify header information - headers already sent by (output started at /path/to/file/login.php:7) in /path/to/file/login.php on line 39.

login.php:7 ==> <?php require 'includes/header.php'; ?>

while

Line 39 in login.php is ==> header('Location: http://myrul/');

Please where is the problem

UPDATE I moved require 'includes/header.php to the top of the file.

<?php require 'includes/header.php';?>
if (isset($_SESSION['username'])){
header("Location: http://murl"); die;}?>
<?php require 'includes/dbconnect.php' ; ?> 

UPDATE 2

<?php require 'includes/header.php';?>
<?php if (isset($_SESSION['username'])){
header("Location: http://myurl/"); die;}?>
<?php require 'includes/dbconnect.php' ; ?> 

                $username = trim ($_POST['username']);

Pointing to $username = trim ($_POST['username']); as output started at /path/to/file/header.php:11

UPDATE

header.php

<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" content="Your description goes here" />
<meta name="keywords" content="your,keywords,goes,here" />
<meta name="author" content="Your Name" />
<link rel='stylesheet' type='text/css' href='css/theme.css' />
<link rel='stylesheet' type='text/css' href='css/fullcalendar.css' />
<link rel='stylesheet' type='text/css' href='css/fullcalendar.print.css' media='print' />
<link rel="stylesheet" href="css/jquery.tagbox.css" />
Community
  • 1
  • 1
ilp
  • 87
  • 1
  • 7

2 Answers2

2

If header.php has your session_start() call, it needs to be right at the top of the file. As it is, I expect that you're getting the "Headers already sent" message because there's a linebreak in your code, after the dbconnect include.

When you use session_start, it's best to put it right at the top of every file that's going to use it; that helps make sure that there are no headers sent before the session can be started.

andrewsi
  • 10,807
  • 132
  • 35
  • 51
  • thanks for the answer: I have moved headers to the top of the page. But still get the same error – ilp Sep 19 '12 at 03:48
  • Could you update your question with the current code? – andrewsi Sep 19 '12 at 03:50
  • Is it still giving you the same error message, or has the line number changed? – andrewsi Sep 19 '12 at 03:56
  • When I try in localhost it works fine – ilp Sep 19 '12 at 03:59
  • But if you've moved header.php, it's no longer on line 7 - is that right? That's terribly confusing... You might have output buffering turned on, on localhost, which might also be a solution: http://php.net/manual/en/function.ob-start.php – andrewsi Sep 19 '12 at 04:03
  • I didnt upload to server the first time . Thanks – ilp Sep 19 '12 at 04:05
  • You don't need to have the second include for header.php if it's already included at the top of the file – andrewsi Sep 19 '12 at 04:07
  • I only have one includes for header.php – ilp Sep 19 '12 at 04:14
  • I'm pretty much stumped, in that case - assigning $username shouldn't output anything at all, and if the session's already started.... Could you please post the full header.php ? And have you tried Mariya's suggestion? – andrewsi Sep 19 '12 at 04:21
  • And it's still telling you the error is on line 11 of header.php? – andrewsi Sep 19 '12 at 04:32
  • Yes. It is. Can I provide link to the full scripts ? – ilp Sep 19 '12 at 07:40
  • Sure - just edit it into your question, and I'll have a look when I get chance. – andrewsi Sep 19 '12 at 12:02
0

try placing this at the top of your page:

<? ob_start(); ?>

then at the bottom of the page place this line of code:

<? ob_flush(); ?>
Kichu
  • 3,284
  • 15
  • 69
  • 135