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.

function httpauth($host,$authurl,$ssl = 0, $user, $pass){
        $port = 443;
        $prefix = "ssl://";
        $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));
    if($ret == "HTTP/1.1 200 OK") return true;
    else return false;

    echo "Authenticated!";
}else echo "Auth failed..."