替代“标题”以在 PHP 中重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8689471/
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
Alternative to "header" for re-directs in PHP
提问by cpowel2
I am working on a project and I am required to run my program on someone else's webserver. It is a pretty simple login page that I am having the problem with. The program works correctly if I run it through my local host via WAMP. The problem I am having is that the re-direct portion is not working correctly it validates the user and starts a session but when it gets to the redirect nothing happens.
我正在做一个项目,我需要在别人的网络服务器上运行我的程序。这是一个非常简单的登录页面,我遇到了问题。如果我通过 WAMP 通过本地主机运行该程序,则该程序可以正常工作。我遇到的问题是重定向部分无法正常工作,它验证用户并启动会话,但是当它到达重定向时什么也没有发生。
I am either doing something wrong with my syntax which I think is highly unlikely since it does work correctly through my local host. Or alternatively I'm thinking that the server does not have that function (not sure if its possible to pick and choose which modules your server supports although I'm sure it's feasible).
我的语法有问题,我认为这不太可能,因为它确实通过我的本地主机正常工作。或者,我认为服务器没有该功能(不确定是否可以选择您的服务器支持的模块,尽管我确定它是可行的)。
I don't know if it matters but they are using "cpanel" which is where I can access the files and there all in the same directory so if someone could tell me where I am going wrong or suggest an alternative to redirecting via "header" any help would be greatly appreciated. I've looked around but it seems that "header" is the standard work horse.
我不知道这是否重要,但他们正在使用“cpanel”,这是我可以访问文件的地方,并且所有文件都在同一目录中,因此如果有人能告诉我我哪里出错或建议通过“标题”重定向的替代方法“ 任何帮助将不胜感激。我环顾四周,但似乎“header”是标准的工作马。
Heres the code I have:
继承人的代码我有:
if( (!empty($_POST['username'])) && (!empty($_POST['password'])) )
{
// username and password sent from Form
$myusername = $_POST['username'];
$mypassword = $_POST['password'];
$sql="SELECT UserName FROM User WHERE UserName='$myusername' and Password='$mypassword'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
//$active=$row['active'];
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1)
{
echo "we made if to the if";
session_start();
session_register("myusername");
$_SESSION['login_user']=$myusername;
echo "right b4 the re-direct";
header("location: UI.php");
exit;
}
else
echo "Your user name/password was not correct pleast TRY AGAIN!!!";
}
Update: In response to the statements about the echos would the problem possible by that I am processing my form in the same file and using echo $_SERVER['PHP_SELF']
更新:为了回应有关回声的陈述,我在同一个文件中处理我的表单并使用 echo $_SERVER['PHP_SELF']
采纳答案by Ignacio Vazquez-Abrams
From the docs:
从文档:
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.
请记住,
header()
必须在发送任何实际输出之前调用它,无论是通过普通的 HTML 标记、文件中的空行还是来自 PHP。
Your echo
is causing the redirect to fail, as is any other output sent before the header.
您echo
导致重定向失败,在标头之前发送的任何其他输出也是如此。
回答by Rajat Singhal
I use this function for redirect...
我使用此功能进行重定向...
Which works in all situations..even if headers are already sent..or even javascript is disabled..
这适用于所有情况......即使标题已经发送......甚至javascript被禁用......
function redirect($url)
{
if (!headers_sent())
{
header('Location: '.$url);
exit;
}
else
{
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>'; exit;
}
}
回答by Ignacio Vazquez-Abrams
By using the below code we redirect the page
通过使用以下代码,我们重定向页面
$page = $_SERVER['REQUEST_URI'];
echo '<script type="text/javascript">';
echo 'window.location.href="'.$page.'";';
echo '</script>';
回答by SATYA PRAKASH NANDY
echo "<script>window.location.href='yourPage.php'</script>";
回答by nico
You cannot set headers after outputting some text, so either you move the header
call before the echo
statements (fine in your case) or you use output buffering.
输出某些文本后无法设置标题,因此要么header
在echo
语句之前移动调用(在您的情况下很好),要么使用output buffering。
回答by ranksrejoined
Redirecting via headers is illegal if any content has come through. Take out all the echo
s and it should work. If you dowant there to be a message shown to the user before the redirect, you'll likely have to do it with JavaScript via location.href
.
如果有任何内容通过,则通过标头重定向是非法的。取出所有的echo
s,它应该可以工作。如果您确实希望在重定向之前向用户显示一条消息,您可能必须通过 JavaScript 来实现location.href
。
回答by Kishor Sonawane
echo '<script type="text/javascript"> window.location="<Your_URL>";</script>';
header("location: UI.php");
Replace with ...
echo '<script type="text/javascript"> window.location="UI.php";</script>';
回答by Max
You probably have already sent content to the browser before the php header is sent. This can be just a \n after a closing php tag ( ?> )
在发送 php 标头之前,您可能已经将内容发送到浏览器。这可以只是一个 php 结束标记之后的 \n ( ?> )
To find the place where unwanted output is generated canbe hard on bigger projects or long lines of classes extending each other. To find the problem you can do this:
在较大的项目或相互扩展的长行类中,找到产生不需要的输出的地方可能很困难。要找到问题,您可以执行以下操作:
error_reporting(E_ALL);
ini_set("display_errors", 1);
header("Location: https://mydomain.com/myLoginAdress/");
die();
Strict error reporting will throw line and file of the output.
严格的错误报告将抛出输出的行和文件。