1

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)));
    }
);
7
  • 1
    session_start() needs to be before everything else you do.. But not in the CLI .. Why you trying to use it in the console exactly ? There might be a better approach Commented Mar 13, 2015 at 12:31
  • 2
    Sessions are a web SAPI concept, and simply don't exist in CLI SAPI, so don't use session start when running from the command line Commented Mar 13, 2015 at 12:32
  • @MarkBaker Still, the error message seems out of touch then. Commented Mar 13, 2015 at 12:33
  • Please show your code Commented Mar 13, 2015 at 12:33
  • It is before everything else dooh i get the wearning. I use it in console because i want to test this with a client-side url transfer library and see the result more clearly for debugging purpose Commented Mar 13, 2015 at 12:35

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

yes now the warning is gone, but steel the session doesn't work
acording to php.net/manual/en/function.session-status.php this function, i get the PHP_SESSION_NONE (if sessions are enabled, but none exists.)
Yes, it is still not working, because it makes no sense to use Session in CLI. Sessions rely on tokens that make the client recognizable. In most cases this is done by cookies, but your CLI won't handle cookies. So everytime your CLI is seen as someone new. You could TRY to use have the CLI store the SID (session id) somewhere, and use that same SID next time as a parameter when calling the script via CLI again.
Yes I know, is no sense to use Session in CLI, so I think to improvise a session cookie system to read/write the SID from/into a file is the only option out here. Then I wonder if setcookie() will do this job? What do you think?
i am working on a websocket server in php, and for me it makes perfectly sense to have access to session variables...
|
2

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

3 Comments

in my php.ini default_charset is "UTF-8" what can I do? to use json with a text file?
Notepad ++, it was a white space before <?php now the warning seems to be gone, but steel the $_SESSION doesn't work
Using sessions in CLI does not make sense: PHP sessions save data over different pages / scripts by setting a cookie in the user's browser, and CLI ignores cookies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.