PHP session error in some pages [duplicate] - php

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.

Related

Header location issue, how to solve it? [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 web site, with php and SQL using WampServer.
And after a condition or in boucle I use Header location to transfer the user to another page. But I got an error from the server, I think is because I use the header after a code and not in . I deleted all blank spaces.
if($passfinal['contrasena']==$_POST['password'])
{
$_SESSION['logedin']=TRUE;
$_SESSION['userid']=$passfinal['id'];
header('Location: ../index.php');
}
Do you have something to help me?
Thnak you.
I think, here is problem $_POST['password'], because before you make compare, first you must check if(isset($_POST['password'])), blank spaces isn't important in this case and last one: I advise you, that you must write full url in header function, like this: header('location: http://example.com/index.php'), because this is more nice and true way.
Warning: Cannot modify header information - headers already sent by (output started at /home/sirobdco/public_html/login/login.php:11) in /home/sirobdco/public_html/login/includes/loginform/loginform.php on line 37
You already sent headers so PHP cannot send them again!
That is, before your code
header('Location: ../index.php');
You already send headers - blank space in html, an echo in PHP, etc.
Have a read through this:
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.

session_start() throwing error

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

Can i change header redirect in php to javascript redirect with same status in PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Headers already sent by PHP
My site is http://www.seoitc.com, we are using joomla for this site, but have 1 problem when i try auto redirect to anypage that show error: Warning: Cannot modify header information - headers already sent by ().
I tried change redirect to use js but cant use same status(302,303...) same as in php. Please help to fix this problem please.
Thanks!
Any output is being sent before you call your header function. Check for any html code before your header call, even a white space or a blank line will cause that
According to PHP Manual
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.
You can use ob_start() at the top of the page. to prevent error.
but also you can use this code. if any error happens in your page it will redirect you to the $page:
// page address
function redirect($page)
{
header("Location: ".$page);
}
set_error_handler("redirect");
And you can check this out: Headers already sent by PHP

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

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