替代“标题"用于在 PHP 中重定向 [英] Alternative to "header" for re-directs in PHP

查看:22
本文介绍了替代“标题"用于在 PHP 中重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个项目,我需要在别人的网络服务器上运行我的程序.这是一个非常简单的登录页面,我遇到了问题.如果我通过 WAMP 通过本地主机运行该程序,则该程序可以正常工作.我遇到的问题是重定向部分无法正常工作,它验证用户并启动会话,但是当它到达重定向时什么也没有发生.

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.

我的语法有问题,我认为这不太可能,因为它确实通过我的本地主机正常工作.或者,我认为服务器没有该功能(不确定是否可以选择您的服务器支持的模块,尽管我确定它是可行的).

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).

我不知道这是否重要,但他们正在使用cpanel",这是我可以访问文件的地方,并且所有文件都在同一目录中,因此如果有人能告诉我我哪里出错了或建议重定向的替代方法通过标题"任何帮助将不胜感激.我环顾四周,但似乎header"是标准的工作马.

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.

这是我的代码:

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!!!";

} 

更新:为了回应有关回声的陈述,我在同一个文件中处理我的表单并使用 echo $_SERVER['PHP_SELF']

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']

推荐答案

来自 文档:

请记住,必须在发送任何实际输出之前调用 header(),无论是通过普通的 HTML 标记、文件中的空行还是来自 PHP.

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.

您的 echo 导致重定向失败,在标头之前发送的任何其他输出也是如此.

Your echo is causing the redirect to fail, as is any other output sent before the header.

这篇关于替代“标题"用于在 PHP 中重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆