首页 > 编程知识 正文

headerphp的简单介绍

时间:2023-12-27 22:28:05 阅读:327270 作者:JSNJ

本文目录一览:

Php header()函数语法及使用代码

语法:

复制代码

代码如下:

Void

header(string

$string[,bool

$replace=true

[,

int

$http_response_code)

向客户端发送原始的HTTP报头

需注意:

Header函数必须在任何实际的输出前调用,无论是一般的html标签、文件中空行,或者来自php。就是在这个函数之前不能有任何形式的输出。

参数说明:

参数 描述

string 必需。规定要发送的报头字符串。

replace 可选。指示该报头是否替换之前的报头,或添加第二个报头。

默认是

true(替换)。false(允许相同类型的多个报头)。

http_response_code 可选。把

HTTP

响应代码强制为指定的值。(PHP

4

以及更高版本可用)

常见用法实例:

一、发送服务器状态码

复制代码

代码如下:

header('HTTP/1.0

404

Not

Found');

常用状态码:

状态码 说明

100-199 表示成功接收请求,要求客户端继续提交下一次请求才能完成整个处理过程

200-299 成功接收请求,并已完成整个处理过程,常用200

300-399 未完成请求,客户端需要进一步细化请求,比如,请求的资源已经移动到一个新的地址,常用302,304

400-499 客户端请求有误

常用404

500-599 服务器端出现错误,常用500

详细见:HTTPFTP相应提示信息

二、页面跳转

复制代码

代码如下:

//

立即跳转

header('Location:

');

//

3秒后跳转

header('refresh:3;url=');

3.设置浏览器缓存

强制浏览器不进行缓存!

[code]

//header('Expires:-1');

header('Expires:

Mon,

26

Jul

1997

05:00:00

GMT');

header('Cache-Control:no-cache,must-revalidate');

header('pragma:no-cache');

header('Last-Modified:

'.

date('D,

j

M

Y

H:i:s

T')

);

php中的header是什么意思

header() 函数向客户端发送原始的 HTTP 报头。

语法

header(string,replace,http_response_code)

参数描述

string 必需。规定要发送的报头字符串。

replace 可选。指示该报头是否替换之前的报头,或添加第二个报头。

默认是 true(替换)。false(允许相同类型的多个报头)。

http_response_code 可选。把 HTTP 响应代码强制为指定的值。(PHP 4 以及更高版本可用)

用于301重定向、网页跳转、和控制网页缓存等

php中的header函数

header("Location:page.php");

是为了跳转到其它页面。当然是运行到它时才跳过去。

编译时还是运行时执行?这个问题问在include 与 require合适。。。。

注意区分它与 include 与 require区别

另外补充回答:

==================

页面跳转并不能用来阻止程序中其他语句的执行?

页面已经跳转了,以前的页面header语句后的逻辑代码是会继续执行的,但是输出语句是不会执行的。

你可以拿个简单的例子做实验:

-----------------------------

文件 a.php

?php

header('Location:b.php');

echo '我是跳转前的页面a.php!';

?

文件 b.php

?php

echo '我是跳转后的页面b.php!';

?

运行a.php,发现输出的是 "我是跳转后的页面b.php"

如果有二个header以上的header 则以最后一个为准。

同样上面的例子:

-----------------------------

文件 a.php

?php

header('Location:b.php');

header('Location:c.php');

echo '我是跳转前的页面a.php!';

?

运行a.php,会跳到c.php

再来个例子证明,逻辑代码是会执行的。

--------

?php

header('Location:b.php');

$a = 'c.php';

header('Location:'.$a);

echo '我是aa.php';

?

结果跳到了c.php,证明$a='c.php'执行了。

---------------------

总结:

header后,想禁止后面语句执行,你可以加上exit; 或return;

如下

?php

header('Location:b.php');

return;

//exit;

$a = 'c.php';

header('Location:'.$a);

echo '我是a.php';

?

则只会跳到b.php页面,不会到c.php.

---------------------

这个答案,你满意不?:)

php的header函数设置在哪

//ok

header(‘HTTP/1.1 200 OK');

//设置一个404头:

header(‘HTTP/1.1 404 Not Found');

//设置地址被永久的重定向

header(‘HTTP/1.1 301 Moved Permanently');

//转到一个新地址

header(‘Location: ‘);

//文件延迟转向:

header(‘Refresh: 10; url=‘);

print ‘You will be redirected in 10 seconds';

//当然,也可以使用html语法实现

// meta http-equiv=”refresh” content=”10; /

// override X-Powered-By: PHP:

header(‘X-Powered-By: PHP/4.4.0′);

header(‘X-Powered-By: Brain/0.6b');

//文档语言

header(‘Content-language: en');

//告诉浏览器最后一次修改时间

$time = time() – 60; // or filemtime($fn), etc

header(‘Last-Modified: ‘.gmdate(‘D, d M Y H:i:s', $time).' GMT');

//告诉浏览器文档内容没有发生改变

header(‘HTTP/1.1 304 Not Modified');

//设置内容长度

header(‘Content-Length: 1234′);

//设置为一个下载类型

header(‘Content-Type: application/octet-stream');

header(‘Content-Disposition: attachment; filename=”example.zip”‘);

header(‘Content-Transfer-Encoding: binary');

// load the file to send:

readfile(‘example.zip');

// 对当前文档禁用缓存

header(‘Cache-Control: no-cache, no-store, max-age=0, must-revalidate');

header(‘Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past

header(‘Pragma: no-cache');

//设置内容类型:

header(‘Content-Type: text/html; charset=iso-8859-1′);

header(‘Content-Type: text/html; charset=utf-8′);

header(‘Content-Type: text/plain'); //纯文本格式

header(‘Content-Type: image/jpeg'); //JPG***

header(‘Content-Type: application/zip'); // ZIP文件

header(‘Content-Type: application/pdf'); // PDF文件

header(‘Content-Type: audio/mpeg'); // 音频文件

header(‘Content-Type: application/x-shockw**e-flash'); //Flash动画

//显示登陆对话框

header(‘HTTP/1.1 401 Unauthorized');

header(‘WWW-Authenticate: Basic realm=”Top Secret”‘);

print ‘Text that will be displayed if the user hits cancel or ‘;

print ‘enters wrong login data';

php header 怎么设置

PHP设置header示例:

?php

//设置页面编码

header("Content-type:/text/html;charset=utf-8;");

//页面重定向跳转示例

header("location:index.php");

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。