Use PHP function 'header' to send HTTP headers

WBOY
Release: 2023-07-25 11:38:02
Original
591 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:
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:
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:
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:
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:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!