要在 PHP 中重定向页面,请使用:
<?php
header('Location: url/file.php');
?>
要刷新到 HTML 中的不同页面,请使用:
<meta http-equiv='refresh' content='0;url=http://url/file.php'>
在内容属性中,0 是等待的秒数。
要在 JavaScript 中刷新到不同的页面,请使用:
window.location.href = 'url/file.php';
如果这些都不起作用,请使用 HTML 跟随锚链接:
<a href="url/file.php">Click here to go now!</a>
要回答您的问题,可以通过以下几种方式完成:
1)非常糟糕,需要两个文件,超级冗余的
HTML文件:
<form action="http://site.com/page.php" method="post" name="myform">
<input type="hidden" name="url" value="<?php=$url?>">
</form>
<script type="text/javascript">
// Submit the form
document.forms['myform'].submit();
</script>
页面.php:
<?php
// Catch url's value, and send a header to redirect
header('Location: '.$_POST['url']);
?>
2)稍微好一点,还是不推荐
<form action="http://site.com/page.php" method="post" name="myform">
<input type="hidden" name="url" value="<?php=$url?>">
</form>
<script type="text/javascript">
// Set form's action to that of the input's value
document.forms['myform'].action = document.forms['myform'].elements['url'].value;
// Submit the form
document.forms['myform'].submit();
</script>
3)仍然非常多余,但我们正在变得更好
<form action="http://site.com/page.php" method="post" name="myform">
<input type="hidden" name="url" value="<?php=$url?>">
</form>
<script type="text/javascript">
// Simply refresh the page to that of input's value using JS
window.location.href = document.forms['myform'].elements['url'].value;
</script>
4)好多了,省了很多麻烦,一开始就用JS
<?php
// Start with a PHP refresh
$url = 'url/file.php'; // Variable for our URL
header('Location: '.$url); // Must be done before ANY echo or content output
?>
<!-- fallback to JS refresh -->
<script type="text/javascript">
// Directly tell JS what url to refresh to, instead of going through the trouble to get it from an input
window.location.href = "<?php=$url?>";
</script>
<!-- meta refresh fallback, incase of no JS -->
<meta http-equiv="refresh" content="0;url=<?php=$url?>">
<!-- fallback if both fail (very rare), just have the user click an anchor link -->
<div>You will be redirected in a moment, <a href="<?php=$url?>">or you may redirect right away</a>.</div>
用.php
扩展名保存它,你应该很高兴。