2017-01-18 48 views
0

我的angularjs登录服务似乎无法读取我的登录php文件,因为它允许用户使用错误的凭据进行日志记录。请帮帮我。提前致谢。登录服务似乎无法读取php文件

这是我的登陆PHP文件

<?php 
$con = mysqli_connect("localhost","root",""); 
mysqli_select_db($con, "motor_pool"); 

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

$Username = mysqli_real_escape_string($con, $user->Username); 
$Password = mysqli_real_escape_string($con, md5($user->Password)); 

$sql = "SELECT * FROM users WHERE Username='$Username' AND     
Password='$Password'"; 
$result = mysqli_query($con, $sql); 

if(mysqli_num_rows($result) > 0){ 
session_start(); 
$_SESSION['uid'] = uniqid('auth_'); 
print $_SESSION['uid']; 
}else { 
echo "no"; 
} 
?> 

,这是我的登录服务文件

'use strict'; 
angular.module('motorApp') 
.factory('loginService', ['$http', '$location', 'sessionService', 
    function($http, $location, sessionService) { 
     return { 
      login: function(data, scope) { 
       var $promise = $http.post('database/login.php', data); 
       $promise.then(function(msg) { 
        var uid = msg.data; 
        if(uid) { 
         sessionService.set('uid', uid); 
         $location.path('/dashboard'); 
        } else { 
         $scope.msgtxt = 'incorrect info'; 
         $location.path('/login'); 
        } 
       }); 
      } 
     } 
    } 
]); 

这是我的登录标记

<div class="w3-group"> 
    <label class="w3-label w3-text-black">User Name</label> 
    <input class="w3-input w3-border w3-round" type="text" style="width:100%" name="Username" ng-model="user.Username" required=""> 
</div> 
<div class="w3-group"> 
    <label class="w3-label w3-text-black">Password</label> 
    <input class="w3-input w3-border w3-round" name="Password" type="password" style="width:100%" required="" ng-model="user.Password"> 
</div> 
<div class="w3-center"> 
    <input type="button" name="button" class="w3-btn-block w3-red btnlog" ng-click="login(user)" value="LOGIN"> 
</div> 
+0

你的登录php文件实际上做了什么,除了查询用户表并且没有对结果做任何事情? – flauntster

+0

我的php文件的工作就是检查一个与用户输入的用户名和密码相匹配的现有数据。如果有,那么用户可以访问主页 – jean

+0

是检查从上面粘贴的代码中排除的位还是尚未实现?因为上面的代码片段似乎没有任何用处,除了创建会话而不管用户输入。 – flauntster

回答

1

在login.php中你是无论用户是否通过身份验证,都可以设置$ _SESSION ['uid']。

你需要的东西,如:

$result = mysqli_query($con, $sql); 
if (mysqli_num_rows($result) > 0) { //found an authenticated user 
    session_start(); 
    $_SESSION['uid'] = uniqid('auth_'); 
    //print $_SESSION['uid']; 
    //now redirect to the first authenticated page of your site 
    header("Location: nextPage.php"); //or you can do a url 
    exit; 
} else { //user wasn't found 
//user wasn't found and present error message or error message page 
} 

注意在nextPage.php开始,您应检查设置的会话,如果没有有效的(即不是谁偶然浏览有一个有效的用户),送该用户在其他地方,或者至少阻止其他脚本运行。

+0

谢谢回答..我会应用此 – jean

+0

该代码工作阻止用户使用错误的凭据,但..它不让我现在登录 – jean

+0

成功后,您将需要重定向有效的用户到开始页面的网站。我已经在上面编辑了我的答案。 –