使用ajax时,php标头加载并打开登录表单中的重定向页面

时间:2015-12-29 12:59:45

标签: php ajax

谢谢,代码可能不太好但仍然是,这是我的php:

此外,我使用会话销毁,因为它可能无法正常工作,它只是登录即使用户名,密码错误留下相关的错误:

<?php
    if($_POST){
        $_SESSION['name'] = $_POST['name'];
        $_SESSION['password'] = md5($_POST['password']);
        if( $_SESSION['name'] && $_SESSION['password']){
            mysql_connect("localhost", "root", "") or die("problem with        connection...");
            mysql_select_db("testdb");
            $query = mysql_query("SELECT * FROM registered WHERE name='".$_SESSION['name']."'");
            $numrows = mysql_num_rows($query);
                if($numrows != 0){
                    while($row = mysql_fetch_assoc($query)){
                        $dbname = $row['name'];
                        $dbpassword = ($row['password']);
                    }
                    if($_SESSION['name']==$dbname){
                        if($_SESSION['password']==$dbpassword){
                            header("location:loggedinuser.php");
                            die();
                        }else {
                            echo "password is not correct";
                            session_destroy();
                        }
                    }else{
                        echo "name is not correct";
                        session_destroy();
                    }
                }else{
                    echo"This name is not registered";
                    session_destroy();
                }
        }else{
            echo"You have to complete the form";
            session_destroy();
        }
    }else{
        echo"Access Denied!";
        exit;
    }?>

这是我在另一页的ajax:

<script type="text/javascript">
        function testing(){
            var xmlhttp;
            if(window.XMLHttpRequest){
                xmlhttp = new XMLHttpRequest();
            }else{
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            var uname = document.getElementById('name').value;
            var upassword = document.getElementById('password').value;
            xmlhttp.onreadystatechange = function(){
                if(xmlhttp.readyState==4){
                    document.getElementById('result').innerHTML = xmlhttp.responseText;
                }
            }
            url = "login.php?name="+uname+"&password="+upassword;
            xmlhttp.open("POST",url,true);
            xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlhttp.send("name="+uname+"&password="+upassword);
        }
    </script>

1 个答案:

答案 0 :(得分:-1)

也许您的登录表单位于(i)frame(或fancybox)中。然后,任何响应标头都将修改此iframe。如果这是您的情况,则无法直接从PHP进行此重定向(当用户提交此表单时),但您必须在ajax代码中传播此事件并在javascript中进行重新加载/刷新或任何您想要的内容。