PHP header()函数教程

HTTP协议使用 HTTP头 以便向用户浏览器提供信息。HTTP头可以提供诸如内容类型、响应代码、时区信息、缓存等信息。HTTP头是由web服务器创建的,就像PHP提供的动态web编程语言一样 标题() 函数以添加、更改HTTP头。header()函数主要用于发送原始HTTP头。PHP header()函数在 PHP 4.0版 后来呢。原始HTTP头表示明文头信息。

null

HTTP头是如何工作的?

在开始提供PHP头函数的示例之前,我们需要提供HTTP头工作逻辑。HTTP是一个无状态协议,每个请求都是原子的。HTTP请求和响应包含头信息,以便设置一些信息和属性。下面您可以看到一个HTTP响应,第一部分是头。

HTTP/1.x 200 OKTransfer-Encoding: chunkedDate: Sat, 20 Nov 2019 04:36:25 GMTServer: NginxConnection: closeX-Powered-By: W3 Total Cache/0.8Pragma: publicExpires: Sat, 28 Nov 2019 05:36:25 GMTEtag: "asdfasrtv;gz"Cache-Control: max-age=3600, publicContent-Type: text/html; charset=UTF-8Last-Modified: Sat, 20 Nov 2019 03:50:37 GMTContent-Encoding: gzipVary: Accept-Encoding, Cookie, User-Agent <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Wisetut.com Tutorials</title><!-- ... rest of the html ... -->

header()函数语法

PHP提供了 header()函数 以便向HTTP头添加或更改属性。header()函数接受3个参数,其中只有一个参数是必须的,另外两个是可选的。

header ( string $header , bool $replace = TRUE , int $http_response_code )

$标题 参数用于提供要添加的标题内容。此参数将包含完整的HTTP头行,如 服务器:Nginx . $header参数是必须的,并且应该在每次使用header()函数时提供。此参数应为字符串格式。

$替换 参数用于指定是否应将已存在的参数替换为提供的$header参数$replace参数是可选的,如果未提供,则默认值为 真的 这意味着现有的HTTP头将替换为提供的头。

$httpu响应u代码 参数用于提供HTTP响应或响应的状态代码。此参数也是可选的。$httpu responseu代码类型是integer。

header()函数用例

PHP header()函数提供了更改HTTP头的功能。由于HTTP提供了许多不同的头属性,您可以使用header()函数将此头用于不同的用例。下面您可以找到PHP header()函数的一些常用用例,以及“PHP header的用途是什么?”问题的答案。

  • 重定向到新URL
  • 设置内容类型
  • 设置内容长度
  • 设置内容语言
  • 设置HTTP状态/响应代码
  • 设置时区
  • 提供技术和Web服务器信息
  • 禁用缓存
  • HTTP基本认证

使用header()函数重定向到新的URL/位置

header()函数最常用的用例是重定向到新的位置或URL。URL或URI是地址标识符或网页的简单地址,如 https://wisetut.com/ HTTP位置头用于将用户重定向到另一个URL,该URL可以是同一个域,也可以是不同的域。在下面的示例中,我们将用户重定向到 https://www.poftut.com/ 通过使用位置标头。可以用单引号或双引号提供标题字符串。

<?php   header('Location: https://www.poftut.com/');?>

作为特殊情况,Location头还将HTTP响应或状态代码设置为 302重定向 除非先前设置了另一个状态代码,否则将自动执行。请记住,在提供Location头之后,不要放入任何PHP代码,因为它们都不会执行,因为响应只包含重定向。

即使302重定向是流行的重定向用户到新的网址有其他重定向状态码和类型如下。所有这些都设置了重定向的不同原因。即使这些原因对人类也没有意义,它们对机器人、搜索引擎优化、搜索引擎、应用程序和服务都有意义。

<?php   // HTTP Status 301 Moved Permanently   header("Location: /my-new-url.php",TRUE,301);   // HTTP Status 303 See Other   header("Location: /my-new-url.php",TRUE,303);   // HTTP Status 307 Temporary Redirect   header("Location: /my-new-url.php",TRUE,307);?>

如前所述,在提供位置报头之后,不能向用户发送HTTP数据或内容。如果您想提供一些HTTP数据或内容,以便提供“您将在5秒钟内被重定向”之类的信息,您应该使用如下所示的刷新头。刷新头将在指定时间后将网页刷新到指定的URL。

<?php   header('Refresh: 5; url=https://www.wisetut.org/');   print 'You will be automatically redirected to the WiseTut in 5 seconds';?>

使用header()函数设置内容类型

HTTP协议通常提供网页和内容,如HTML代码、JavaScript代码、CSS、图像、字体等,但文件、视频也可以通过HTTP提供。内容类型HTTP报头可以提供不同类型的内容。Content-Type头提供HTTP响应数据类型。例如,在下面的示例中,将提供PDF或可移植文档格式的文件数据作为响应。

<?php   // Set the Content-Type HTTP header as PDF   header('Content-Type: application/pdf');   // Set the file name of the PDF file   header('Content-Disposition: attachment;filename="Wisetut.pdf"');   // Specify the PDf file location in order to read data and return as HTTP response   readfile('Wise.pdf');?>

以下内容类型可用于不同的文件或数据格式。

<?php   // Set the Content-Type HTTP header as Amazon Kindle eBook format   header('Content-Type: application/vnd.amazon.ebook');   // Set the Content-Type HTTP header as Cascading Style Sheets (CSS)   header('Content-Type: text/css');   // Set the Content-Type HTTP header as Microsoft Word   header('Content-Type: application/msword');   // Set the Content-Type HTTP header as Graphics Interchange Format (GIF)   header('Content-Type: image/gif');   // Set the Content-Type HTTP header as HyperText Markup Language (HTML)   header('Content-Type: text/javascript');   // Set the Content-Type HTTP header as JavaScript   header('Content-Type: text/html');   // Set the Content-Type HTTP header as Portable Network Graphics (PNG)   header('Content-Type: image/png');   // Set the Content-Type HTTP header as WEBP image   header('Content-Type: image/webp');?>

使用header()函数设置HTTP状态/响应代码

HTTP状态代码 用于提供HTTP响应状态,如200 Success、404 Not Found等。header()函数可以以不同的方式提供HTTP状态代码。首先,我们可以用下面的有效状态代码设置头字符串。在下面的示例中,我们将返回404notfound。

<?php   header("HTTP/1.0 404 Not Found");?>

另一种方法是使用$httpu responseu code参数提供状态代码。我们还将为HTTP头提供$header参数。

<?php   header("Server: Nginx", TRUE, 404);?>

使用header()函数设置X-Powered-By值

X-Powered-By-HTTP标头 用于提供有关Web服务器、技术、应用程序、编程语言等的信息。它只是服务器端的商标。可以使用PHP header()函数提供X-Powered-By头。

<?php   header("X-Powered-By: PHP/25.0");    header("X-Powered-By: WiseTut");   header("X-Powered-By: ZendServer 8.5.0");   header("X-Powered-By: ASP.NET");   header("X-Powered-By: IIS 8.0");?>

带有header()函数的内容语言

我们生活在一个多语言的世界和网络中。可以用不同的语言创建网页,并且可以通过 内容语言 HTTP响应头。

<?php   // Set Content Language As German   header("Content-Language: de-De");   // Set Content Language As English   header("Content-Language: en-UK");   // Set Content Language As Turkish   header("Content-Language: tr-TR");   // Set Content Language As American English   header("Content-Language: en-US");   // Set Content Language As Spanish   header("Content-Language: sp-SP"); ?>

使用header()函数禁用缓存

PHP是一种动态服务器端编程语言,其中PHP web应用程序和站点通常创建定期变化的动态内容。HTTP协议提供了一种缓存机制,以保留带宽、时间,并提高网站或网页导航的性能。使用HTTP缓存将阻止向用户提供更改,并且可以使用header()函数禁用 缓存控制 HTTP头。在下面的示例中,我们将禁用当前响应的缓存,并将过去的日期设置为过期。我们将不设置缓存,并且必须重新验证缓存控制头的值。

<?php   header("Cache-Control: no-cache, must-revalidate");    header("Expires: Sun, 16 Jan 2020 05:00:00 GMT"); ?>

使用header()函数进行HTTP基本身份验证

HTTP基本认证 提供用户使用HTTP进行身份验证。顾名思义,它是一种基本级别的身份验证,使用用户名和密码。HTTP基本身份验证可以通过 WWW身份验证 标题。PHP header()函数可用于启用HTTP基本身份验证。在下面的示例中,我们将使用NTLM散列启用HTTP基本身份验证。我们还将提供$replace参数作为FALSE参数,以防止重写以前的头值。我们将使用WWW Authenticate两次来启用身份验证,并将哈希设置为NTLM。

<?php   header("WWW-Authenticate: Negotiate");   header("WWW-Authenticate: NTLM", false);?>

PHP无法修改标头错误

通常,HTTP响应头由web服务器软件(如Apache、Nginx、IIS等)生成。使用PHP修改或更改HTTP响应头是一种有用的方法,但可能会产生问题、异常和错误。

PHP无法修改标题信息错误 在使用header()函数时是常见的。此错误的原因是在创建HTML输出之前运行header()函数。关注 标题不正确() 函数用法示例。

<?php   echo "How Are You";   header ('Location: https://wisetut.com/');?>
How Are You! <?phpheader ('Location: https://wisetut.com/');?>

header()函数的正确用法是在使用header()函数之前运行PHP语句。我们可以用PHP连接数据库或创建变量或其他任何东西,PHP将创建HTML输出,然后使用下面的header()函数。

<?php   mysql_query("SELECT * FROM USERS");   $test= "wisetut";   header ('Location: https://wisetut.com/');?>

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享