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
Related
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
I am getting pretty annoyed with an issue with my log in and register scripts that are not working. The funniest thing is that for two weeks ago I used the exact same scripts in another website in another server and it works awesome!
ISSUE
When I try to register or log in I am not redirected to the page I was supposed to be. On register.php the user gets registered (I can see it on the database) but I am not redirected to the login.php, instead the script get's crashed on the message "redirecting you to xxxx.php". Here you can read my scripts.
I took contact with my server (ipage) and asked if something was going wrong with their system. I got 3 different and crossed answers:
1st) I was able to replicate your issue. I did some preliminary troubleshooting and was not able to get to the root cause of the issue.
2nd) They answered me later that they were able to register new users and log in with no problem
3rd) Final answer was that the issue was on my scripts
I am not sure of what is happening here because the scripts are not working (on the opposite to what they say). I wonder if they are lying because they simply are no good or can't find out the cause of the problem. Anyway, the error log says the following:
*"20131014T033118: url.org/folder/login.php
PHP Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /hermes/bosweb26a/b366/ipg.domainname/folder/folder/common.php:1) in /hermes/bosweb26a/b366/ipg.domainname/folder/folder/common.php on line 77 "*
Line 77 is: header('Content-Type: text/html; charset=utf-8');. If I take it out, the error just passes on to another line...
Presuming that I already debugged the scripts and found no error, could this be due to the server's quality? (PHP version f.i.?)
You can put session_start() on the first line ok common.php
You need to make sure session_start() is called before calling header(), or echoing any content. So in login.php, you need to make sure session_start() is either at the top of the page, or you need to move it so it is called much earlier in your script, at least before any header() calls.
Just switch round session_start and header('Content-Type: text/html; charset=utf-8'):
// This initializes a session. Sessions are used to store information about
// a visitor from one web page visit to the next. Unlike a cookie, the information is
// stored on the server-side and cannot be modified by the visitor. However,
// note that in most cases sessions do still use cookies and require the visitor
// to have cookies enabled. For more information about sessions:
// http://us.php.net/manual/en/book.session.php
session_start();
// This tells the web browser that your content is encoded using UTF-8
// and that it should submit content back to you using UTF-8
header('Content-Type: text/html; charset=utf-8');
Have you got a space before <?php as the top of common.php? If you do you need to remove it.
Personally i use ob_start(); after the session_start(); and ob_flush() at the end of the file;
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).
I'm using set_cookie() on a site. After adding some functionality, I'm getting Warning: Cannot modify header information - headers already sent by... error. The line number it references as to where the headers initiated from is the very line where set_cookie() is! And I checked, it's not being called twice.
How can I track down these premature headers? I looked at the source code and didn't see any stray characters or anything before the error message starts ( I'm using xdebug, so the first thing is a , which I thought was me, but is actually the beginning of the xdebug message ). I've grepped my code for extra echo and so forth -- nothing.
Can PHP tell me when and where the headers are starting? Or are they really starting on the set_cookie line, and if so, how have I gotten myself into this situation, and how do I get out?
edit: I'm not sure I can paste the code -- do you want just the set_cookie() line? I thought the headers were coming out before that. And there's a lot going on before that with different classes being instantiated. And also I don't think I can post all the classes and stuff -- you know, proprietary information ;)
Edit: I've gotten rid of all terminating delimiters ( all ?>s) and made sure that the first characters of every included file is <?. This is frustrating! It was working before I did these recent changes :(((
Also, why doesn't headers_sent() work?
if ( headers_sent($filename, $linenum) ) {
echo "headers sent: $filename:$linenum.";
}
set_cookie(...);
gives
headers sent: :0.
You can use headers_sent():
if (!headers_sent($filename, $linenum)) {
set_cookie(/*...*/);
} else {
echo "Headers already sent in $filename on line $linenum\n";
exit;
}
One approach would be to run your script from the command line with STDERR redirected to null.
This will show you everything that is being sent to STDOUT, i.e. everything that the server would send to a client.
Hopefully, once you see this, it will be clear where your headers are coming from.
Besides using headers_sent to detect if the HTTP header has already been sent, you could use the PHP’s output control to buffer the output. That allows you to change the header even if you’ve already done some output.
Here’s some example using the explicit output buffering by calling ob_start:
ob_start();
echo 'foobar';
header('Content-Type: text/plain;charset=utf-8');
But make sure you call ob_start before any output was done. Otherwise the HTTP header probably has already been sent.
Can you paste the code?
If its saying headers already sent by [line where set_cookie is], then the error is further on in the script.
Are you sure that you don't have any include files being called prior to your set_cookie() initialization? Do you have sessions running, because if you do, you could just set the cookie at that same point in your script as you started the session.
If there is even a single white space before your set_cookie() initialization it will cause the headers to not be sent correctly. Maybe try using a different editor and checking your results.
I found it! It was a flush(); that I had left lying around all by itself in one of the pages. Surprisingly, it doesn't correctly bind $filename and $linenumber to headers_sent().
I asked the question "php warning - headers already sent after server move" yesterday and I have made changes since to try and fix the problem but im still not getting it!
I am working on code that has been made by another company! Im not moving the site off their server and putting it on ours but the my problem is that sessions are not working across the pages on the new server!
I am getting the warning:
Warning: session_start() [function.session-start]: Cannot send session
cache limiter - headers already sent (output started at
/home/com10002/public_html/bank/index.php:28) in
/home/com10002/public_html/bank/includes/quickform.php on line 3
In my index.php
include('includes/functions.php');
$activeTab = "navhome";
$sent = false;
$title = (isset($_GET['title']))? mysql_real_escape_string($_GET['title']) : 'Home';
$title = str_replace('-',' ', $title);
if($title != '') {
$sql = "SELECT *
FROM contents
WHERE name LIKE '%$title%'
LIMIT 1";
$result = #mysql_query($sql);
$row = mysql_fetch_assoc($result);
}
//Set page title
$pagetitle = (isset($row['name']) && $title != 'Home')? ucwords($row['name']) : "Bank Charges";
HTML..HEAD..META DATA AND TITLE TAGS..ECT
include('includes/header.php');
<div class="textarea">
<?php include('includes/rightcol.php'); ?>
<div id="contentvideo" style="display:none;"></div>
<h1><?=$row['h1'];?></h1>
<h2><?=$row['h2'];?></h2>
<?=$row['intro'];?>
<?php include('includes/quickform.php');?>
<?=$row['page_content'];?>
</div>
<?php include('includes/subnav.php'); ?>
and in quickform.php
if($_POST) {
session_start();
$error = false;
$captcha = false;
$sent = false;
etc.......
This is their code and currently works fine on the their server!
1) I have tried moving the session_start() to the top of index.php
2) Putting a session on both pages.
3) Removing white space before the session when it was in the index.php
My new version is the site is at www.compensation-claims.org/bank
Because this is not my code im not sure as to why they put the session in the quickform.php
A quick and dirty fix would be to activate output_buffering on your server, by modifying your php.ini file (and restarting Apache), setting something like this :
output_buffering=4096
The problem you have is because HTTP headers can't be sent if even only 1 character has already been sent ; by using that directive, you indicate that nothing should be sent before 4k of data have been buffered.
That's usually enough to buffer some white spaces that are present at the beginning/end of .php files, and are enough to cause that kind of trouble.
Note : this is a quick-and-dirty fix, but it might help you get the application working.
Of course, the best solution would be to remove any kind of output that is sent before calling session_start / or to move the call to session_start to the beginning of your PHP script.
One thing that often helps is to remove the closing ?> tag at the end of .php files, so you don't have any white space after it, as it doesn't exist anymore (yes, that's perfectly valid)
EDIT after having a second look at the given code :
In header.php :
You are doing some output (HTML code, like the div "textarea")
And, only then, you are starting the session with session_start
as some output has already been sent to the browser, the session_start cannot create the cookie, and fails.
So, you really need to move the session_start as high as possible : but it right at the beginning of index.php, for instance ; or, maybe, to get it everywhere, at the top of functions.php...
... But definitly NOT in the middle of some HTML output!
(What I said earlier about output_buffering is probably the reason why this (not good) code worked before)
This should solve the problem, I think...
Good luck, anyway !
Sounds like a UNIX character return is being sent to the browser because of how Windows handles carriage returns differently. One or more characters sent before sending headers will cause this error to occur. The alternative may be that the other server had output buffering enabled by a php.ini directive that was capturing and parsing any output before sending it to the client. This could have been the case for compression reasons. I would suggest comparing the php.ini configariton and seeing if gzip compression was enabled on the Apache server prior to the move.
You can use the ini_set() function in your script to set php.ini configuration values.
ini_set('output_buffering', 4096);