使用带有角度js的php从Mongodb检索用户名和密码

时间:2016-04-11 13:42:41

标签: php angularjs mongodb

我使用mongodb,angular js和php创建了一个简单的登录页面。当用户通过从数据库中检索用户名和密码从数据库登录时,它将转到管理页面。但是数据库的登录没有响应,因为无法从db

获取数据

的index.php: 这是该网站的主页。

  <?php
   include('login.php'); // Includes Login Script

   ?> 
  <!doctype html>
  <html>
  <head>
  <meta charset="utf-8">
  <title>LoginIN</title>

  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
  <script type="text/javascript" src="app.js"></script>
   <link href="styles/bootstrap.min.css" rel="stylesheet">
   <link href="styles/signin.css" rel="stylesheet"> 
     </head>
    <body>
     <?php
     if(isset($error) and count($error) > 0){
     ?>
     <div class="errorContainer"> <ul>
     <?php foreach($error as $err) echo '<li>'.$err.'</li>'; ?>         
     </ul></div>
      <?php 
     }

      if(!empty($success)){
    echo '<p class="text-success">'.$success.'</p>';
     }
    ?>
   <div id="wrapper">
      <header id="top">
     <center><h1>Leave Reporting System</h1></center>
    <div class="container">
   <div ng-app ='angularPost' ng-controller='loginCtrl'>
  <form class="form-signin" role="form"  action ="" method="post">
  <h2 class="form-signin-heading">Please sign in</h2>

  <input type="email" name="username" class="form-control" ng-model="username" placeholder="Email address" required autofocus>
      <input type="password" name="password" class="form-control" ng-model="password" placeholder="Password" required>
     <br>
     <div class="checkbox">
     <label>
     <input type="checkbox" value="remember" name ="remember">Remember me
     <br>
     </label>
     </div>
     <input name="submit"button class="btn btn-lg btn-primary btn-block"  ng-click=login() type="submit" value=" Sign in">
            <span>{{responseMessage}}</span>
           <br>
     <a href="">Forgot your password?</a> 
     </form>      
     </div>
     </body>
     </html>

的login.php

  <?php
  $succss = "";
   if(isset($_POST['submitForm'] )){


   $usr_email = $_POST['username'];
   $usr_password = $_POST['password'];

   $error = array();
   if(empty($usr_email) or      !filter_var($usr_email,FILTER_SANITIZE_EMAIL)){
    $error[] = "Empty or invalid email address";
    }
    if(empty($usr_password)){
    $error[] = "Enter your password";   
      }
    if(count($error) == 0){
    $con = new MongoClient();
    if($con){
        // Select Database
        $db = $con->lrs;
        // Select Collection
        $Login = $db->Login;
        $qry = array("username" => $usr_email,"password" => md5($usr_password));
        $result = $Login->findOne($qry);
        if($result){
            $success = "You are successully loggedIn";
            // Rest of code up to you....   
        }           
    } else {
        die("Mongo DB not installed");
    }   
}
  }   
 ?>

app.js

     var app = angular.module('angularPost',[]);
     app.controller('loginCtrl', function($scope, $http)
     {
     $scope.login = function() {
      var request = $http({
        method : "post",
        url  : "login.php",

        data : {
            email : $scope.username,
            password: $scope.password
        },
    });
    request.success(function(data)
    {
        if(data == "1"){
            $scope.responseMessage = "Successfully Logged in";
        }
        else{
            $scope.responseMessage= "Username or Password is invalid"
        }
    });
}
 });

0 个答案:

没有答案