PHP auth against htaccess protection

I needed to authenticate against a .htaccess protected directory using PHP.
I needed it because I wanted to use LDAP login with PHP but couldn't access the LDAP server directly. I set up a protected directory using LDAP ressources on a web server and authenticated against it with this function.
To be frank, that is rather a workaround, but it was the only way to establish what I wanted to have.

<?php
function httpauth($host,$authurl,$ssl = 0, $user, $pass){
    if($ssl){
        $port = 443;
        $prefix = "ssl://";
    }else{
        $port = 80;
        $prefix = "";
    }

    $sock = fsockopen($prefix.$host, $port);
    fputs($sock, "HEAD ".$authurl." HTTP/1.0\r\n");
    fputs($sock, "Host: ".$host."\r\n");
    fputs($sock, "Authorization: Basic ".base64_encode($user.":".$pass)."\r\n");
    fputs($sock, "\r\n");
    $ret = trim(fgets($sock));
    fclose($sock);
    if($ret == "HTTP/1.1 200 OK") return true;
    else return false;
}


//USAGE:
if(httpauth("example.org","/protectedDir/index.html",1,"anyone","myPass")){
    echo "Authenticated!";
}else echo "Auth failed..."
?>

Buchcover