PHP session_start function and CLI

ε祈祈猫儿з 提交于 2019-12-11 07:11:08

问题


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();
//$entries = array();
//$client_address = $_SERVER["REMOTE_ADDR"] . ':' . $_SERVER["REMOTE_PORT"];
//$client_entry = time();
//array_push( $entries, $client_entry );
$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)));
    }
);

回答1:


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.




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



来源:https://stackoverflow.com/questions/29032312/php-session-start-function-and-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!