a few words about web development

PHP Error: Cannot send session cache limiter

Another straight to the point solution
If got an error like this one:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at E:\websites\index.php:3) in E:\websites\index.php  on line 5

it means you tried to change headers of the file after sending the content of the file.
Headers can, for example, tell the webbrowser what content is in the file it receives. They can also redirect the browser to another page or site.

If you don't specify any headers- PHP will do this automatically before you send any content- that means before any echo, print_r, print, var_dump and so on.

You can also specify headers by yourself. For example this PHP function will tell the browser your page is in fact an image of type JPEG:

headers('Content-Type: image/jpg');

and this one that it is an HTML page:

headers('Content-Type: text/html');


You can also redirect visitors somewhere else:

headers('Location: http://google.com');


Function session_start tries to set the headers, but it will fail if you already sent the headers.

In this situation you have two solutions-
1) move the call to "session_start" to the top of the script
so instead of this:

<?php
echo 'TEST';
session_start();
?>


or this:

TEST
<?php
session_start();
?>

you will have this:

<?php
session_start();
echo 'TEST';
?>


or this:

<?php
session_start();
?>
TEST

2) enable output buffering. If you do- PHP will first grab all the content generated by your file, then add the headers to it and then send to the browser.
To enable it you need to put this in your php.ini file:

output_buffering = On

or put this in a .htaccess file:

php_value output_buffering On

Comments