通过AJAX工作不正常称为PHP头() [英] PHP header() called via AJAX not working properly

查看:177
本文介绍了通过AJAX工作不正常称为PHP头()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的网络发展。

现在我的工作在一个网站的登录功能。我使用的JavaScript / AJAX来获取用户名和密码,并在MYSQL数据库发送到PHP文件进行验证。这就是我将要进行。

我的问题是,为什么不能在header()函数正常工作?我希望用户登录后,她被重定向到的个人资料页(profile.php)

下面是PHP(login.php中)的片​​段:

  $查询=SELECT * FROM用户其中username ='$的uname'和密码= MD5('$通行证);;
$ RET =请求mysql_query($查询)或死亡(mysql_error());

如果(!$ RET){
    $味精=无效的查询。 mysql_error()。 \ N的;
    $味精。=整个查询。 $查询;
    死亡($味精);
}

$用户ID = -1;
而($行= mysql_fetch_array($ RET)){
    $用户ID = $行[ID];
}

$ CNT = mysql_num_rows($ RET);
如果($ CNT == 1){
    在session_start();
    $ _SESSION [用户id] = $用户ID;
    $ _SESSION [的uname] = $的uname;
    回声您已成功登录!;
    标题(位置:profile.php);
} 其他 {
    回声错误的用户名/密码;
}
 

下面是对Javascript的(一个AJAX功能):

  VAR OBJ;
    VAR URL =的login.php;
    VAR PARAMS =的uname =+ document.getElementsByName(的uname)[0] .value的+&放大器;通=+ document.getElementsByName(通行证)[0] .value的;
    如果(window.XMLHtt prequest){//主浏览器
        OBJ =新XMLHtt prequest();
        obj.open(POST,网址,真实);
        obj.setRequestHeader(内容型,应用程序/ x-WWW的形式urlen codeD);
        obj.setRequestHeader(内容长度,params.length);
        obj.setRequestHeader(连接,关闭);
        obj.onreadystatechange =功能(){
            如果(obj.readyState == 4){
                如果(obj.status == 200){
                    // 成功
                } 其他 {
                    警报(在返回的数据问题+错误状态=+ obj.status);
                }
            }
        }
        obj.send(PARAMS);
 

解决方案

我不认为重定向将与AJAX。这是会发生什么:

  1. 在AJAX请求被发送到的login.php
  2. 的login.php 发回一个头与地点:profile.php
  3. 在浏览器然后重定向和取 profile.php
  4. 的结果 profile.php ,然后传递给你的XMLHtt prequest对象。

一个方式来获得Ajax响应重定向你的网页是做到这一点:

  • 的login.php 响应返回包含状态code(302或301)和位置重定向到一个JSON响应。

  • 响应本身有200状态code(成功)。

  • 过程中的JSON响应,并检查其状态code为302或301,并重定向到的位置。

I'm new to web development.

Right now I'm working on a login feature on a site. I used Javascript/AJAX to fetch the username and password and send it to a PHP file for verification on the MYSQL database. That's what I'm about to make.

My question is why can't the header() function working properly? I want after the user login she is redirected to the profile page (profile.php)

Here's snippet of the PHP (login.php):

$query = "SELECT * from user WHERE username = '$uname' AND password = md5('$pass');";
$ret = mysql_query($query) or die(mysql_error());

if(!$ret) {
    $msg = "Invalid query " . mysql_error() . "\n";
    $msg .= "Whole query " . $query;
    die($msg);
}

$userid = -1;
while($row = mysql_fetch_array($ret)) {
    $userid = $row["ID"];
}

$cnt = mysql_num_rows($ret);
if($cnt == 1) {
    session_start();
    $_SESSION["userid"] = $userid;
    $_SESSION["uname"] = $uname;
    echo "You have logged in successfully!";
    header("Location: profile.php");
} else {
    echo "Wrong Username/Password";
}

And here's for the Javascript (an AJAX function):

var obj;
    var url = "login.php";
    var params = "uname=" + document.getElementsByName("uname")[0].value + "&pass=" + document.getElementsByName("pass")[0].value;
    if(window.XMLHttpRequest) { // Major browsers
        obj = new XMLHttpRequest();
        obj.open("POST",url,true); 
        obj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        obj.setRequestHeader("Content-length", params.length);
        obj.setRequestHeader("Connection", "close");
        obj.onreadystatechange = function() {
            if(obj.readyState == 4) {
                if(obj.status == 200) {
                    // success
                } else {
                    alert("Problem in returned data" + "Error status=" + obj.status);
                }
            }
        }
        obj.send(params);

解决方案

I don't think the redirect will work with AJAX. This is what will happen:

  1. AJAX request is sent to login.php
  2. login.php sends back a header with Location: profile.php
  3. The browser then redirects and fetches profile.php
  4. The results of profile.php is then passed to your XMLHttpRequest Object.

A way to get the AJAX response to redirect your page is to do this:

  • The response from login.php returns a JSON response containing the status code (302 or 301) and the location to redirect to.

  • The response itself has a status code of 200 (successful).

  • The process the JSON response and check the status code for 302 or 301 and redirect to the location.

这篇关于通过AJAX工作不正常称为PHP头()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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