PHP语言解释

2025-03-11 07:13:22
推荐回答(1个)
回答1:

Header函数的功能是输出 HTTP 协议的标头到浏览器。

//设置地址被永久的重定向
header('HTTP/1.1 301 Moved Permanently');

//转到一个新网址,这个网址由变量$siteurl和$request_uri动态生成
header('Location: '.$siteurl.$request_uri);

还有很多其他的header,具体请查阅html手册,以下是一些相对常用的:

范例一: 本例用来重导用户到 PHP 的官方网站。
Header("Location: http://www.php.net");
exit;
?>

范例二: 欲让用户每次都能得到最新的资料,而不是 Proxy 或 cache 中的资料,可以使用下列的标头
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");

范例三: 让用户的浏览器出现找不到文件的信息。
header("Status: 404 Not Found");
?>

范例四: bill@evil.inetarena.com (28-Apr-1999) 提供让用户下载文件的范例。
header("Content-type: application/x-gzip");
header("Content-Disposition: attachment; filename=some-file.tar.gz");
header("Content-Description: PHP3 Generated Data");