簡體   English   中英

codeigniter (CI) 的會話問題

[英]Session issue with codeigniter (CI)

我在使用 codeigniter 進行實時開發時遇到 php 會話錯誤

A PHP Error was encountered

Severity: Warning

Message: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/userts4m/public_html/development/relatioweb/admin/index.php:1)

Filename: Session/Session.php

Line Number: 140

Backtrace:

File: /home/userts4m/public_html/development/relatioweb/application/admin/controllers/User.php
Line: 9
Function: __construct

File: /home/userts4m/public_html/development/relatioweb/admin/index.php
Line: 293
Function: require_once

在 localhost 工作正常。 但是當我在服務器上上傳時,我們正面臨這個問題。

我應該怎么辦 ...

有兩種解決方案可以解決此問題。

有時這個錯誤是由於框架的錯誤配置而發生的,進入文件夾applications/config並打開config.php在文件的開頭寫下這樣的

<?php

  if (!defined('BASEPATH')) exit('No direct script access allowed');
    ob_start();
    /* Remaining Part Of your file
     .........................................
   */

另一種解決方案是在類的構造函數中編寫ob_start() ,如下所示:

<?php

 if (!defined('BASEPATH')) exit('No direct script access allowed');
   class Your_Controller extends CI_Controller {
     public function __construct() {
       parent::__construct();
       ob_start();
     }
   }

問題在這里得到了很好的解釋How to fix "Headers already sent" 錯誤在 PHP 中

為了解決這個問題,我需要實現輸出緩沖的概念(輸出緩沖是一種機制,在這種機制中,我們不是立即向瀏覽器發送響應,而是將其緩沖在某個地方,以便我們可以在整個內容准備好時立即發送它)。 因此,錯誤表明問題發生在 index.php
我做了什么我插入了

ob_start();

在 index.php 的開頭

按照@kunal@dale建議有在一個空間/home/userts4m/public_html/development/relatioweb/admin/index.php

這通常意味着在某個地方你已經有一個session_start()並且第二個會拋出這個錯誤。 去掉多余的。

在這個文件中,在開始 PHP 標記之前可能有空格。

嘗試使用@session_start(); 它將繞過錯誤並啟動會話。 請注意@session_start() 是您的第一行代碼。 不要在它之前執行或回顯任何內容。

檢查控制器文件,如果 php 標記之前有空格,請將其刪除。

檢查您的文件是否有多余的空格或輸入,在我的情況下,我在 php 標記之前在 application/config/autoload.php 中有一個額外的輸入(我使用的是 codeigniter)

 ob_start ([ callable $output_callback = NULL [, int $chunk_size = 0 [, int $flags = 
 PHP_OUTPUT_HANDLER_STDFLAGS ]]] ) : bool

此功能將打開輸出緩沖。 當輸出緩沖處於活動狀態時,腳本不會發送任何輸出(標題除外),而是將輸出存儲在內部緩沖區中。

可以使用 ob_get_contents() 將此內部緩沖區的內容復制到字符串變量中。 要輸出存儲在內部緩沖區中的內容,請使用 ob_end_flush()。 或者, ob_end_clean() 將默默地丟棄緩沖區內容。

我不得不去 php.ini 並從always_populate_raw_post_data = -1行中刪除注釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM