简体   繁体   中英

PHP LDAP bind intermittent issue

I am developing an application for my company which requires the users to log in. To automate the log in process I have tried to integrate the ldap id and the password authentication with this app. Below is the code that I have written for this integration. I have checked online and could not find any one facing this issue.

I have this code ->

function isAuthenticated($u,$p) {
        $ldap_host = 'my_host';
        $ldap = ldap_connect($ldap_host);
        ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);
        if($ldap) {
                if($bind = ldap_bind($ldap,$u,$p)) {
                    ldap_unbind($ldap);
                    return 'Authenticated';
                } else {
                        return 'Invalid Credentials';
                    }
            } else {
                    return 'Not able to connect';
                }
    }

$username = 'preventAnonymousLogin';
$password = 'preventAnonymousLogin';

if(isset($_REQUEST['uname'])) {
        $username = $_REQUEST['uname'];
    }

if(isset($_REQUEST['password'])) {
        $password = $_REQUEST['password'];
    }

$auth = isAuthenticated($username,$password);

This works most of the time but some time it does not and gives this error ->

Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\avrs\htdocs\ldap.php on line 12

I have no idea why this would work some times and some times it just don't. Please suggest. Thanks in advance.

This same code works intermittently. I have tried to clear the cookies and all which seems to work some time but it is not today. So, unless I know for sure what the issue is I think I will not be able to fix this issue for good.

确保提供ldap_host的完整DNS名称,例如myhost.mydomain.com。

Based on the Error you can try setting the maximum script execution time in your C:\\xampp\\htdocs\\avrs\\htdocs\\ldap.php to 60 seconds or something greater than 30 seconds. From your code above it would look something like

function isAuthenticated($u,$p) {
        set_time_limit(60);
       // Rest of your code from above
    }

set_time_limit documentation

Take a look at these examples. The first one is a very simple LDAP / active directory login script. The second is actually built for Yii framework but is more robust in that it handles using multiple servers in case one of them is down.

Simple LDAP / Active Directory Form

Yii Active Directory UserIdentity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM