2012-08-22 74 views
0

我想檢查用戶名是否在我的數據庫之一。PHP腳本來檢查用戶是否在數據庫然後授權到LDAP

$mysqli->prepare("SELECT username FROM table WHERE username = ?") ... 

如果用戶名在我的數據庫中,我想讓他們嘗試授權給我的LDAP服務器。

if($stmt->num_rows === 0){ ... 

下面的代碼不需要所有的mysqli就可以測試用戶名是否在數據庫中。現在,如果我嘗試登錄,我會見了:

Fatal error: Call to undefined method mysqli_stmt::get_result() in C:\xampp\htdocs\webapp\login.php on line 22

<?php 
    require_once('config.php'); //db connection 

    //error_reporting(0); 
    if (isset($_POST['submitted'])) { 

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

     $ldap = ldap_connect("localserv1.local.com", 389) or exit ("Error connecting to LDAP server."); 

     //Settings for AD 
     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3); 
     ldap_set_option($ds, LDAP_OPT_REFERRALS, 0); 

      //Prep statment to check if user is in tblAdmins 
      if ($stmt = $mysqli->prepare("SELECT username FROM table WHERE username = ?")) 
      { 
        $stmt->bind_param('s', $username); 
        $stmt->execute(); 
        $stmt->store_result(); 
        $result = $stmt->get_result(); 

        // Check if the username is in the db 
        if($stmt->num_rows === 0){ 
        //The user is not in the DB 
        echo('<p class="error">You are not authorized to view this application.</p><div class="clear"></div>'); 
        }elseif ($bind = ldap_bind($ldap, 'local\\'.$username, $password)) {   
        //Log them in! 
        session_register("username"); 
        session_register("password"); 
        header("Location: https://" . $_SERVER['HTTP_HOST'] . substr($_SERVER['REQUEST_URI'], 0, -9) . "index.php"); 
        exit;  
        }else { 
         // Invalid LDAP user/pass 
         echo('<p class="error">Invalid username or password.</p><div class="clear"></div>');   
        } 
      }else { 
       //Error 
       printf("Prep statment failed: %s\n", $mysqli->error); 
      } 
    } 
    ?> 
+3

打開錯誤記錄。你明確地關閉了它。如果沒有它,你希望如何進行調試? –

回答

1

一個comment PHP手冊頁面上get_result暗示「mysqlnd驅動程序」可能需要獲得該功能的工作。

由於您實際上沒有使用$result變量,因此您可以完全刪除該行以作爲更簡單的解決方法。

+0

你說得對,我沒有用它..傻了。沒有它,完美地工作。謝謝。 –

相關問題