Last active
August 29, 2015 14:22
-
-
Save stamster/5403de0c6e198fb85764 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
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($op); //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. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment