note 115662 added to function.ldap-connect
| From: | nemanja at prolux-universal dot com | Date: | Thu, 04 Sep 2014 11:00:05 +0000 |
| Subject: | note 115662 added to function.ldap-connect | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-200367@lists.php.net to get a copy of this message | ||
If you don't want your PHP program to wait XXX seconds before giving up in a case when one of
your corporate DC have failed, and since ldap_connect() does not have a mechanism to timeout on a
user specified time, this is my workaround which shows excellent practical results.
===========================================================
function serviceping($host, $port=389, $timeout=1)
{
$op = fsockopen($host, $port, $errno, $errstr, $timeout);
if (!$op) return 0; //DC is N/A
else {
fclose($opanak); //explicitly close open socket connection
return 1; //DC is up & running, we can safely connect with ldap_connect
}
}
// ##### STATIC DC LIST, if your DNS round robin is not setup
//$dclist = array('10.111.222.111', '10.111.222.100',
'10.111.222.200');
// ##### DYNAMIC DC LIST, reverse DNS lookup sorted by round-robin result
$dclist = gethostbynamel('domain.name');
foreach ($dclist as $k => $dc) if (serviceping($dc) == true) break; else $dc = 0;
//after this loop, either there will be at least one DC which is available at present, or $dc would
return bool false while the next line stops program from further execution
if (!$dc) exit("NO DOMAIN CONTROLLERS AVAILABLE AT PRESENT, PLEASE TRY AGAIN LATER!");
//user being notified
//now, ldap_connect would certainly connect succesfully to DC tested previously and no timeout will
occur
$ldapconn = ldap_connect($dc) or die("DC N/A, PLEASE TRY AGAIN LATER.");
===========================================================
Also with this approach, you get a real nice failover functionality, take for an example a company
with a dozen of DC-a distributed along distant places, this way your PHP program will always have
high availability if at least one DC is active at present.
----
Server IP: 72.52.91.14
Probable Submitter: 89.216.53.20
----
Manual Page -- http://php.net/manual/en/function.ldap-connect.php
Edit -- https://master.php.net/note/edit/115662
Del: integrated -- https://master.php.net/note/delete/115662/integrated
Del: useless -- https://master.php.net/note/delete/115662/useless
Del: bad code -- https://master.php.net/note/delete/115662/bad+code
Del: spam -- https://master.php.net/note/delete/115662/spam
Del: non-english -- https://master.php.net/note/delete/115662/non-english
Del: in docs -- https://master.php.net/note/delete/115662/in+docs
Del: other reasons-- https://master.php.net/note/delete/115662
Reject -- https://master.php.net/note/reject/115662
Search -- https://master.php.net/manage/user-notes.php