php怎样实现页面跳转页面跳转页面
PHP 实现页面跳转的方法
使用 header 函数
通过 header 函数发送 HTTP 头部信息实现跳转,跳转后需立即终止脚本执行。
header("Location: https://example.com/target-page.php");
exit;
使用 HTML meta 标签
在 HTML 中插入 meta 标签实现自动跳转,适用于无法修改 HTTP 头部的情况。

echo '<meta http-equiv="refresh" content="0;url=https://example.com/target-page.php">';
使用 JavaScript 跳转
通过输出 JavaScript 代码实现客户端跳转,灵活性较高。
echo '<script>window.location.href = "https://example.com/target-page.php";</script>';
使用 HTTP 状态码
对于临时或永久重定向,可以指定 HTTP 状态码。

header("HTTP/1.1 301 Moved Permanently");
header("Location: https://example.com/new-location.php");
exit;
延迟跳转实现
通过 meta 标签或 JavaScript 设置延迟跳转时间。
// 5秒后跳转
echo '<meta http-equiv="refresh" content="5;url=https://example.com/target-page.php">';
// 或
echo '<script>setTimeout(function(){ window.location.href = "https://example.com/target-page.php"; }, 5000);</script>';
条件跳转实现
根据特定条件决定是否跳转。
if ($condition) {
header("Location: https://example.com/target-page.php");
exit;
}
注意事项
- 使用
header函数前不能有任何输出 - 跳转 URL 可以是相对路径或绝对路径
- 对于重要操作跳转,建议同时使用服务器端和客户端跳转方法






