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

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

Related

Xdebug - how to suppress "Cannot modify header information"?

Closers & dupers, please not that I am asking for a solution which involves configuring Xdebug - no one has offered one of those yet.
Normally, I debug my PHP in Eclipse, but I also allow adding &debug to a URL to do some simple echo and var_dump() to a browser page (for development only).
I know, I know ... debug by printf() ... shudder!
But, it's quick and it works when needed.
My problem is that the generated page is full of
Warning: Cannot modify header information - headers already sent by (output started at ...
with a backtrace.
Is there any way that I can suppress this?
It looks like you are trying to start a session, do a header redirect, set a cookie, etc. after you have outputted your debug information to the browser. And that leads to the warnings you are receiving.
What you could do, is to start output buffering (perhaps conditionally, depending on your debug variable...) at the very top of your script and dump the output at the end of the script or at least after the sections that cause the warnings.

dynamic css file won't load on web, works on xampp

This is my first question and I will try to do a good job.
I use a DSS file for my website in development as I want to "display:none" some divs if certain conditions are met (user is logged in -> no need for a register-form)
So my index.php head-tag for linking the stylesheet looks as follows:
<link rel="stylesheet" href="css/default.php" type="text/css">
the default.php CSS file has the following start:
<?php
session_start();
header('Content-type: text/css');?>
On my local webserver (xampp on OS X) everything works just fine.
But on the online server the css isn't applied. You can visit the broken website here:
hsturnierv2.pixelpioniere.net
It links the correct file, but the style is not applied.
EDIT:
Never underestimate the power of a linebreak.
The php-tag in the CSS file did not start at line 1. Thank you!
Your server is sending error message before the actual css:
http://hsturnierv2.pixelpioniere.net/css/default.php
Disable error displaying, or fix the issue with headers.
I go to your CSS link and get Warning:
Warning: session_start(): Cannot send session cache limiter - headers already sent
It means that your script file send the output for client before session_start().
Make sure there is absolutely no whitespace or another characters before <?php
If that fails, try commenting out the session_start() as one of your includes might already be starting the session, like so: /**session_start(); **/
Check the MIME type the server is returning your css with. https://developer.mozilla.org/en-US/docs/Incorrect_MIME_Type_for_CSS_Files
Note: Even though you wil get it to work the way you want, you should double-check if the reduction in CSS file size makes up for the impossibility to cache the CSS client-side.
Apart from that, the actual problem is that you try to modify header information when the header has already been sent. This puts error message output on top of your css, invalidating it.
<br />
<b>Warning</b>: session_start(): Cannot send session cache limiter - headers already sent (output started at /www/htdocs/w011c30e/hst/public_html/css/default.php:2) in <b>/www/htdocs/w011c30e/hst/public_html/css/default.php</b> on line <b>3</b><br />
<br />
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at /www/htdocs/w011c30e/hst/public_html/css/default.php:2) in <b>/www/htdocs/w011c30e/hst/public_html/css/default.php</b> on line <b>4</b><br />
You've inserted PHP code into your CSS file, that is wrong, please remove it.
<?php
session_start();
header('Content-type: text/css');?>
This is for your PHP file.

Suppress session_start() Warnings (headers already sent)

I have a situation where I need to call session_start() in the middle of the file multiple times. Obviously, as session_start() outputs buffer, I get the following warnings:
Warning: session_start(): Cannot send session cookie - headers already sent by...
Warning: session_start(): Cannot send session cache limiter - headers already sent...
There was a lot of written on StackOverflow about header related warnings caused by session_start(). Now before you decide to mark this post as a duplicate, please continue reading - you will see, it is different.
Most of the proposed solutions in the other threads recommend putting session_start() at the beginning of the file, before any PHP output is made. This can be suitable solution if session_start() needs to be called only once per file - however, it is not my case.
I have a file download script (see the code below.) My script regularly, during the file download, updates '$_SESSION' variables with download progress status. I have then another script regularly issued through AJAX requests, which monitors download progress and informs my JavaScript code.
To avoid locking the $_SESSION variables, I therefore have to call session_start() and session_write_close() multiple times per file download, which occurs in a loop in the PHP file.
As every subsequent call to session_start() returns the warnings, which in my case are injected into the downloaded file, is there any programmatically correct way of suppressing them?
Two solutions come to my mind now, but I am not happy with none of them:
calling #session_start() instead of session_start() - this is not considered as a good practice, as it suppresses all warnings and errors returned by the function.
calling ob_clean() to empty the output buffer at the end of every file write iteration, to remove the warnings injected into the buffer to be written to the file. - in this case, the warnings are still present, I do not correctly manage the situation, I simply erase the warning and ignore it.
So my question, please, how to correctly call session_start() and session_write_close() multiple times per file and avoid getting warnings?
Code Sample:
<?php
//...
// file download in small pieces
while(!feof($file_source))
{
$chunk_data = fread($file_source, $chunk_size);
print($chunk_data);
ob_flush();
flush();
$bytes_already_transferred += strlen($chunk_data);
$percent_complete = round(($bytes_already_transferred / $file_size) * 100);
// update session variable, but how to avoid warnings?
if (session_status() == PHP_SESSION_NONE) session_start();
$_SESSION['DownloadProgress'] = $percent_complete;
session_write_close();
usleep(1000);
}
fclose($file_source);
?>

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.

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