Voting

: max(five, four)?
(Example: nine)

The Note You're Voting On

antoine dot php dot net at bonnefoy dot eu
9 years ago
Hello,

Little corrections to nemanja post.
- There was a warning if connection is denied by firewall (adding @ before fsockopen)
- fclose parameter was incorrect.

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.
<?php
function serviceping($_host, $_port = 389, $_timeout = 1) {
$op = @fsockopen($_host, $_port, $errno, $errstr, $_timeout);
if (!
$op) {
echo
"KO!";
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
}
}

function
ldap_connect_failover($_domain) {
// ##### 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);

foreach (
$dclist as $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) {
return
false;
}
//user being notified

return ldap_connect($dc);
}
?>

<< Back to user notes page

To Top