Session_Start(); Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
POINTBLANK0704
Forum Newbie
Posts: 2
Joined: Thu Jan 20, 2011 8:00 am

Session_Start(); Error

Post 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
DanHardy
Forum Newbie
Posts: 3
Joined: Thu Mar 25, 2010 9:25 am

Re: Session_Start(); Error

Post 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
Peter Kelly
Forum Contributor
Posts: 143
Joined: Fri Jan 14, 2011 5:33 pm
Location: England
Contact:

Re: Session_Start(); Error

Post by Peter Kelly »

try putting session_start() on Home.php as well before including the files.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Session_Start(); Error

Post by AbraCadaver »

mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
brothaofdes
Forum Newbie
Posts: 21
Joined: Thu Jan 20, 2011 10:05 am

Re: Session_Start(); Error

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Session_Start(); Error

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
brothaofdes
Forum Newbie
Posts: 21
Joined: Thu Jan 20, 2011 10:05 am

Re: Session_Start(); Error

Post 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.
POINTBLANK0704
Forum Newbie
Posts: 2
Joined: Thu Jan 20, 2011 8:00 am

Re: Session_Start(); Error

Post 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
brothaofdes
Forum Newbie
Posts: 21
Joined: Thu Jan 20, 2011 10:05 am

Re: Session_Start(); Error

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Session_Start(); Error

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply