如何在身份验证后重定向到另一个视图

时间:2017-05-29 13:12:35

标签: php angularjs mysqli ionic-framework

我正在使用angularJS和php后端,我尝试进行身份验证。我通过console.log对它进行了测试,当密码不正确时我收到错误信息,当密码正确时我没有得到任何东西,在这种情况下,我想被骑到其他视图,我怎么办请:

app.js

app.controller('loginCtrl', function($scope, $location,$state,$http,$window){

    $scope.submit = function()
    {
        data = {
            'NomClient' : $scope.NomClient,
            'mdp' : $scope.mdp
        };

        $http.post('http://localhost/deb/login.php', data)
        .success(function(data, status, headers, config)
        {
          // $window.location.href = '#/admin';
            console.log(data);
        })
        .error(function(data, status, headers, config)
        {
            console.log('error');
        });
    }

});

的login.php

 <?php  

$data = json_decode(file_get_contents("php://input"));

 $connect = mysqli_connect("localhost", "root", "", "test");  


 if(count($data) > 0)  
 { 
$NomClient=mysqli_real_escape_string($connect, $data->NomClient);
$mdp=mysqli_real_escape_string($connect, $data->mdp);


$query = 'SELECT * FROM `client` WHERE NomClient = "'.$NomClient.'" AND   mdp= "'.$mdp.'"';

$q = mysqli_query($connect , $query);

if(mysqli_num_rows($q) > 0 )
        { 
            $_SESSION["logged_in"] = true; 
            $_SESSION["naam"] = $NomClient; 
        }
        else
        {
            echo 'The username or password are incorrect!';
        }

}
 ?>

如您所见,我在app.js中有一条评论行:// $ window.location.href =&#39;#/ admin&#39 ;;我把它作为评论,因为当我把它,它重定向到管理员视图,但密码不正确。

提前致谢

2 个答案:

答案 0 :(得分:0)

在php文件中      

$data = json_decode(file_get_contents("php://input"));

 $connect = mysqli_connect("localhost", "root", "", "test");  
$response = array();

 if(count($data) > 0)  
 { 
$NomClient=mysqli_real_escape_string($connect, $data->NomClient);
$mdp=mysqli_real_escape_string($connect, $data->mdp);


$query = 'SELECT * FROM `client` WHERE NomClient = "'.$NomClient.'" AND   mdp= "'.$mdp.'"';

$q = mysqli_query($connect , $query);

if(mysqli_num_rows($q) > 0 )
        { 
            $response["error"] = false;
            $_SESSION["logged_in"] = true; 
            $_SESSION["naam"] = $NomClient; 
        }
        else
        {
            $response['error'] = true;
        }
  echoRespnse(200, $response);

}
function echoRespnse($status_code, $response)
{

    $app->contentType('application/json');
    echo json_encode($response);
}
 ?>

app.js

 app.controller('loginCtrl', function($scope, $location,$state,$http,$window){

    $scope.submit = function()
    {
        data = {
            'NomClient' : $scope.NomClient,
            'mdp' : $scope.mdp
        };

        $http.post('http://localhost/deb/login.php', data)       .success(function(data)
        {
          console.log(data.error);
          if(data.error=="true") { 
              //Show error
           }else {
              //else redirect
         }

        })
        .error(function(data)
        {
            console.log('error');
        });
    }

});

答案 1 :(得分:0)

在您的php页面上

if(mysqli_num_rows($q) > 0 )
        { 
            echo 'success'; 
        }
        else
        {
            echo 'error';
        }

}

在你的app.js上

$http.post('http://localhost/deb/login.php', data)
        .success(function(response)
        {
            if(response.data =='success'){
                  $window.location.href = '#/admin';
                  }else{
                    console.log('Username or Password Error');
                     }

            console.log(data);
        })
        .error(function(data, status, headers, config)
        {
            console.log('error');
        });