PHP session_start function and CLI - php

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

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.

Send SMS containing 'æøå' with AT commands

I'm making a SMS sending function for at project i'm working on. The code works just fine, but
when i send the letters 'æ-ø-å-Æ-Ø-Å' it turns to 'f-x-e-F-X-E'.
How do i change the encoding so that I can send these letters?
This is my code:
<?php
include "php_serial.class.php";
$html = $_POST['msg'];
$serial = new phpSerial;
$serial->deviceSet("/dev/cu.HUAWEIMobile-Modem");
$serial->deviceOpen();
$serial->sendMessage("ATZ\n\r");
// Wait and read from the port
var_dump($serial -> readPort());
$serial->sendMessage("ATE0\n\r");
// Wait and read from the port
var_dump($serial -> readPort());
// To write into
$serial->sendMessage("AT+cmgf=1;+cnmi=2,1,0,1,0\n\r");//
$serial->sendMessage("AT+cmgs=\"+45{$_POST['number']}\"\n\r");
$serial->sendMessage("{$html}\n\r");
$serial->sendMessage(chr(26));
//wait for modem to send message
sleep(3);
$read=$serial->readPort();
$serial->deviceClose();
$read = preg_replace('/\s+/', '', $read);
$read = substr($read, -2);
if($read == "OK") {
header("location: index.php?send=1");
} else {
header("location: index.php?send=2");
}
?>
First of all, you must seriously redo your AT command handling to
Read and parse the every single response line given back from the modem until you get a final result code for every single command line invocation, no exceptions whatsoever. See this answer for more details.
For AT+CMGS specifically you also MUST wait for the "\n\r> " response before sending data, see this answer for more details.
Now to answer your question about æøå turning into fxe, this is a classical stripping of the most significant bit of ISO 8859-1 encoding (which I had almost forgotten about). This is probably caused the default character encoding, but since you always should be explicit and set the character encoding you want to use in any case, investigating that further is of no value. The character encoding used for strings to AT commands is controlled by the AT+CSCS command (see this answer for more details), run AT+CSCS=? to get a list of options.
Based on your information you seem to using ISO 8859-1, so running AT+CSCS="8859-1" will stop zeroing the MSB. You might be satisfied with just that, but I strongly recommend using character encoding UTF-8 instead, it is just so vastly superior to 8859-1.
All of that failing I am quite sure that at least one of the GMS or IRA encodings should supports the æøå characters, but then you have to do some very custom translation, those characters will have binary values very different from what is common in text elsewhere.

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.

How to ignore session warning when include a file in 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.

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