PHP header()的用法有哪些

PHP
927
2023/9/13 23:07:01
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

PHP中的header()函数用于发送HTTP标头。

以下是header()函数的一些常见用法:

  1. 设置页面重定向:
header("Location: http://www.example.com");
exit;
  1. 设置页面字符编码:
header("Content-Type: text/html; charset=UTF-8");
  1. 设置缓存控制:
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
  1. 设置文件下载:
header("Content-Disposition: attachment; filename=\"example.txt\"");
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
  1. 设置页面刷新:
header("Refresh: 5; url=http://www.example.com");

这些只是header()函数的一些常见用法,实际上还有许多其他用法。根据具体需求,你可以使用不同的HTTP标头来控制页面的行为。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: php array_merge_deep能保留键名吗