View Full Version : Headers already sent
tralsi
09-11-2009, 10:46 AM
Hi all !
I am a new kid on the PHP Block. I am using dreamweaver CS3 with WAMP.
While performing as decribed in one of the tutorial, I am getting the following error.
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\wamp\www\nationalEx\admin\login.php:1) in C:\wamp\www\nationalEx\admin\login.php on line 0
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\wamp\www\nationalEx\admin\login.php:1) in C:\wamp\www\nationalEx\admin\login.php on line 0
Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\nationalEx\admin\login.php:1) in C:\wamp\www\nationalEx\admin\login.php on line 66
Kindly advise me where & what to change.
Thanks in advance.
Tralsi
the line "session_start()" MUST be placed before any information is sent to the browser - it's best to put it in *immediately*, like so:
<?php
session_start();
//rest of your code...
JShor
09-11-2009, 10:15 PM
Unrelated question, and I usually don't use WYSIWYGs, but how do you like Dreamweaver CS3? Was it worth the $$?
tralsi
09-12-2009, 03:57 AM
Thanks Traq for your valuable suggestion !
I am able to manage the first two errors while my page loads.
unfortunately I still gets the following error after supplying username & password & while clicking on the submit button
Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\nationalEx\admin\login.php:4) in C:\wamp\www\nationalEx\admin\login.php on line 70
PHP Code
code from line no 1 to 10 & from line no 67 to 75 is pasted for your reference.
actually nothing has been changed to original code attached except the first 3 lines as suggested
<?php
session_start();
?>
<?php virtual('/nationalEx/Connections/ConnNationalEx.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
.
.
.
.
// code from line no 67 to 75
if (isset($_SESSION['PrevUrl']) && false) {
$MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
}
header("Location: " . $MM_redirectLoginSuccess );
}
else {
header("Location: ". $MM_redirectLoginFailed );
}
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
I am hopeful that you will able to guide me in solving this one also.
Thanks a lot again for your help.
Tralsi
JShor
09-12-2009, 04:03 AM
Your IF statement is set so that you have a header(); called no matter what. This line is probably the problem:
<?php virtual('/nationalEx/Connections/ConnNationalEx.php'); ?>
Somewhere in that page you're calling headers already [whether being hypertext or headers() or sessions or cookies or whatever].
Post the code from that page, or fix it so that headers are NOT called first from that page.
HTH:)
try like this
<?php
ob_start();
session_start();
virtual('/nationalEx/Connections/ConnNationalEx.php');
and continue your code by removing ur php open tag on 5th line.
and i think it is better to use include() or require() than virtual
JShor
09-12-2009, 04:14 PM
Agreed. You really don't need to use virtual();. && like I said, be sure headers are terminated.
tralsi
09-14-2009, 03:25 AM
Thanks for your reply JSHor & Hemi !
I tried the script change suggested by Hemi i.e.
<?php
ob_start();
session_start();
virtual('/nationalEx/Connections/ConnNationalEx.php');
even this didn't fix my problem so I am sending my code for ConnNationalEx.php as suggested by JShor
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_ConnNationalEx = "Localhost";
$database_ConnNationalEx = "nationalex";
$username_ConnNationalEx = "nationalEx";
$password_ConnNationalEx = "nationalEx";
$ConnNationalEx = mysql_pconnect($hostname_ConnNationalEx, $username_ConnNationalEx, $password_ConnNationalEx); or trigger_error(mysql_error(),E_USER_ERROR);
?>
djr33
09-15-2009, 07:47 PM
It can be very confusing once you get two (or more) pages embedded in each other. The real problem is when you end up having several page each with a header section and content section. This is actually similar to having two php pages embedded in each other each with <head> and <body> sections.
In either case, the best way to approach it is to either write some complex php with confusing logic in order to embed part of one into the other then the rest of it later, or you could just go with four pages that all work together (header1.php, header2.php, content1.php, content2.php), and if you just put those together in the right order then it should be fine.
But if you end up with something this complex then it is probably just time to rewrite the entire site so you are only sending headers once. If you can't, though, then, yes, it's time for something this complex.
Since this is so important for how your site will function, it's best to design around this rather than adding it on at the end (which seems easier when you start building a site piece-by-piece rather than structure down to content).
It will just take some practice and you'll find workarounds as you're aware of these issues.
The best way to think about this is to understand PHP as an html-generator. So you have your PHP code with stuff going on in it to change settings, etc., then after it's all ready to start putting out html you can do it and the environment is setup. For that reason, there are a number of things you should do at the start (especially header-specific code, but generally just any non-output php [setup stuff]), then after that put any content.
As a general rule your pages should look something like this, at least as much as possible:
<?php
//lots of php code here setting up the page
?>
<html>
....
</html>
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.