PHP 使用 Session 防止页面重复跳转
使用 session 来判断是否已经跳转过:
session_start();
if (!isset($_SESSION['redirected'])) {
$_SESSION['redirected'] = true;
$current_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$new_url = 'http://www.host.gay' . $_SERVER['REQUEST_URI'];
header('Location: ' . $new_url);
exit();
} else {
// 已经跳转过,不再跳转
}
该代码片段首先启动 session,然后检查是否存在名为 'redirected' 的 session 变量。如果不存在,则将 'redirected' 变量设置为 true,然后进行重定向操作。如果 'redirected' 变量已经存在,则表示已经跳转过,不再进行重定向。
通过这种方式,可以确保用户只被重定向一次,避免重复跳转造成的页面加载问题。
原文地址: https://www.cveoy.top/t/topic/nypS 著作权归作者所有。请勿转载和采集!