Use PHP function "header" to send HTTP headers
In PHP, we often need to communicate with browsers and servers by sending HTTP headers. In this regard, PHP provides a very useful function header(), which allows us to send different types of headers, such as redirects, setting cookies, setting cache, etc. This article will show you how to use the PHP function header() to send HTTP headers.
<?php header("HTTP/1.1 200 OK"); ?>
<?php header("Location: http://www.example.com"); ?>
<?php // 设置缓存时间为10分钟 $cache_time = 600; header("Cache-Control: public, max-age={$cache_time}"); ?>
<?php // 设置一个名为 "username" 的Cookie $username = "John Doe"; header("Set-Cookie: username={$username}; expires=Thu, 31 Dec 2022 23:59:59 GMT"); ?>
<?php header("HTTP/1.1 403 Forbidden"); ?>
Summary:
This article shows you how to use the PHP function header() to send HTTP headers. You can use the header() function to send different header types such as redirect, set cookies, set cache, etc. By using these headers flexibly, we can better communicate with browsers and servers and improve the user experience.
The above is a short introduction and code example about using the PHP function header() to send HTTP headers. Hope this helps!
The above is the detailed content of Use PHP function 'header' to send HTTP headers. For more information, please follow other related articles on the PHP Chinese website!