How to use the PHP header() function?

In PHP, the header() function is used to send a raw HTTP header. It is typically used for redirecting pages, setting page encoding, and controlling caching. The header() function must be called before any other output on the page, or it will cause an error.

The instructions for use are as follows:

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

header("Content-Type: text/html; charset=utf-8"); // 设置页面编码为utf-8

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

Note: Before using the header() function, make sure there is no output in the page content, including spaces and line breaks. Otherwise, the header() function will not work properly.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds