Jump to content

Session start code causing header conflict


Rheves

Recommended Posts

I have several pages on my site using the exact same code at the very top, which works fine except for one page:

 

<?php include_once("../members/dbinfo.php");

 

The top of dbinfo looks like this:

 

<?php session_start();

And then the connection strings for the database

 

However it is giving me this error on this one page:

 

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Inetpub\WebSites\premierathome_com\products\FrenchTalkingDictionary.php:1) in C:\Inetpub\WebSites\premierathome_com\members\dbinfo.php on line 1

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Inetpub\WebSites\premierathome_com\products\FrenchTalkingDictionary.php:1) in C:\Inetpub\WebSites\premierathome_com\members\dbinfo.php on line 1

 

Now the session does actually start and doesn't prevent anything from working, I just get those messages. I'm at a loss as to why a different page with the exact same code, save for searching the database for a different product version would have this problem.

 

I have had this code in place as well which works fine on other pages but doesn't help my problem at all:

 

if(!isset($_SESSION['OK'])){
session_start();
}

OK being a variable I set up during log in.

 

I'm at a loss, is anyone able to help me out with this?

Link to comment
Share on other sites

session_start() should only be at the top of the one page- the page that you are calling the include into.

 

<?php
session_start();
blahblahblah- code here
include("../members/dbinfo.php");
blahblahblah- more code here
?>

 

If you were to have session_start(); at the beginning of the page that you include-

you would then be calling session_start() twice in one page.

 

Hope that helps.

Link to comment
Share on other sites

I changed the code to

 

<?php
//include_once("../members/dbinfo.php");
session_start();
//DBINFO HERE

 

And now the error reads:

 

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Inetpub\WebSites\premierathome_com\products\FrenchTalkingDictionary.php:1) in C:\Inetpub\WebSites\premierathome_com\products\FrenchTalkingDictionary.php on line 3

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Inetpub\WebSites\premierathome_com\products\FrenchTalkingDictionary.php:1) in C:\Inetpub\WebSites\premierathome_com\products\FrenchTalkingDictionary.php on line 3

 

I had included the session start call on that page because every page on the site needed it and the db connection string.

Link to comment
Share on other sites

Then..............put session_start() at the beginning of the page that is calling include and not in the included file.

 

Also..........session_start() must be the first thing at the top of the page.  No other output such as html, error messages or anything else can precede it like <head>, or <title> etc....

With out seeing the entire page that you say you have started the session_start() on - it is not possible for us to trouble shoot.  Same with the dbinfo.pphp.

 

Link to comment
Share on other sites

As meltingpoint said, you cannot have any output before the session_start() call, even white-space. I'd check the problematic file for a line break or single space that could be triggering the error.

 

Though I disagree about your other point meltingpoint, placing the session_start() call within the included file is the best idea. Since you're going to be using it on virtually every page it makes sense not to repeat it every time.

Link to comment
Share on other sites

Mr. Adam-

 

Consider a code where session_start() is at the begining of a page and then runs some code that produces some out put and continues along and also then includes() a file that has session_start() in it also.  Won't that generate an error as technically- there was output to the browser or white space etc...

 

 

Link to comment
Share on other sites

I've eliminated the included dbinfo.php for troubleshooting, it only had the db variables and session start call anyway. Here's my page up until the html begins:

 

<?php
session_start();
//DB Info
$LoggedIn = "No";
if(isset($_SESSION['OK'])){
$LoggedIn = "Yes";
}
//Mysql Queries
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

 

session_start() Is the first thing that happens, no output before it. I've made sure there's no spaces before it. The only other php code on the page is displaying the information gathered from the mysql queries.

Link to comment
Share on other sites

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Inetpub\WebSites\premierathome_com\products\FrenchTalkingDictionary.php:1) in C:\Inetpub\WebSites\premierathome_com\products\FrenchTalkingDictionary.php on line 2

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Inetpub\WebSites\premierathome_com\products\FrenchTalkingDictionary.php:1) in C:\Inetpub\WebSites\premierathome_com\products\FrenchTalkingDictionary.php on line 2

Link to comment
Share on other sites

This is what I get looking at the source code:

 

<br />

<b>Warning</b>:  session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cookie - headers already sent by (output started at C:\Inetpub\WebSites\premierathome_com\products\FrenchTalkingDictionary.php:1) in <b>C:\Inetpub\WebSites\premierathome_com\products\FrenchTalkingDictionary.php</b> on line <b>2</b><br />

<br />

<b>Warning</b>:  session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at C:\Inetpub\WebSites\premierathome_com\products\FrenchTalkingDictionary.php:1) in <b>C:\Inetpub\WebSites\premierathome_com\products\FrenchTalkingDictionary.php</b> on line <b>2</b><br />

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

Link to comment
Share on other sites

Mr. Adam

 

I agree.  My point is that if he includes a global file that has session_start() at the top of every page, or if he simply puts session_start() at the top of each page.............then in any include() file that he adds to that page........he would not put session_start() at the top of that included page.  Using his included file- dbinfo.php as an example which he stated he

use session_start() again.

Link to comment
Share on other sites

I never had two session start calls, sorry if I implied that somewhere. I was originally calling the session start within dbinfo.php and not on the calling page, but changed that during troubleshooting to remove dbinfo.php and call session start on the problematic page.

Link to comment
Share on other sites

I changed the code to

 

<?php
//include_once("../members/dbinfo.php");
session_start();
//DBINFO HERE

 

Rheves only added session_start() within the file in an effort to debug the code; he commented out the dbinfo.php file.

 

Okay, the error says there is a problem on line 1 of FrenchTalkingDictionary.php, what is line 1?

Link to comment
Share on other sites

Also Standards of Websites

 

<html>

<head>

//Title  Meta ect

</head>

<body>

The website Stuff

</body>

</html>

 

You have in your code

Half way through the Body

  <HTML> 
              <!--  		@page { margin: 0.79in }  		P { margin-bottom: 0.08in }  	--> 
              <BODY DIR="LTR"> 
            </p> 
           

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.