2016-04-27 83 views
0

我是Cordova和離子的新手。目前我需要使用MySQL(XAMPP服務器)來驗證登錄。使用PHP,MySQL(XAMPP)的離子用戶認證

這是我的login.html

<ion-view view-title="Login" name="login-view"> 
    <ion-content class="padding"> 
     <div class="app-icon"></div>  
     <div class="list"> 
      <label class="item item-input item-floating-label"> 
       <span class="input-label">Username</span> 
       <input type="text" placeholder="Username" ng-model="data.username"> 
      </label> 
      <label class="item item-input item-floating-label"> 
       <span class="input-label">Password</span> 
       <input type="password" placeholder="Password" ng-model="data.password"> 
      </label> 
     </div> 
     <div class="list"> 
      <ion-md-input placeholder="Username" highlight-color="balanced" type="text"></ion-md-input> 
      <ion-md-input placeholder="Password" highlight-color="energized" type="password"></ion-md-input> 
     </div> 
     <div class="padding"> 
      <button class="button button-block button-calm" ng-click="login()">Login</button> 
     </div> 

     </ion-content> 
    </ion-view> 

這是我Services.js

.service('LoginService', function($q) { 
    return { 
     loginUser: function(name, pw) { 
      var deferred = $q.defer(); 
      var promise = deferred.promise; 

      if (name == 'user' && pw == 'secret') { 
       deferred.resolve('Welcome ' + name + '!'); 
      } else { 
       deferred.reject('Wrong credentials.'); 
      } 
      promise.success = function(fn) { 
       promise.then(fn); 
       return promise; 
      } 
      promise.error = function(fn) { 
       promise.then(null, fn); 
       return promise; 
      } 
      return promise; 
     } 
    } 
}) 

這是我的PHP文件。

<?php 
    include_once './database_conn.php'; 
    // array for JSON response 
    $response = array(); 
// check for required fields 

    if(isset($_POST['username']) && isset($_POST['password'])){ 

    $username = $_POST['username']; 
    $pass = $_POST['password']; 


    // connecting to db 
    $db = new DbConnection(); 
    // mysql checking the login 
    $result = mysql_query("SELECT tu_user, tu_pass FROM twp_users WHERE tu_user = '$username' AND tu_pass = '$pass'"); 



    // check if row inserted or not 
    if (mysql_num_rows($result) > 0) { 
     // successfully retrieve data into database 
     $row = mysql_fetch_assoc($result); 
     $response["success"] = 1; 
     $response["message"] = mysql_error(); 

     echo json_encode($response); 
    } else { 
     // failed to retrieve row 
     $response["success"] = 0; 
     $response["message"] = "Login failed"; 
     // echoing JSON response 
     echo json_encode($response); 
    } 
} else { 
    // required field is missing 
    $response["success"] = 0; 
    $response["message"] = "Required field(s) is missing"; 
    // echoing JSON response 
    echo json_encode($response); 

} 
    ?> 

我的問題是我怎麼想的Services.js使用

if (name == 'user' && pw == 'secret') { 

來驗證我的用戶,而不是

+0

我在PHP相當糟糕,但你試圖在客戶端驗證用戶名和密碼?這絕不是一個好主意......將您的憑據發送到服務器,檢查它們並告訴客戶端身份驗證是否成功。 – Phonolog

回答

0
loginUser: function(name, pw) { 
      var deferred = $q.defer(); 
      var promise = deferred.promise; 

      var link = 'http://localhost/xampp/ioninc-php-login/login.php'; 

      $http.post(link, { username: name, password: pw }).then(function (res) { 

       // alert("hi ",res.data); 
       if (res.data == "Suuccessful") { 
        deferred.resolve('Welcome ' + name + '!'); 
       } else { 
        deferred.reject('Wrong credentials.'); 
       } 
      });