PHP函数之header() 实现页面跳转

高洛峰
Libérer: 2023-03-03 16:52:01
original
3005 Les gens l'ont consulté

页面跳转有多个方法。 先介绍PHP中利用自带的函数实现方法,主要用到header()函数。
header()函数的主要功能是将HTTP协议标头(header)输出到浏览器。

语法

header(string,replace,http_response_code)
参数描述string必需。规定要发送的报头字符串。replace可选。指示该报头是否替换之前的报头,或添加第二个报头。默认是 true(替换)。false(允许相同类型的多个报头)。http_response_code可选。把 HTTP 响应代码强制为指定的值。(PHP 4 以及更高版本可用)

提示和注释

注释:从 PHP 4.4 之后,该函数防止一次发送多个报头。这是对头部注入攻击的保护措施。

具体代码:

< ?php
//重定向浏览器
header("Location: http://www.php.cn");
//确保重定向后,后续代码不会被执行
exit;
?>
Copier après la connexion
< ?php
//重定向到news.php页面
header("Location: news.php");
//确保重定向后,后续代码不会被执行
exit;
?>
Copier après la connexion

注意事项:
该函数需要更改PHP.INI文件才能生效。找到php.ini 这个配置文件,然后查找一项:output_buffering将其值由原来的off改为on,重新启动Apache就ok了。

附录(另外两种跳转实现方法):

方法1:使用Meta标签

Meta标签是HTML中负责提供文档元信息的标签,在PHP程序中使用该标签,也可以实现页面跳转。 若定义http-equiv为refresh,则打开该页面时将根据content规定的值在一定时间内跳转到相应页面。

若设置content="秒数;url=网址",则定义了经过多长时间后页面跳转到指定的网址。例如,使用meta标签实现疫苗后页面自动跳转到冠威博客。

< meta http-equiv="refresh" content="1;url=http://www.php.cn">
Copier après la connexion

例如,以下程序meta.php实现在该页面中停留一秒后页面自动跳转到 ph中文网

方法2:使用JS

< ?php
echo "<script language=&#39;javascript&#39; type=&#39;text/javascript&#39;>";
echo "window.location.href=&#39;http://www.php.cn&#39;";
echo "</script>";
?>
Copier après la connexion

更多PHP函数之header() 实现页面跳转相关文章请关注PHP中文网!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!