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;
} else {
fclose($op); return 1; }
}
function ldap_connect_failover($_domain) {
$dclist = gethostbynamel($_domain);
foreach ($dclist as $dc) {
if (serviceping($dc) == true) {
break;
} else {
$dc = 0;
}
}
if (!$dc) {
return false;
} return ldap_connect($dc);
}
?>