在 PHP 中重定向而不使用 header 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27123470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect in PHP without use of header method
提问by dee_styles
This contact.php form was working to handle a submit and then redirect to a new page and then all of a sudden just stopped working. I have tried adding error handling and also moving header to the top in front of all other, but neither works. The form is still submitting the data as expected, it's just the redirect that doesn't work. Any ideas would be appreciated.
这个contact.php 表单正在处理提交,然后重定向到一个新页面,然后突然停止工作。我尝试添加错误处理并将标题移到所有其他前面的顶部,但都不起作用。表单仍然按预期提交数据,只是重定向不起作用。任何想法,将不胜感激。
<?php
include 'config.php';
$post = (!empty($_POST)) ? true : false;
if($post)
{
$email = trim($_POST['email']);
$subject = "Downloaded Course Units";
$error = '';
if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
"From: ".$email."\r\n"
."Reply-To: ".$email."\r\n"
."X-Mailer: PHP/" . phpversion());
if($mail)
{
echo 'OK';
header('location: http://www.google.com.au/');
exit();
}
}
}
?>
回答by Pupil
Use javascript.
使用 JavaScript。
Instead of
代替
header('location: http://www.google.com.au/');
Use,
用,
?>
<script type="text/javascript">
window.location.href = 'http://www.google.com.au/';
</script>
<?php
It will redirect even if something is output on your browser.
即使您的浏览器上有输出,它也会重定向。
But, one precaution is to be made: Javascript redirection will redirect your page even if there is something printed on the page.
但是,需要采取一项预防措施:即使页面上打印了某些内容,Javascript 重定向也会重定向您的页面。
Make sure that it does not skip any logic written in PHP.
确保它没有跳过任何用 PHP 编写的逻辑。
回答by Shubham Kumar
Replace the header('location: http://www.google.com.au/');
line with the below code to redirect in php without using header function.
用header('location: http://www.google.com.au/');
下面的代码替换该行以在 php 中重定向而不使用 header 函数。
$URL="http://yourwebsite.com/";
echo "<script type='text/javascript'>document.location.href='{$URL}';</script>";
echo '<META HTTP-EQUIV="refresh" content="0;URL=' . $URL . '">';
If you're wondering that why I have used both Meta tag and JavaScript to Redirect, then the answer is very simple.
如果您想知道为什么我同时使用 Meta 标记和 JavaScript 来 Redirect,那么答案很简单。
If JavaScript is Disabled in the Browser, then meta tag will redirect the page.
如果浏览器中禁用了 JavaScript,则元标记将重定向页面。
回答by Javlon Tulkinov
header not working after include, echo. try again without include, echo. OR instead of function header use
包含,回显后标题不起作用。不包括,回声再试一次。OR 而不是函数头使用
echo '<meta http-equiv="refresh" content="0; URL=http://www.google.com.au/">';
回答by jay.jivani
If you want to redirect to another page after html code then use location.href
javascript method.
如果您想在 html 代码之后重定向到另一个页面,请使用location.href
javascript 方法。
Refer to this sample code:
参考这个示例代码:
<html>
<head>
<title> Using the href property of the Location object</title>
</head>
<body>
<script language="JavaScript">
<!--
function show(){
document.location.href ="http://www.java2s.com";
}
-->
</script>
<form name="form1">
<br>
<input type="button" name="sethref" value="Set href" onClick='show()'>
<br>
</form>
</body>
</html>
回答by Gregory Santana
I solved this with:
我用以下方法解决了这个问题:
function GoToNow ($url){
echo '<script language="javascript">window.location.href ="'.$url.'"</script>';
}
Use : GoToNow ('http://example.com/url-&error=value');
使用:GoToNow(' http://example.com/url-&error=value');
回答by sidoco
In addition to the above answers, you can use the following JavaScript code for both the document and the main window, as well as in iframes:
除了上述答案之外,您还可以在文档和主窗口以及 iframe 中使用以下 JavaScript 代码:
<script type="text/javascript" >
window.open('http://www.google.com.au/', '_self');
</script>
_selfOpens the linked document in the same frame as it was.
_self在原来的框架中打开链接的文档。
In the above command, the values of the second parameter are similar to the values of the target attribute in the tag A(<a></a>
) in HTML, which can use the following values in addition to the _selfvalue:
上述命令中,第二个参数的值类似于<a></a>
HTML中标签A( )中target属性的值,除了_self值外,还可以使用以下值:
_blankOpens the linked document in a new window or tab
_blank在新窗口或选项卡中打开链接的文档
_parentOpens the linked document in the parent frame
_parent在父框架中打开链接文档
_topOpens the linked document in the full body of the window
_top在整个窗口中打开链接的文档
framenameOpens the linked document in a named frame
framename在命名框架中打开链接文档