PHP 中的 URL 跳转通常是指将用户重定向到另一个页面或网站。这可以通过多种方式实现,例如使用 header()
函数、meta
标签或 JavaScript。
meta
标签在客户端进行重定向。header()
函数进行重定向<?php
// 设置 HTTP 响应头进行重定向
header("Location: https://example.com/new-page.php");
exit(); // 确保在发送重定向头之后停止脚本执行
?>
meta
标签进行客户端重定向<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="0; url=https://example.com/new-page.php">
</head>
<body>
<p>Redirecting...</p>
</body>
</html>
<?php
echo '<script type="text/javascript">';
echo 'window.location.href = "https://example.com/new-page.php";';
echo '</script>';
?>
header()
函数没有生效?header()
函数之前有任何输出(包括空格或换行),header()
函数将不会生效。确保在调用 header()
之前没有任何输出。header()
函数调用后有 exit()
或 die()
来停止脚本执行。header()
函数调用在 HTTP 响应头发送之前。<?php
ob_start(); // 开启输出缓冲
// 你的代码
header("Location: https://example.com/new-page.php");
ob_end_flush(); // 刷新输出缓冲并关闭
exit();
?>
希望这些信息对你有所帮助!
领取专属 10元无门槛券
手把手带您无忧上云