session-start warning - php

on my pages I get the following warning:
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/com10002/public_html/compo/index.php:1) in /home/com10002/public_html/compo/index.php on line 1
My code says:
<?php session_start();
require_once("lib/lib.inc.php");
$form_page = "Compensation-Claims";
?>
The page works fine but I want to turn the warning off so that visitors dont see the warning. I removed the code that says:
error_reporting(E_ALL);
ini_set("display_errors","1");
any ideas?

You have to do your session initialization before you send anything to the browser. So if you print anything before it will give you similar errors.

Putting # before session_start(); should suppress the error, even though it would be better to fix the root cause (which is data being output to the browser before session_start(); is called.

Make sure the php start tag (<? , although you should really be using <?php instead) is the first character in the first line of the file.
Alternatively you can enable output_buffering in php.ini

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

How do I hide "headers already sent" warnings in my PHP script?

I have written the below script (snipped) that includes the WordPress functions, however it appears one of the plugins is trying to start a session when it has already been started:
<?php
define('WP_USE_THEMES', false);
require('../../../wp-blog-header.php');
...
?>
I get the following in my php errors log:
<b>Warning</b>: session_start(): Cannot send session cookie - headers already sent in <b>/home/sp32/public_html/wp-content/plugins/woocommerce-abandon-cart-pro/woocommerce-ac.php</b> on line <b>44</b><br />
<br />
<b>Warning</b>: session_start(): Cannot send session cache limiter - headers already sent in <b>/home/sp32/public_html/wp-content/plugins/woocommerce-abandon-cart-pro/woocommerce-ac.php</b> on line <b>44</b><br />
I'm assuming this is an error in the plugin itself, however I want to prevent these warnings from showing in my log file for my specific script. How would I go about that? Ideally I don't want to hide all warnings, just this one.
Note that is not a duplicate- I am not asking how to fix the issue (because the issue lies in a 3rd party plugin), I am asking how to suppress the warning messages.
There are three options I see here:
Fix the offending code. As you have mentioned, this may not be optimal with 3rd party code.
Ask for a refund, and replace this plugin with something well written; specifically, something that abides by an accepted coding standard such as PSR-2, or pear.
Find where the offending files are included (or offending functions are called and wrap them in calls to change the error reporting level (code below). The downside to this is that your sessions will not work, since the headers have already been sent by the plugin; this prevents a session cookie from being set, making the browser unrecognized by php sessions.
$previousErrorLevel = error_reporting();
error_reporting(\E_ERROR);
//offending code inclusion or call here
error_reporting($previousErrorLevel);

session headers already sent issue

I keep getting the error message below. This problem does not exist on my localhost(xampp). It is when the files are on the server that I get this problem.
Warning: session_start() [function.session-start]: Cannot send
session cookie - headers already sent by (output started at
/home/plosta/public_html/bloom/Connections/bloom.php:1) in
/home/plosta/public_html/bloom/signin.php on line 158
this happens on the server but not on the localhost
<?php
//bloom.php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_bloom = "localhost";
$database_bloom = "datacenter";
$username_bloom = "root";
$password_bloom = "";
$bloom = mysql_pconnect($hostname_bloom, $username_bloom, $password_bloom) or trigger_error(mysql_error(),E_USER_ERROR);
?>
Start your session at top of your page.
<?php
//before that noting
session_start();
// Your code here
?>
seems like you are including some php and be sure that your Connections/bloom.php line:1 has no a line breake, white space, html etc. yeah everyone said move your session_start() to line:1 but i think they ment you should move it to top of your all flow's line:1 not only in the signin.php
ob_start(); ob_flush();
put <?php ob_start(); ?> at top of the file signin.php and <?php ob_flush();?> at bottom of the file sigin.php
you can tried with the below solutions :
Place the line of code <?php ob_start(); ?> at top of you file
check with your signin.php file , session_start() must be at the top of your code. Move it form line 158 to line 1
Please check with your code if you print any variable/array using echo or print , please remove that
if you can paste the whole code of the file , it will be more clear for us to give you proper solution quickly.
Usually, when it says "headers already sent" It means that you already started the "ouput buffering", means that you already started to send text to your user.
If it's unintentional (your are not in your code header setup), check for few things, for exemple :
A line or character before the "< ?php" of one of your file
An echo you forget somewhere in your code.
The error says it all, you have session_start() on line 158, try to add it on top of your file.
Make sure session_start() must be at the top of your code. Move it form line 158 to line 1

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

session_start() error

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).

Categories