How to ignore session warning when include a file in php - php

when I am using include() with my wordpress plugin which says an error as follows.
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/www/wordpress3/wp-content/plugins/new/listAllItems.php:10) in /var/www/wordpress3/wp-content/themes/thesis_16/lib/functions/document.php on line 3
listallitems.php's 10 th line says
`
include_once("../../../wp-config.php");
include_once("../../../wp-load.php");
....`
How can I avoid this error.

As explained before, this is due to some text which was already sent to the browser before your session_start() statement. Check if you haven't includes spaces/characters in your script or in one of the scripts your are including.
THis could also be an encoding problem, i.e. your file is encoded in UTF-8 with BOM but your webserver is configured in another charset, so the BOM is treated as extra characters (but this problem has indeed be answered a lot of times).

This is just a quick-fix, not the right solution.
Put an "#" symbol before the command, like this...
#session_start();
That will suppress any warnings or errors. You can do that with any PHP command. But it's not a good idea in general, because you are only putting a band-aid on the issue, and not resolving the root cause, which in your case is that the session_start() is getting called after something has been output to the buffer.

I have just added in my script :
if(!session_start()){
session_start();
}
All warnings gone.

Usually this error appears when there is output before session start function. sometimes even blank space or small character, file encoding problems.

Related

Trapping line of code that emits first character

Suddenly, an application isn't any longer able to output ZIP files. An inspection revealed the cause: The first character of the ZIP is a blank, which breaks the ZIP format spec.
To track down this problem, I enabled CStatementTracer, which prints each line of executed code to a log file. Didn't help. [Remark: declare(ticks=1); doesn't seem to trap each line of executed code]
I then set an output handler like so:
function callback( $buffer ) {
$deb = print_r( debug_backtrace(), TRUE );
file_put_contents( './statementTrager.log', $deb );
return $buffer;
}
ob_start("callback", 1 );
Unfortunately, this handler isn't called at all.
Q: Does a generic / canonical solution exists, which identifies the file / line of PHP-code, which emits the first character.
A solution, that finds the loc whatever other code gets executed.
Remarks:
Not even a single PHP file is closed using ?>
Meanwhile I found the suspicious like of code: A blank in front of a starting
Still, I'd like to get hints regarding a programmatic solution. Preferrably a solution written in pure PHP.
https://linux.die.net/man/1/strace is probably the most reliable tool to find out where the output comes from. Assuming you are on Linux. There must be similar tools for other platforms.
Although it will not give you the line of the php code, you can analyse the context of system calls made before and after the offensive character was sent. Usually it is enough to identify where the problem originates.
It is quite time consuming process though. Should be used as the last resort.

PHP-Firebird (ibase_connect = CHARACTER SET iso-8859-1 is not defined)

im new about firebird. I'm trying to connect between php and firebird. This is the code :
$host='192.168.12.1:D:/DB/ALFABETA.FDB';
$username='john.doe';
$password='123456789';
$database='ALFABETA';
$dbh=ibase_connect($host,$username,$password) or die (ibase_errmsg());
$sth= ibase_query($dbh) or die (ibase_errmsg());
But after i run the code in browser, the warning statement is coming up. Help. What i should do ?
Warning Statement
Warning: ibase_connect(): bad parameters on attach or create database
CHARACTER SET iso-8859-1 is not defined in
/var/www/fortrainingcrud/connect_db.php on line 7 bad parameters on
attach or create database CHARACTER SET iso-8859-1 is not defined
I finally got the answer ! Here it is :
In php.ini, i add extension=php_interbase.dll
Restart the Apache service
The code that i've write before, works well
You aren't setting the connection charset yet you get an error about it. That suggests that PHP is taking the value from somewhere else and the first candidate is the ibase.default_charset directive. You can see its current value with var_dump(ini_get('ibase.default_charset')); or simply by running phpinfo().
You can either change the directive yourself or, even better, specify the encoding at ibase_connect() so your code doesn't break randomly depending on server configuration.
As about iso-8859-1, it seems the appropriate syntax for Firebird is ISO8859_1 (assuming you really want that encoding).

PHP session_start function and CLI

Function session_start used in PHP CLI print the next warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/robi/p/test.php:1) in /home/robi/p/test.php on line 2 why?
I want to log all the client entries in a variable and check this out to see if i get forgery from a specific remote address by comparing the time user last entry and current entry time! Am I doing it wrong?
here is my code:
<?php
session_start();
$client_entry = time();
$_SESSION["entries"][] = $client_entry;
$entries = $_SESSION["entries"];
$check_out = array_filter(
$entries,
function($value) use($client_entry) {
return ($value >= ($client_entry + (1 * 0.6)));
}
);
Your problem is, apart from that it makes no sense to use sessions in CLI, that output has already started prior to session_start();.
As I see in your code, you code begins directly with session_start();, I believe you have some characters before <?php. Make sure <?php is on the very first line of your file (so also no empty lines above it), and that there is nothing (such as a white space) in front of it.
This should fix this problem you are having.
The error comes from the very first line, so you should try to convert your file to utf-8 without BOM.
Quoting Wikipedia's article:
The byte order mark (BOM) is a Unicode character used to signal the endianness (byte order) of a text file or stream.
That character is sent to the output stream, so you can't redefine headers (session_start sets up a cookie in the headers).

CodeIgniter redirect() error

When I test my CodeIgniter redirect code, the following error occurs:
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /var/www/allci/CIone/application/config/autoload.php:1)
Filename: helpers/url_helper.php
Line Number: 544
What is causing this?
Output started at (as the error says) /var/www/allci/CIone/application/config/autoload.php on line 1. This means that either autoload.php is a mixed HTML/PHP file (which it probably shouldn't be) or, more likely, autoload.php is has leading white space before it's opening <?php tag.
The < should be the first byte in the file - you need to make sure there are no spaces, tabs, carriage returns etc at the start of the file. You also need to make sure the file does not have a BOM at the start of it.

php header function produces an error when it shouldnt

Im working with PHP 4.3.11 and when I execute a header always responds with an error like this
Warning: Cannot modify header information - headers already sent by (output started at d:\folder\file.php:1) in d:\folder\file.php on line 2
Warning: Cannot modify header information - headers already sent by (output started at d:\folder\file.php:1) in d:\folder\file.php on line 3
Current PHP version: 4.3.11
the code I used to generate this error was
<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
echo 'Current PHP version: ' . phpversion();
// prints e.g. '2.0' or nothing if the extension isn't enabled
echo phpversion('tidy');
?>
It has no spaces nor newlines before or after the php tags, and the same code in a 5.x version returns just the php version as expected.
Any clue?
Thanks in advance
Edit:
Solved!: I've opened the file with western european encoding and deleted the BOM and it worked. Thanks all for your help!
Do you have a UTF BOM at the start of the file?
Make sure that there are no empty lines and invisible characters (such as the UTF BOM) before the PHP block so that <?php is really the first in the file.
if your file does not contain anything else but php code it is recommended to skip the php closing tag to avoid empty spaces issue at the end of the file
The error you're getting means that your entire script (other files included) are sending some output before the header() function is called.
You should revise all your files and see if any of them is outputting something (with echo() or print()), or if you missed some empty spaces after the last closing tag ?>
it's unlikely, but there's the possibility to define auto_prepend_file in the php.ini (probably .htaccess too).
http://at2.php.net/manual/en/ini.core.php - this acts like a require() at the beginning of every script. you can check this by looking at the phpinfo(); output.
but i too think the problem is the utf-8 BOM.

Categories