session_start() error - php

I can't handle this error, please help me. It worked on my laptop but did not work on my desktop.
Why?
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at F:\xampp\htdocs\etest\index.php:1) in F:\xampp\htdocs\etest\common\header.php on line 3
The code:
<?php
ob_start();
session_start();
include("constants.php");
include("includes.php");
?>
Thanks for your kindness!

the error was at F:\xampp\htdocs\etest\common\header.php on line 3 but output was already started at F:\xampp\htdocs\etest\index.php:1
I assume you posted the header.php, but your index.php either has whitespace before the
<?php include() or outputs something other than a header on the first line.
the error is caused by line one of index.php, whatever is there. I'd guess whitespace.

In general, when you see "headers already sent", it means that you are trying to do something that requires an HTTP header to be set, but you have already sent content to the client. When using HTTP, you have to send the headers, then the content comes after. If you've sent content, you've missed your opportunity to send headers.

Start the session on top of the file.
direct afer the php tag when you make an echo or send some header before you get this errormessage.
<?php
session_start();
ob_start();
include("constants.php");
include("includes.php");
?>

Enable output buffering by configuration. This way it will start before anything else, so it certainly solves your problem. Although you should look at your code and find the cause of the present error, because it seems you do not fully understand your site yet.
After enabling output buffering by configuration, you no longer need to call ob_start() manually.
Create a file called .htaccess with this content:
php_flag output_buffering on
... or add this line to your php.ini:
output_buffering = On

use_only_cookies=0 to use_only_cookies=1 in php.ini file
I encountered the same problem in xampp 7.0.1 and this fixed it. Read it here.

Yes start the session on top of the file amd make sure you do not have a space before
<?php
Plus you can call session_start(); only one time

session_start() must be called before there is ANY other output. The error is telling you that output started on index.php line 1. The call to session_start() must be placed before that.

Put this code inside includes.php
ob_start();
session_start();
And remove the same code from every other page.
Also make sure that in your files there is no whitespace before the initial
(specifically you are getting the error because something is wrong on the first line of index.php not of header.php)

I was getting this even when I had it at the top of the file.
Problem INVISIBLE UTF8 CHARACTER
Solution
Was to delete the file and make a new one starting with <?php and then copy everything after the php into the new one. (Don't copy the beginning of the file).

Related

session cache error only on two pages - All other pages are working fine

I developed a website from a developer. My website was working fine. All of a sudden I discovered that whenever I click on Contact Us link, I get an error
"Warning: session_start() Cannot send session cache limiter - heards
already sent (output started at /home/xxx/public_html/contact.php:1)
in /home/xxx/public_html/connection/config.php on line 2"
This was not happenning before. The developer is not giving me support and is ignoring my emails. So I searched and researched for it a lot on the Internet and found that there is something wrong in the way session_start() is defined.
Here is the code of contact.php
<?php
include("connection/config.php");
session_start();
?>
<?php
if(isset($_POST['enquiry2']))
{
.
.
.
}
?>
<!DOCTYPE html PUBLIC
.
.
.
<?php include "footer.php"; ?>
</html>
Code of config.php
<?php
session_start();
error_reporting(0);
if ($_SERVER['HTTP_HOST'] == 'localhost' || $_SERVER['HTTP_HOST'] == '192.168.1.111')
{
$con=
.
.
}
else
{
$con=
.
.
}
error_reporting(E_NOTICE ^ E_WARNING ^ E_ALL);
require_once("logic/class.config.php");
.
.
<?php date_default_timezone_set("Asia/Kolkata");?>
In lot of articles I read that session_start() should be right at the top of the page so I reversed the second and third line of contact.php but it gave me same error pointing to contact.php file.
"Warning: session_start() Cannot send session cache limiter - heards
already sent (output started at /home/xxx/public_html/contact.php:1)
in /home/xxx/public_html/connection/contact.php on line 2"
Earlier it was pointing to config.php fle
Same thing is happening in registration page. I have three files register.php, register1.php and register2.php. The same error occurs only on register3.php when it loads the page and once again when submit button is clicked.
I request if someone can help me identify the problem.
Thanks.
EDIT
As suggested by Premalatha:
Removed session_start(); from contact.php -> but same warning.
Placed session_start(); on top of include("connection/config.php"); -> but same warning.
As suggested by Alok:
I dont know how to check if I am sending content or calling header somewhere before session_start(). The session_start() line is either on top or in the second line with first line being include("connection/config.php"); that I tried interchanging upside down with no help.
One thing I fail to understand that other .php files also have the same coding then why this warning comes only on contact.php? Similarly, register1.php, register2.php also have same coding but why the warning comes only while loading and unloading register3.php. All was working before, this started all of a sudden and I dont remember doing any changes in any of these files so the issue may be something else. I also wanted to try suggestion from wangpeng but I could not find the php.ini file anywhere in cpanel. Moreover, I did not figure out how to check if the file's code if it is UTF8-BOM. Also where exactly should i put OB_START() because i am unable to identify where is the header.
EDIT2
By the way, this warning does not affect the functioning of the site. The error just appears and then disappears. However, it disturbs the css, all fonts and images become bigger and the look of the page goes for a toss. Can someone tell me how to ignore this error so that the system doesn't show it at all?
session_start();
is already included in config.php file.
You can remove in contact.php file and it would start working without any issues.
If you still face any issues then you can place session_start(); line of code in contact.php above the include statement.
1.php.ini session.auto_start = 0 to session.auto_start = 1 .
2.check the file's code is UTF8-BOM? yes,UTF8-BOM to UTF8.
3.OB_START();before header
Headers already sent means PHP script sent the headers to the server already, and it cannot be modified now.
Which means before session_start() your script must be sending content or calling header to the server and and after sending it script is calling session_start() function which tries to modify the headers.
Solution in your case is to check you're not sending content or calling header some where before session_start() in both the scripts.
Update:
Try this,
Locate all the *.php files pointed in contact.php and find session_start(); in it.
Replace it with the following,
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
Above code will make sure that you don't start the session again if it is already being started.
I never thought the solution would be this !
However, when I read same thing from four different people, I had to try this and it worked ! I had few leading, trailing and blank spaces in my include files. I got rid of them and my problem was solved.
Here is the article that helped me a lot
Troubleshooting "Warning: session_start(): Cannot send session cache limiter - headers already sent"
if you have blank space after php tag (or html or any output) in your include files then this error will come. -by Imdad
The reason for getting this issue is 99% likely to be trailing spaces at the end of your include files (yes - I know it sounds unlikely but just try it). Remove the trailing spaces, newlines etc... and all will be well. -by Chris B
a white-space before <?php also send for output and arise error. Conclusion: Do not output any character before calling session_start() or header() functions not even a white-space or new-line -by Biswadeep Sarkar
For others who may run across this - it can also occur if someone carelessly leaves trailing spaces from a php include file. Example:
<?php
require_once('mylib.php');
session_start();
?>
In the case above, if the mylib.php has blank spaces after its closing ?> tag, this will cause an error. This obviously can get annoying if you've included/required many files. Luckily the error tells you which file is offending. -by Ross

"session_start(): Cannot send session cache limiter" warning, using ajax

I am having this error in PHP:
session_start(): Cannot send session cache limiter - headers already
sent
I know I have to put session_start() even before the html <head>, what I am doing. The issue here is that I am using id containers for ajax generated content.
For example, I have a button in the homepage.php which updates some calculate.php file in a divResults, and this calculate.php begins with session_start(). However, this warning is appearing inside divResults.
How could I solve it. Is it possible to simply ignore the warning. How?
(I don't have this problem in XAMPP, only using an external hosting provider)
Thanks
Based on your problem following errors may possible.
you write session_start(); in somewhere middle or end of the .php file so you need to put session_start();at top of .php file just after <?php.
May be some empty lines are there in your .php file at the beginning or end, because of that also this error will arise. remove those empty line.

Alternative to <?php ob_start(); ?> ..<?php ob_flush(); ?>

Is there any alternative available to
<?php ob_start(); ?>
Codes go here..........
<?php ob_flush(); ?>
for eliminating Warning: Cannot modify header information - headers already sent by (output started at............
I'm looking for an alternative .htaccess rewrite rule to the above PHP functions.
Thanks,
You will be unable to fix that error by editing your htaccess file unless you hide the error which won't fix your actual issue because the header change that you attempted will fail.
Instead, you need to ensure that you modify the headers before you output any html, text or white space. This error can often be caused by:
white space before your opening php tag
outputting some html (such as the docytpe and head) then attempting to change the location header to redirect to another page
The true "alternative" (which really isn't an alternative, but is instead the right way) is structuring your code so that no header is set after the output has started.
There is really no case when this is not possible, and output buffering is not meant to be used as a workaround for this.
An alternative and simple resolution to this problem is to enable output_buffering variable on the PHP server which can be easily done by saving the following code in php.ini file.
output_buffering = On

possible to use session_start() inside included file?

I have 2 pages one is index.php other is classes.php
should not this work classes.php:
<?php
ob_start();
session_start();
?>
index.php:
<?php
include "classes.php";
error_reporting(E_ALL);
echo "Loaded";
?>
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /public_html/index.php:1) in /public_html/includes/classes.php on line 2
Ensure there is no white space before the opening <?php tag in your classes and index files.
Make sure you do not send anything before anything regarding session is executed.
That means:
remove everything before <?php,
remove trailing ?> tags (they are not required and often harmful),
The problem you experienced is caused by something that is before "<?php" within index.php file (it may be a space, tab, end of line or anything else you did not show us), but if you follow the above rules, you should have this issue a lot less often during further development of your application.
It's not an include issue - you have whitespace somewhere, possibly in classes.php after the ?>.
output started at /public_html/index.php:1
Apparently you have white-space (or possibly content) before the PHP start tag in your index.php file which is being sent as output.
check your php.ini and see if you have the session.auto_start set to 1(or true, or yes).
If you want to start the session manually, it should be set to 0
More info here: http://www.php.net/manual/en/session.configuration.php#ini.session.auto-start
if it is already set to 0, then you have a space or a new line at the beginning of index.php, try to make sure that you do not see any weird char

PHP session_start() fails to create session

These are my first 5 lines in my index.php:
<?php
session_start();
if (!isset($_SESSION['cart'])) $_SESSION['cart']='';
$page = $_GET['page'];
?>
and so on..
I'm looking at the sessions trough the firefox->firebug->firecookie plugin and a session is not created.(I am sure of it because the cart that worked yesterday doesnt work today.)
Any ideas why this might happen and how to fix it ?
I found this when enabling errors:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/controll/public_html/metalkonsult.com/index.php:1) in /home/controll/public_html/metalkonsult.com/index.php on line 2
I explored further - something IS sent to the browser but I dont know where its coming from. I did a var_dump(headers_list()); on the first line and this is what I get:
array(2) { [0]=> string(23) "X-Powered-By: PHP/5.2.6" [1]=> string(23) "Content-type: text/html" }
How do I turn this off ? What's sending it ?
I set session.auto_start = 1
in php.ini in the website folder
sessions work now..
dont know what caused the problems but problem is temporarily fixed
If the white space I see before your php opening tag (<?php) is in your source code too, then this is the reason :) Make sure that no output has been done before your session_start call.
You can check with firebug the exact output, by going to the net tab, locating your PHP file transfer, expanding it, and seeing the Response tab. You could put a print just after the session_start and see where it appears... it should be the very first thing.
Otherwise, if you have php errors directed to log, go and see the log if there is anything in it. Finally, make sure that the browser is displaying session cookies too: some browsers don't show them.
Something is being sent to the browser first which is causing the headers to be sent. Check your code to make sure that there isn't even a single space before your PHP code.
Some editors add an invisible UTF-8 byte order mark at the beginning of the file. Depending on the various server software versions, it may or may not be sent to the browser. Could you check a hex dump of your source code and make sure the first 5 bytes are 3c 3f 70 68 70?
Works for me. Make sure your server has write access to the folder where session data is kept (check your session path: http://php.net/manual/en/function.session-save-path.php)
<?php
session_start();
$_SESSION['cart'] = "test<br />";
print $_SESSION['cart'];
if (!isset($_SESSION['cart'])) $_SESSION['cart']='';
print $_SESSION['cart'];
//blank it out
if (isset($_SESSION['cart'])) $_SESSION['cart']='';
print $_SESSION['cart'];
?>
outputs
test
test
#Palantir: And the whitespace doesn't matter.
Is that the first PHP page called or is it included from another one?
If it is the first, try enabling output_buffering on php.ini. You may then be able to see what is being sent before by calling ob_get_contents()
I had this problem caused by two different things:
1- there were an empty line at the beginning of the file case the php 5.2 to push 'headers already sent' warning.
http://board.phpbuilder.com/showthread.php?10310794-RESOLVED-Warning-session_start()-Cannot-send-session-cookie-headers-already-sent
if < ?php is at line 2 of your script and a blank line is above it then
that can cause problems such as the php header function not working.
2- PHP output_buffering was turned off by default.
If you're using shared hosting and code works on localhost maybe this can be the reason.
just set these parameter in php.ini or local .user.ini
output_buffering=4096

Categories