Home > Backend Development > PHP Tutorial > Use PHP function 'header' to send HTTP headers

Use PHP function 'header' to send HTTP headers

WBOY
Release: 2023-07-25 11:38:02
Original
718 people have browsed it

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.

  1. Send basic HTTP headers
    First, let’s look at an example of sending basic HTTP headers. In the following example, we will use the header() function to set a header with a status code of 200, indicating that the request was successful. Please look at the code below:
<?php
header("HTTP/1.1 200 OK");
?>
Copy after login
  1. Redirect Page
    Next, we will look at a common use case - redirect page. By using the header() function, we can redirect the user to another page. The following is a simple example:
<?php
header("Location: http://www.example.com");
?>
Copy after login
  1. Set up cache
    In web development, we often need to use caching to reduce server load and improve user experience. By sending cache-related headers, we can tell the browser to load files from the cache within a certain period of time instead of downloading them from the server. Please see the following example:
<?php
// 设置缓存时间为10分钟
$cache_time = 600;
header("Cache-Control: public, max-age={$cache_time}");
?>
Copy after login
  1. Set Cookie
    Using the header() function, we can also set and send Cookies to the browser. The following is an example of setting cookies:
<?php
// 设置一个名为 "username" 的Cookie
$username = "John Doe";
header("Set-Cookie: username={$username}; expires=Thu, 31 Dec 2022 23:59:59 GMT");
?>
Copy after login
  1. Restrict access permissions
    Sometimes, we need to restrict access permissions to certain pages on the server side. We can use the header() function to send the "HTTP/1.1 403 Forbidden" header to tell the browser that the user does not have permission to access the page. Please see the example below:
<?php
header("HTTP/1.1 403 Forbidden");
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template