使用php函数 "header" 发送http标头
在PHP中,我们经常需要通过发送HTTP标头来与浏览器和服务器进行通信。在这方面,PHP提供了一个非常有用的函数header(),它允许我们发送不同类型的标头,如重定向、设置Cookie、设置缓存等。本文将向您展示如何使用PHP函数header()来发送HTTP标头。
<?php
header("HTTP/1.1 200 OK");
?><?php
header("Location: http://www.example.com");
?><?php
// 设置缓存时间为10分钟
$cache_time = 600;
header("Cache-Control: public, max-age={$cache_time}");
?><?php
// 设置一个名为 "username" 的Cookie
$username = "John Doe";
header("Set-Cookie: username={$username}; expires=Thu, 31 Dec 2022 23:59:59 GMT");
?><?php
header("HTTP/1.1 403 Forbidden");
?>总结:
本文向您展示了如何使用PHP函数header()来发送HTTP标头。您可以使用header()函数发送不同的标头类型,如重定向、设置Cookie、设置缓存等。通过灵活使用这些标头,我们可以更好地与浏览器和服务器进行通信并改善用户体验。
以上是关于使用PHP函数header()发送HTTP标头的简短介绍和代码示例。希望对您有所帮助!
以上就是使用PHP函数 "header" 发送HTTP标头的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号