PHP 跳转到其他网站是指使用 PHP 代码实现当前页面重定向到另一个 URL 地址。这种操作通常用于导航、错误处理、表单提交后的反馈等场景。
<?php
// 永久重定向到 https://www.example.com
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://www.example.com");
exit();
// 临时重定向到 https://www.example.com
header("HTTP/1.1 302 Found");
header("Location: https://www.example.com");
exit();
// 自定义重定向逻辑
if (isset($_GET['page'])) {
$page = $_GET['page'];
switch ($page) {
case 'home':
header("Location: https://www.example.com/home");
break;
case 'about':
header("Location: https://www.example.com/about");
break;
default:
header("Location: https://www.example.com");
break;
}
exit();
}
?>header() 函数在输出内容之后调用。header() 函数之前没有任何输出,包括空格和换行。header() 函数之前没有任何输出,包括空格和换行。通过以上信息,您可以更好地理解 PHP 跳转到其他网站的基础概念、优势、类型、应用场景以及常见问题的解决方法。