Page 1 of 1

Session_Start(); Error

Posted: Thu Jan 20, 2011 8:20 am
by POINTBLANK0704
Guys,

I'm wondering if I can get some kind of help on these forums. I am creating a Web Application for my company everything is going well except I am getting the following error:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\Home.php:1) in C:\xampp\htdocs\auth.php on line 2

What happens is, if the user logs in successfully with the correct credentials he/she is displayed a welcome screen then redirected to Home.php

I've checked multiple areas on the internet for this error and they all seam to say the same thing (make sure session_start(); has no whitespaces or blank lines), however, I believe that this is not the case and that there may be a problem with my code that I am using, nonetheless, this error is displayed, however, what I want this to do works even if the error is still there. All I want to do is make this error disappear and the code still work. lol

Here is the Code for Home.php

Code: Select all

<?php
require_once('auth.php');
require_once('Setupstyles.css');
?>

<!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">

<head>
<meta http-equiv="Content-Language" content="en-ca" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="Setupstyles.css" rel="stylesheet" type="text/css" />
<title>FORM</title>
</head>

<body>
<form method="post" action="logout.php" style="width: 1443px; height: 107px">
<div style="position: absolute; width: 21%; height: 20px; z-index: 1; left: 70%; top: 17%;" id="layer1" class="style7">
				Welcome <b><i><?php echo ($_SESSION['SESS_FIRST_NAME']); echo (' '); echo ($_SESSION['SESS_LAST_NAME']);?></i></b></div>
<input name="Logout" type="submit" value="Logout" style="z-index: 1; position: absolute; top: 17%; left: 92%" />
</form>
Here is the code for auth.php

Code: Select all

<?php
session_start();

//Check whether the session variable SESS_MEMBER_ID is present or not
	if(!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == '')) {
		header("location:denied.php");
		exit();
	}
?>
Also, please keep in mind that Session_Start(); was used in the welcome screen before being redirected to Home.php.

Any help is much appreciated.

Richard aka POINTBLANK0704

Re: Session_Start(); Error

Posted: Thu Jan 20, 2011 8:53 am
by DanHardy
Not sure if this is what you want but replacing your line:

header("location:denied.php"); in auth.php

with:

echo "<script>document.location.href='denied.php'</script>";

will do the trick

Re: Session_Start(); Error

Posted: Thu Jan 20, 2011 9:05 am
by Peter Kelly
try putting session_start() on Home.php as well before including the files.

Re: Session_Start(); Error

Posted: Thu Jan 20, 2011 10:29 am
by AbraCadaver

Re: Session_Start(); Error

Posted: Thu Jan 20, 2011 10:55 am
by brothaofdes
In your PHP.INI file check the value of the output_buffer variable. If it is not ON (1) then you may need to use the ob_start(); and ob_end_flush(); functions (see PHP reference).

Also, documents encoded in UTF-8 are becoming more and more common. Many programs, however, will add a byte order mark at the very beginning of UTF-8 encoded documents. This appears invisible in the editor and is actually placed before any text you write. This can be a problem when working with PHP scripts. PHP treats the byte order mark as text that needs to be output, and this will happen before it starts executing any PHP code, even if the PHP code appears at the very beginning of the document when viewing it in an editor. Now, the issue here is that you don't get a chance to start output buffering (ob_start()) or call any functions that manipulate the HTTP response headers (session_start(), setcookie(), header() and so on) before the output is started.

All of this can be avoided if you make sure that your favorite editor saves your documents without the byte order mark or, alternatively, by enabling output buffering in php.ini.

Some food for thought.

Re: Session_Start(); Error

Posted: Thu Jan 20, 2011 11:02 am
by AbraCadaver
Yes, my link was to a post about the BOM. Please don't use any workarounds like turning on output buffering or flushing buffers. If coded correctly you don't need any of these.

Re: Session_Start(); Error

Posted: Thu Jan 20, 2011 11:10 am
by brothaofdes
Just passing on info brotha, no need to get uptight. I sort of agree that 'if properly coded... blah blah blah', but this is not always a true statement and you probably know that.

Too many variations of coding cause many variations in ini configurations. This is why it is an option. Also why ob_start() and ob_end_flush() where created. They may indicate a less than desirable code structure in the grand scheme of things, but if they solve a problem and are reliable, then so be it.

Re: Session_Start(); Error

Posted: Thu Jan 20, 2011 12:23 pm
by POINTBLANK0704
brothaofdes wrote:In your PHP.INI file check the value of the output_buffer variable. If it is not ON (1) then you may need to use the ob_start(); and ob_end_flush(); functions (see PHP reference).

Also, documents encoded in UTF-8 are becoming more and more common. Many programs, however, will add a byte order mark at the very beginning of UTF-8 encoded documents. This appears invisible in the editor and is actually placed before any text you write. This can be a problem when working with PHP scripts. PHP treats the byte order mark as text that needs to be output, and this will happen before it starts executing any PHP code, even if the PHP code appears at the very beginning of the document when viewing it in an editor. Now, the issue here is that you don't get a chance to start output buffering (ob_start()) or call any functions that manipulate the HTTP response headers (session_start(), setcookie(), header() and so on) before the output is started.

All of this can be avoided if you make sure that your favorite editor saves your documents without the byte order mark or, alternatively, by enabling output buffering in php.ini.

Some food for thought.
Brothaofdes, your awesome man thanks for the help and everyone that replied. My Output_Buffering was turned Off and when I Enabled it the problem is now fixed.

Thanks again Brothaofdes and everyone who replied to this post.

Cya around again.


Richard aka POINTBLANK0704

Re: Session_Start(); Error

Posted: Thu Jan 20, 2011 12:41 pm
by brothaofdes
Be advised that Abracadaver is correct in that you should try to find out why you need the output_buffering turned on. It may be that you absolutely need it, but it may also cause you issues down the road that are really hard to find.

Try to get it to work without this. Also, check the BOM coding (see previous post), this has caused more people problems and may very well be part of your issue as well. Before you run off all giddy, be sure that this solution is the RIGHT solution.

Re: Session_Start(); Error

Posted: Thu Jan 20, 2011 12:46 pm
by AbraCadaver
brothaofdes, no worries I wasn't getting uptight. My point was just what we see here in this thread. POINTBLANK0704 skipped right over my post and ignored your explanation about the BOM and jumped right to the easy hack instead of fixing the problem.

And no, the output buffering options where not created for this reason. They can be used if you legitimately want to send data and then discard it in order to send headers, but that is not the case here. The poster has done something wrong and needs to fix it.