php实现三秒跳转
PHP 实现三秒跳转的方法
使用 header 函数结合 refresh 参数可以实现页面自动跳转。以下是一个简单的示例代码:

<?php
header("refresh:3;url=http://example.com");
echo "页面将在 3 秒后跳转至目标网址...";
?>
更友好的跳转提示
可以结合 HTML 和 CSS 提供更友好的跳转提示:

<?php
header("refresh:3;url=http://example.com");
?>
<!DOCTYPE html>
<html>
<head>
<title>跳转中...</title>
<style>
.redirect-message {
text-align: center;
margin-top: 50px;
font-size: 18px;
}
</style>
</head>
<body>
<div class="redirect-message">
页面将在 3 秒后自动跳转,<a href="http://example.com">立即跳转</a>
</div>
</body>
</html>
使用 JavaScript 实现跳转
也可以使用 JavaScript 实现跳转,作为备用方案:
<?php
echo '<script>
setTimeout(function() {
window.location.href = "http://example.com";
}, 3000);
</script>';
echo "页面将在 3 秒后跳转...";
?>
注意事项
header函数必须在任何实际输出之前调用- 跳转时间单位为秒
- 目标 URL 可以是相对路径或绝对路径
- 建议提供手动跳转链接作为备用
带倒计时的跳转实现
以下代码实现了带倒计时显示的跳转:
<?php
$target_url = "http://example.com";
$wait_time = 3;
header("refresh:$wait_time;url=$target_url");
?>
<!DOCTYPE html>
<html>
<head>
<title>跳转中...</title>
<script>
var count = <?php echo $wait_time; ?>;
function countDown() {
document.getElementById("count").innerHTML = count;
count--;
if (count < 0) {
clearInterval(timer);
}
}
var timer = setInterval(countDown, 1000);
</script>
</head>
<body>
<div style="text-align:center;margin-top:50px;">
页面将在 <span id="count"><?php echo $wait_time; ?></span> 秒后跳转...
<br>
<a href="<?php echo $target_url; ?>">立即跳转</a>
</div>
</body>
</html>






