에이바바
에이바바

Reputation: 1031

PHP script to check if user is in database then auth to LDAP

I'm trying check if a username is in one of my databases.

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

If the username is in my database I want to have them then try to auth to my LDAP server.

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

The code below works without all of the mysqli to test if the username is in the DB. Right now if I try to log in I'm met with:

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);
            }
    }
    ?>

Upvotes: 0

Views: 1358

Answers (1)

John C
John C

Reputation: 8415

A comment on the PHP manual page for get_result suggests the "mysqlnd driver" may be needed to get that function to work.

Since you aren't actually using the $result variable, you could remove that line entirely as an easier work around.

Upvotes: 1

Related Questions