您好,欢迎访问一九零五行业门户网

关于header,headers_sent,headers_list,header_remove 使用说明

1.headervoid 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_sentbool 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_listarray 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_removevoid 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());?>
本文讲解了header,headers_sent,headers_list,header_remove 使用说明,更多相关内容请关注。
相关推荐:
通过pdo 查询mysql返回字段整型变为string型的解决方法
通过php根据地理坐标来获取国家、省份、城市,及周边数据类
如何使用glob方法遍历文件夹下所有文件的相关方法
以上就是关于header,headers_sent,headers_list,header_remove 使用说明的详细内容。
其它类似信息

推荐信息