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)));
    }
);
Jsowa
  • 9,104
  • 5
  • 56
  • 60
Robert-Dan
  • 337
  • 2
  • 4
  • 13
  • 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 – Pogrindis Mar 13 '15 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 – Mark Baker Mar 13 '15 at 12:32
  • @MarkBaker Still, the error message seems out of touch then. – nl-x Mar 13 '15 at 12:33
  • Please show your code – nl-x Mar 13 '15 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 – Robert-Dan Mar 13 '15 at 12:35
  • ok done, edited the question @ml-x the code is on the page – Robert-Dan Mar 13 '15 at 12:38
  • @MarkBaker, I have read that sessions are not part of CLI so many times I believed it. Today, I learned otherwise. Sessions function within php CLI. – DanAllen Aug 23 '19 at 00:23

2 Answers2

5

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.

nl-x
  • 11,762
  • 7
  • 33
  • 61
  • yes now the warning is gone, but steel the session doesn't work – Robert-Dan Mar 13 '15 at 13:10
  • acording to http://php.net/manual/en/function.session-status.php this function, i get the `PHP_SESSION_NONE` (if sessions are enabled, but none exists.) – Robert-Dan Mar 13 '15 at 13:13
  • 1
    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. – nl-x Mar 13 '15 at 13:14
  • 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? – Robert-Dan Mar 13 '15 at 13:25
  • The question you have now is handled here: http://stackoverflow.com/questions/7578595/is-it-possible-to-read-cookie-session-value-while-executing-php5-script-through It kind-of is possible, but I wouldn't advocate it. – nl-x Mar 13 '15 at 13:30
  • 2
    i am working on a websocket server in php, and for me it makes perfectly sense to have access to session variables... – nfo Jun 09 '16 at 21:37
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).

Benoit Esnard
  • 2,017
  • 2
  • 24
  • 32