上一篇
<?php header("Location: https://www.newdomain.com"); exit; // 必须终止脚本执行! ?>
🔥 关键点:
exit
避免后续代码执行header("HTTP/1.1 301 Moved Permanently"); header("Location: https://www.newdomain.com"); exit;
header("HTTP/1.1 302 Found"); header("Location: /sale-page"); exit;
ob_start(); header(...); ob_end_flush();
echo '<script>alert("欢迎注册成功!");</script>';
echo '<script> alert("操作完成!"); window.location.href = "/dashboard"; </script>';
echo '<link href="https://cdn.jsdelivr.net/npm/sweetalert2@11" rel="stylesheet">'; echo '<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>'; echo '<script> Swal.fire({ "🎉 成功!", text: "您的订单已提交", icon: "success", confirmButtonText: "查看详情" }).then(() => { window.location.href = "/orders"; }); </script>';
// 前端代码 document.getElementById("btn").onclick = function() { const win = window.open("", "_blank"); fetch("/api/data") .then(res => res.json()) .then(data => { win.location.href = data.url; }); };
<script src="https://cdn.polyfill.io/v3/polyfill.min.js"></script>
.modal { width: 90% !important; max-width: 500px; }
<?php if ($valid_login) { header("Location: /dashboard"); exit; } else { echo '<script> Swal.fire({ "登录失败", text: "用户名或密码错误", icon: "error" }).then(() => { window.history.back(); }); </script>'; } ?>
echo '<script> Swal.fire({ "确认提交?", text: "提交后不可修改", icon: "warning", showCancelButton: true, confirmButtonText: "提交", cancelButtonText: "取消" }).then((result) => { if (result.isConfirmed) { document.getElementById("myForm").submit(); } }); </script>';
场景 | 推荐方案 | 兼容性 |
---|---|---|
简单跳转 | header() + exit | |
带反馈的跳转 | SweetAlert2 + location.href | |
移动端友好弹窗 | SweetAlert2 + 自定义CSS | |
避免浏览器拦截 | 同步请求 + 预打开窗口 |
🔮 未来趋势:
2025年PHP 8.3已原生支持http\Response
类,推荐使用:
$response = new http\Response(302); $response->setHeader("Location", "/new-page"); $response->send();
💬 互动时间:
遇到弹窗被拦截?评论区留下你的场景,帮你1秒解决! 👇👇👇
本文由 业务大全 于2025-08-26发表在【云服务器提供商】,文中图片由(业务大全)上传,本平台仅提供信息存储服务;作者观点、意见不代表本站立场,如有侵权,请联系我们删除;若有图片侵权,请您准备原始证明材料和公证书后联系我方删除!
本文链接:https://xdh.7tqx.com/wenda/738183.html
发表评论