【问题标题】:Simple authentication using angularJS and php使用 angularJS 和 php 的简单身份验证
【发布时间】:2017-10-29 16:24:33
【问题描述】:

自从我阅读有关身份验证的教程和问题以来已经一个多星期了,但徒劳无功。这个问题我已经问过好几次了,每次都改代码,但是找不到解决办法。

我想在我的应用程序中进行简单的身份验证,保持与我使用的代码相同的结构。这是我的 html 文件:

登录.html

<ion-content class="padding" ng-controller="loginCtrl">
<div class="list list-inset" >
<label class="item item-input">
      <input type="text" placeholder="nom" required="" ng-model="NomClient"> 
</label> 
<label class="item item-input">
      <input type="password" placeholder="Password" ng-model="mdp"> 
</label> 
    <button class="button button-block button-positive" ng-click="submit()">Login</button>       
 </ion-content>

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';
        })
        .error(function(data, status, headers, config)
        {
            console.log('error');
        });
    }

});

登录.php

<?php  

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

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



$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.'"';


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

此代码不起作用,因为它不检查密码和用户名,并且总是将我重定向到管理员视图。

提前致谢

【问题讨论】:

  • 控制台有错误吗?
  • 控制台没有错误,但是当我独立执行 login.php 文件时,我得到 NomClient 和 mdp 的未定义索引
  • 您可以从最后一段代码的颜色格式看出您的 SQL 中的引号有问题。
  • 是的,我编辑代码,复制问题
  • 打印成功值 -> console.log() 中的数据。

标签: php angularjs mysqli ionic-framework


【解决方案1】:

我检查了您的代码,发现了一个错误。 登录.php

if(mysqli_num_rows($q) > 0 ) //here $q is undefined 

第二件事是你必须首先执行如下查询:

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

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

if(mysqli_num_rows($q) > 0 ){}

最后的变化是

$http.post('http://localhost/deb/login.php', data)
        .success(function(data, status, headers, config)
        {
            if(data == "The username or password are incorrect!")
               console.log('error');
            else 
               $window.location.href = '#/admin';
        })
        .error(function(data, status, headers, config)
        {
            console.log('error');
        });

【讨论】:

  • 是的,你是对的,它是 '$query' 而不是 '$q'。我更正了,但我仍然有同样的问题
  • 我改正了,可惜还是有同样的问题
  • 当密码和用户名正确时,它不会将我重定向到管理员视图!
  • 对不起,现在它会将我重定向到管理员视图,但是密码错误
  • 评论您在控制台中看到的内容
猜你喜欢
  • 1970-01-01
  • 2017-10-24
  • 1970-01-01
  • 1970-01-01
  • 2013-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多