header,headers_sent,headers_list,header_remove 使用說明,headerssent

來源:互聯網
上載者:User

header,headers_sent,headers_list,header_remove 使用說明,headerssent
1.header

void header ( string $string [, bool $replace = true [, int $http_response_code ]] )
功能:發送一個自訂的http報文。

請注意一點,header()必須在任何實際輸出之前調用,不管是普通的html標籤,還是檔案裡面的空行,空格或者是PHP檔案裡的空行,空格。這是一個非常普遍的錯誤,在通過include,require,或者其訪問其他檔案裡面的函數時,如果在header()被調用之前,其中有空格或空行。如果不是調用其他檔案,僅僅是單獨使用一個PHP或者HTML檔案,在header()被調用之前有輸出也會出錯。


參數說明:

string  報文字串

replace 如果為true,表示後面一個相同類型的報文資訊來取代前面一個相似的報文資訊。預設為true,如果設為false,可以強制使相同的報文資訊並存。

http_response_code 強制指定HTTP響應的值。注意,這個參數只有在報文字串(string)不為空白的情況下才有效。


例子:把重定性302強制指定為303

<?phpheader('location:http://www.example.com/', true, 303);?>

2.headers_sent

bool headers_sent ([ string &$file [, int &$line ]] )

功能:檢查 HTTP 標題是否已被發送以及在哪裡被發送。


參數說明:

file 如果設定此參數,會把執行了header輸出的php源檔案名稱存入file變數中

line 如果設定此參數,會把執行了header輸出的php源檔案的程式碼號存入line變數中


例子:

例子:<?phpheader('content-type:text/html;charset=utf-8');echo 'fdipzone<br>';ob_end_flush();if(headers_sent($file, $line)){    echo "header send in $file on line $line";}else{    echo 'not header response';}?>
上例輸出:header send in /home/fdipzone/demo.php on line 5


3.headers_list

array headers_list ( void )

功能:列出所有的header輸出(或準備輸出),返回為數組


例子:輸出header list

<?phpheader('content-type:text/html;charset=utf-8');header('access-control-allow-origin:*');$headers_list = headers_list();print_r($headers_list);?>

輸出:

Array(    [0] => X-Powered-By: PHP/5.4.3    [1] => content-type:text/html;charset=utf-8    [2] => access-control-allow-origin:*)

4.header_remove

void header_remove ([ string $name ] )

功能:移除某個header輸出


參數說明:

name 要移除的header name


例子:判斷是否有access-control-allow-origin:*,如果有則移除

<?phpheader('content-type:text/html;charset=utf-8');header('access-control-allow-origin:*');if(in_array('access-control-allow-origin:*', headers_list())){    header_remove('access-control-allow-origin');}print_r(headers_list());?>


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.