How is the php header function used?

The header() function in PHP is used to send raw HTTP header information to the client. This function must be called before any other output in the page, or it will result in an error.

The header() function is commonly used for redirecting pages, setting cookies, and controlling caching. For example:

// 重定向到另一个页面
header("Location: http://www.example.com");

// 设置cookie
header("Set-Cookie: username=johndoe");

// 设置缓存控制
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");

It is important to note that the header() function must be called before any page output, otherwise an error will occur. Additionally, once the header() function has sent HTTP header information, no further output can be sent, otherwise it will result in an error.

bannerAds