Hi has anyone seen an error like this before I believe it has to do with logging into a page using domain credentials but our logins have been converted to exchange online (PHP Fatal error: Uncaught TypeError: ldap_get_dn(): Argument #2 ($entry) must be of type LDAP\ResultEntry, bool given in /var/www/html/folder/external/ldap.php:26\nStack trace:\n#0 /var/www/html/folder/external/ldap.php(26): ldap_get_dn()\n#1 /var/www/html/folder/login.php(141): _ldap_login()\n#2 {main}\n thrown in /var/www/html/folder/external/ldap.php on line 26,)

It sounds like the $entry variable that was passed in as the second parameter of the ldap_get_dn() function in ldap.php is most likely FALSE. This typically happens when the function that was used to populate $entry failed and returned false.

commented: Thanks for responding! So here is lines 25 and 26 the $first entry is failing $first = ldap_first_entry($ldap, $res); $data = ldap_get_dn($ldap, $fi +0

Yes, I understand that’s what’s failing, but it’s the second parameter being passed into that function. I can’t help unless I see the complete ldap.php file.

<?php
/*
error_reporting(E_ALL);
ini_set('display_errors', '1');
*/
/*xxxxx.xx.xxx.xxxxx*/
function _ldap_login($user, $password, $samuid)
{
    ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);
    $ldap = ldap_connect("xx.xx.xx.xx") or die("Could not connect to ldap host");
    if ($ldap)
    {
        $username = $user;
        ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
        ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
        define('LDAP_OPT_DIAGNOSTIC_MESSAGE', 0x0032);
        if ($bind = @ldap_bind($ldap, $username, $password))
        {
            $login_status = 1;
            $login_err = "";

            $filter="(samaccountname=$samuid)";
            $dn="OU=xxxx,OU=United States Users & Workstations,DC=xx,DC=xxx,DC=xxxxx";
            $res = ldap_search($ldap, $dn, $filter);
            $first = ldap_first_entry($ldap, $res);
            $data = ldap_get_dn($ldap, $first);
            //echo "<br /><br />OU= $data<br /><br />";
            switch (true)
            {
                case strpos($data, 'OU=XXXX') > 0:
                    echo '<br /><br />Welcome XXXX DC user.<br /><br />';
                break;
                case strpos($data, 'OU=XXXX') > 0;
                echo '<br /><br />Welcome YYYY DC user<br /><br />';
                break;
                case strpos($data, 'OU=XXXX') > 0:
                    echo '<br /><br />Welcome ZZZZ DC user.<br /><br />';
                break;
                default:
                echo "<br /><br />I don't know where you're from.<br /><br />";
            }

        }
        else
        {
            // error message
            $login_status = 0;
            ldap_get_option($ldap, LDAP_OPT_DIAGNOSTIC_MESSAGE, $extended_error);
            $ldapcode = explode(",", $extended_error);
            $ldaperr = trim($ldapcode[2]);
            //*echo "ldaperr = $ldaperr<br />";
            switch ($ldaperr)
            {
                case "data 773":
                    $login_err = "Password Must Be Changed";
?>
                <SCRIPT LANGUAGE="JavaScript">
                <!-- Start Hiding the Script
                jlert = alert("Your domain password is set to require a change at the next logon.\nPlease log off and then back on to your computer to complete this.");
                // Stop Hiding script --->
                </SCRIPT>
<?php
                    break;
                case "data 775":
                    $login_err = "Domain Account Locked";
?>
                <SCRIPT LANGUAGE="JavaScript">
                <!-- Start Hiding the Script
                jlert = alert("Your domain account appears to be locked.\nPlease open a support ticket to have this resolved.");
                // Stop Hiding script --->
                </SCRIPT>
<?php
                    break;
                case "data 701":
                    $login_err = "Domain Account Expired";
?>
                <SCRIPT LANGUAGE="JavaScript">
                <!-- Start Hiding the Script
                jlert = alert("Your domain account appears to have expired.\nPlease open a support ticket to have this resolved.");
                // Stop Hiding script --->
                </SCRIPT>
<?php
                    break;
                case "data 532":
                    $login_err = "Domain Password Expired";
?>
                <SCRIPT LANGUAGE="JavaScript">
                <!-- Start Hiding the Script
                jlert = alert("Your domain passowrd appears to have expired.\nPlease open a support ticket to have this resolved.");
                // Stop Hiding script --->
                </SCRIPT>
<?php
                    break;
                default:
                    $login_err = "Invalid login credentials";
                    break;
            }
        }
        //*print "LDAP Return: " . ldap_errno( $ldap )  . " " . ldap_error( $ldap ) . "<br />";
        if (isset($extended_error))
        {
        //*print "LDAP Extended Return: $extended_error<br />";
        }
        $login_result = array($login_status, $login_err);
        $ldap = ldap_unbind($ldap);
        return($login_result);
    }
}
?>

When you removed the comment on the line

//echo "<br /><br />OU= $data<br /><br />";

What did it say the value of $data is?

If it doesn’t show anything, try var_dump($data);

commented: Here is the value $first = ldap_first_entry($ldap, $res); +0

Here is the value

$first = ldap_first_entry($ldap, $res);
$data = ldap_get_dn($ldap, $first);

No, what I mean is, if you have something like:

$res = ldap_search($ldap, $dn, $filter);
$first = ldap_first_entry($ldap, $res);
$data = ldap_get_dn($ldap, $first);

var_dump($res);
var_dump($first);
var_dump($data);

When you run that PHP code, what does var_dump() spit out? It looks as if ldap_first_entry() is returning FALSE so we want to investigate why that is.

I'm sorry I am not sure where to place the code you mentioned above?

Lines 24-27 in the code you posted above.

So it should look like this?

 $res = ldap_search($ldap, $dn, $filter);
 $first = ldap_first_entry($ldap, $res);
 $data = ldap_get_dn($ldap, $first);

  var_dump($res);
  var_dump($first);
  var_dump($data);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.