session_start() throwing error - php

I am currently getting the following error using php:
Warning: session_start() [function.session-start]:
Cannot send session cache limiter - headers already sent
(output started at /home/paramireze/madisonh3.com/calendar.php:1)
in /home/paramireze/madisonh3.com/includes/common.php on line 5
The first line of every file is include common.php, and the first line of code in common.php is 'if(!isset($_SESSION)) {session_start();}`.
This error only occurs on calendar.php and news.php (you can see the error if you visit http://www.madisonh3.com/calendar.php). All my files are the same, which includes a common.php. After that, I will write the html tag and include the header from there.
I've read other discussions regarding session_start and all say to make sure you do not output any html before session_start. Also, if I am doing something wrong, why is it only happening to two out of my 10 files?

There is something outputting data BEFORE your session_start() command. As the session cookie is set to the HTTP header it must precede any HTML output.
The error Cannot send session cookie - headers already sent by (output started at /home/paramireze/madisonh3.com/calendar.php:1) in /home/paramireze/madisonh3.com/includes/common.php on line 5 indicates something is outputted before.
So look into your code and find what could be echoing data before your session_start().

You should care that your editor does not store the utf-8 BOM header, this header is sometimes stored at the begin of the file with 3 bytes .
The editor will hide them, so if you are not sure if your file contains these characters, you can either use a non interpreting editor (hex editor), or this wonderful online W3C checker. The BOM header is treated as output by PHP, and this can cause nasty Cannot modify header information - headers already sent errors.
Checking your URL shows, that there is indeed such a BOM header. Have a look at the settings of your editor (IDE).

Related

PHP session error in some pages [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I have written <?php session_start(); ?> above everything in all pages. Some pages are rendering fine but I am getting this error in other pages. I have checked and matched each page code and code is fine. If I remove <?php session_start(); ?> then page renders fine but I need to use session.
Cannot send session cache limiter - headers already sent (output started at /home/) in ...
One thing to note is: It runs fine on my local server.
I would bet it's one of two things:
Make sure there is no output, including newlines, in your file before you call session_start. PHP must send HTTP headers first (before the message body, i.e. page content), so trying to send a header after you've already sent page content will give you that error.
You should only call session_start only once per page. If you're using include in a file after you've called session_start, make sure you're not calling session_start again in the included file. This would cause the "headers already sent" error.
you have some output (maybe an whitespace) in one of your included files.
maybe your <?php is not the very first of your file somewhere.
You could also try to use output buffering (http://php.net/manual/es/function.ob-start.php). I'd first check to see whether you are not sending any output by mistake (as #steven suggests), but still, it might be a good idea for you to buffer your outputs.
Since you are including multiple files, and you seem to have a session_start() in multiple files, I bet the error is thrown in the second file.
Check all files for the session_start(), and for whitespace before any of these are called.
This is a very typical BOM header problem. I suspect that your editor stored a UTF-8 BOM header with 3 bytes  at the begin of the file. The editor will hide them, so if you are not sure if your file contains these characters, you can either use a non interpreting editor (hex editor), or this wonderful online W3C checker.

header function [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Headers already sent by PHP
I have a PHP file which I'm using to check username and password. This part is working, but after successful login I would like to use header() to redirect to user panel page. This is the logged error that I'm getting:
[10-Dec-2012 12:25:26] PHP Warning: Cannot modify header information - headers already sent by (output started at /home2/jzperson/public_html/imes/php/login.php:10) in /home2/jzperson/public_html/imes/php/login.php on line 32
This is line 10:
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
And this is line 32:
header("Location: http://imes.jzpersonal.com/userpanel.html");
Any idea why?
You probably have some output echoed out before getting to the line 32 with your header call.
See description of the header function: http://php.net/manual/en/function.header.php
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
Clarifications
To clarify things a little bit, the redirection using header() is performed by including a raw location response-header field to the server response. When the receiving party reads the response and sees that header field, it drops the current response and issues another request to the destination you provided.
Now, headers always come at the top (head) of the server response. That's why they are called headers! If you output any content, PHP will immediately "prefix" it with default headers and it's not possible to add any more of them after this point. So, by attempting to set another header later in your code, you get an error:
Cannot modify header information - headers already sent
By outputting HTML at line 10 you can no longer issue any more headers, because they were already sent (prefixed to your HTML output).
You can find more information about headers here: http://www.faqs.org/rfcs/rfc2616.html
Basically, you need to check whether the user is logged in or not (and redirect) before anything is sent to the browser (before HTML). Your code, then, would look something like this:
<?php
...
if($loggedIn)
{
header("Location: http://imes.jzpersonal.com/userpanel.html");
exit();
}
?>
<html>
...
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
You are trying to write something before header statement
Remove any echo statements/html content before header statement. That should do the trick
You could also cheat and just use output buffering - at the very beginning of the script tree use ob_start(); to begin capturing the output. You can use headers and set cookies etc as much as you like then.
At the last line of the script tree use ob_end_flush(); to send the output. You can also grab it to a variable to further process if you wish with $buffer = ob_get_clean();
Although its not a solution as such it does allow for a more flexible coding environment AND it will solve your above problem.
Its best to flush and die if you are going to be sending a Location header:
ob_start();
/* very long snip */
header('Location: somepage.php');
ob_end_flush();
die();
This will prevent any further processing after the location change has been sent.
Just as a side note: When I speak of a script tree I mean the include path - like put the ob_start(); into a header file thats included before anything else and a footer file that flushes (and processes if required) the output buffer. Remembering, as highlighted above, that Location changes should have the script halted immediately after.
Sessions may also need to be closed with a header Location followed by a die - to use that simply
ob_start();
/* very long snip */
header('Location: somepage.php');
ob_end_flush();
session_write_close();
die();
I found that one out after hours of wondering why session data was being lost! Bear that on mind.
You can't use header(); if anything has already been sent as output. This means HTML. Do all your PHP processing first, then output your HTML/JS.

jQuery .load and PHP session start

I am currently working on a game in PHP and jQuery and at some point I will need to use the .load() from jQuery to load a PHP page into a div. That page will load some player information based on their login information, account id, etcetera, stored into an array inside $_SESSION["arrayname"].
It works perfectly on all sites, except those where jQuery load() is use. On easyphp, I got no errors, but on my web host server im getting this:
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /fr/game/map.php:1) in fr/game/map.php on line 6
Here is my code:
<?php
require_once("../../connectionRO.inc.php");
//used to check if we see the array in session, and i dont when i load it with jquery
if(!ISSET($_SESSION["comptejeu"]))
{
require_once('../../classInfoCompte.php');
session_start();
require_once('../../lg.php');
require_once("./glg.php");
}
$ic = new infoCompte;
$ic = $_SESSION["comptejeu"];
?>
I understand that at this point a lot of stuff is already output to the page before that is loaded. Could anyone point me to a better way to retrieve the information that I need from that array to build my object from my class?
Thank you.
Since you are getting this error as output started at /fr/game/map.php:1 (note: line 1) I will place money on you having whitespace or a BOM before the opening <?php tag in /fr/game/map.php.
Make sure the opening < is the first character in the file. If you file is UTF-8, make sure it is UTF-8 without BOM, or convert it to ASCII.
Wrong:
<?php
Right:
<?php
Either there is some white space in your PHP file, there is some whitespace in classInfoCompute.php or there is a function/method call that triggers output in either this or the classInfoCompute file. Sessions depend on cookies, and cookies depend on HTTP headers. Once content has been output, the headers are already sent and you can't send a new one.
There are plenty of questions and answers on Stack Overflow that cover this issue, but it boils down to either sweeping the problem under the carpet (with output buffering), or fixing it properly (by finding what's causing content to be output before your call to session_start and either removing it or moving it to later in the script, or moving session_start to earlier in the script).
If your script mixes HTML and PHP then make sure that the session_start occurs before the first bit of HTML. Otherwise, you'll have to hunt around for white space. If the file is saved as UTF-8 then your text editor might have attached an unnecessary Byte Order Mark (BOM) to the start of the file. Make sure that your file is being saved without a BOM, because it will be treated as content and trigger output to the client (and put a weird character at the start of the output).

How I can remove this error from my website? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Headers already sent by PHP
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/eitlabs1/public_html/salmanoreen.com/index.php:1) in /home/eitlabs1/public_html/salmanoreen.com/libraries/joomla/session/session.php on line 423
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/eitlabs1/public_html/salmanoreen.com/index.php:1) in /home/eitlabs1/public_html/salmanoreen.com/libraries/joomla/session/session.php on line 423
Warning: Cannot modify header information - headers already sent by (output started at /home/eitlabs1/public_html/salmanoreen.com/index.php:1) in
There are something that can cause this problem when you have session in your codes:
Having a white space before <?php
Having something before session_start() in your HTML codes.
Your PHP file has saved with BOM supporting option. Remove BOM from your file.
It's caused by text written to the web page before the call to session_start(). You can't have any text - even a carriage return - before the session_start() function call.
the error is triggered because you output something before the session_start() call. session_start should always be placed before any output or else it will produce an error like yours.
you are probably doing something like this:
<?php echo "test";
session_start();
?>
it should be
<?php session_start();
echo "test";
?>
There is also a possibility you have edited a file through ftp and a "invisible" character was added at the start of the file (boom operator).
This is not necessarily a session problem. This is usually the result of a different error message that is being sent to the web browser. As soon as any text is to be sent, php sends headers beforehand. After the text is sent, the session cannot be started because this also requires sending headers, but headers have to be sent all in one go before any other output. This means you must find the original error first.

PHP login form problem [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I created a login form on another server and it worked perfectly, now i transfered it to another server and im getting lots of new errors:
Warning: Cannot modify header information - headers already sent by (output started at /home/davethom/public_html/login.php:16) in /home/davethom/public_html/login.php on line 55
but the actual login works this message just appears, its probably just me being stupid and missing something,
www.scottechtrio.co.cc/login.html username: 1 password: 1
You are probably calling the header() function, or another function that sends headers, like setcookie(), after starting sending some output.
Those functions must be called before any output is sent to the browser :
Before any echo / print is done,
Before any character (including white-spaces) outside of <?php ... ?> tags
Check for the whitespaces in your code. Remove the php closing tags (if any) at the end of your php page.
I think you need to turn off the display_errors directive in the php.ini file.
If you are not able to edit php.ini file, call the following function on top of all your php files.
ini_set('display_errors', 'Off');
You can also use .htaccess file in the webroot of your application with the following content.
php_flag display_errors Off
You shouldn't just turn off error reporting; you should fix your code so it doesn't cause any errors.
As stated by Pascal MARTIN, this error occurs when you call a function that sends an HTTP header after you have already sent output to the browser. session_start() sends a cookie header, so this (like header() and setcookie()) needs to be called before you output any content. Check line 55 in /home/davethom/public_html/login.php to find the offending function and make sure no content is sent to the browser before you call it.
Page content is anything sent to the browser to be rendered to the user. This could be your opening tags (or ) or even some errant whitespace, accidentally output somewhere. Look for echo or print statements, or anything not inside a set of delimiters (even just new lines or spaces here will be problematic).
As a debugging aid: to find out precisely what has been sent to the browser when this error occurs, put die(); immediately after line 55, visit the page in your browser, then use the browser to view the source it has received from the server so far.
See http://uk.php.net/manual/en/function.header.php for more information on incorrectly sending headers after content.

Categories