PHP 使用 header("Location:") 重定向到不同的 url 不起作用
PHP Redirect to different url using header("Location:") does not work
我对 php 很陌生,我做了一些研究并尝试了某人 ,但对我来说没有用。我想在执行特定代码后将用户重定向到另一个页面。我意识到没有错误消息,网站也没有改变。所以我从代码中删除了几乎所有内容并将其放入一个小的 test.php 文件中。
同样的问题仍然存在。
<!DOCTYPE html>
<html>
<body>
<h1>Tes2</h1>
<?php
// Execute some code here
sleep(10); // Give the user 10 seconds to read the output of my code
// redirect the user with php or trigger a JS script to do that
header("Window-target: _parent");
header("Location: https://www.w3schools.com/");
?>
</body>
</html>
期望:页面应该执行主要的 php 脚本(通过评论可视化)并触发计时器。当计时器结束时,它应该将我重定向到“www.w3schools.com”。应该没有错误或其他消息。如果可能的话,重定向应该由 php 代码完成(JS 可能是解决这个问题的一种方法,但我仍然需要在我的 php 代码执行后启动 JS 代码)。
结果:页面显示并加载了 html 代码。该站点保持不变。没有错误。
环境:运行 在 Chromium 上
Linux Mint(64 位)的版本 96.0.4664.45(Offizieller Build)
网站功能正常,确实按预期执行了 PHP 代码,但不是这个。
是否有将用户重定向到另一个页面的轻量级通用(适用于大多数流行浏览器)解决方案?
Headers 必须 在传输任何数据之前设置,所以你不能把它们放在文件中间。引用 the manual:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
所以至少您需要将文件重写为:
<?php
header("Window-target: _parent");
header("Location: https://www.w3schools.com/");
?>
<!doctype html>
...
此外,永远不要在 http(s) 响应中 sleep()
:响应应该尽可能快地完成,无论它需要生成什么内容。睡眠在(真的)PHP 代码中没有位置。
PHP 和 JS 的组合似乎是最简单的解决方案。但这可能只是我的意见。我尽量把代码记录下来,以便其他人可以理解:
<?php
function redirect() { // Create some JS code which will pause for 3 seconds and execute the move function afterwards. This Function will redirect the user
echo "<script>";
echo "function move() {window.location.replace('http://www.w3schools.com');}";
echo "setTimeout(move, 3000);";
echo "</script>";
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Test2</h1>
<?php
echo "<p>You will be redirected after the code has been executed!</p>";
// Run actual code
redirect(); // Redirect using JS code
?>
</body>
</html>
我对 php 很陌生,我做了一些研究并尝试了某人
<!DOCTYPE html>
<html>
<body>
<h1>Tes2</h1>
<?php
// Execute some code here
sleep(10); // Give the user 10 seconds to read the output of my code
// redirect the user with php or trigger a JS script to do that
header("Window-target: _parent");
header("Location: https://www.w3schools.com/");
?>
</body>
</html>
期望:页面应该执行主要的 php 脚本(通过评论可视化)并触发计时器。当计时器结束时,它应该将我重定向到“www.w3schools.com”。应该没有错误或其他消息。如果可能的话,重定向应该由 php 代码完成(JS 可能是解决这个问题的一种方法,但我仍然需要在我的 php 代码执行后启动 JS 代码)。
结果:页面显示并加载了 html 代码。该站点保持不变。没有错误。
环境:运行 在 Chromium 上 Linux Mint(64 位)的版本 96.0.4664.45(Offizieller Build) 网站功能正常,确实按预期执行了 PHP 代码,但不是这个。
是否有将用户重定向到另一个页面的轻量级通用(适用于大多数流行浏览器)解决方案?
Headers 必须 在传输任何数据之前设置,所以你不能把它们放在文件中间。引用 the manual:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
所以至少您需要将文件重写为:
<?php
header("Window-target: _parent");
header("Location: https://www.w3schools.com/");
?>
<!doctype html>
...
此外,永远不要在 http(s) 响应中 sleep()
:响应应该尽可能快地完成,无论它需要生成什么内容。睡眠在(真的)PHP 代码中没有位置。
PHP 和 JS 的组合似乎是最简单的解决方案。但这可能只是我的意见。我尽量把代码记录下来,以便其他人可以理解:
<?php
function redirect() { // Create some JS code which will pause for 3 seconds and execute the move function afterwards. This Function will redirect the user
echo "<script>";
echo "function move() {window.location.replace('http://www.w3schools.com');}";
echo "setTimeout(move, 3000);";
echo "</script>";
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Test2</h1>
<?php
echo "<p>You will be redirected after the code has been executed!</p>";
// Run actual code
redirect(); // Redirect using JS code
?>
</body>
</html>