Hi there,

I have a small problem. The following php pulls data from the ldap search. It all works fine but once the person has logged in they are greeted with a "hello John welcome to the site". The issue is that it prints a "1" after the john like so: "hello John1 welcome to the site"

The authenticate code:

// verify user and password
	if($bind = @ldap_bind($ldap, $user . $ldap_usr_dom, $password)) {

	// valid
	// check presence in groups
	$filter = "(sAMAccountName=" . $user . ")";
	$attr = array("memberof","cn","mail","givenName");
	$result = ldap_search($ldap, $ldap_dn, $filter, $attr); 

	$entries = ldap_get_entries($ldap, $result);
	ldap_unbind($ldap);

	// check groups
	foreach($entries[0]['memberof'] as $grps) {
		// is manager, break loop
		if (strpos($grps, $ldap_manager_group)) { $access = 2; break; }

		// is user
		if (strpos($grps, $ldap_user_group)) $access = 1;
	}

	if ($access != 0) {
		// establish session variables

		$_SESSION['user'] = $user;
		$_SESSION['access'] = $access;
                    $_SESSION['name'] = $entries[0]['cn'][0];
                    $_SESSION['email'] = $entries[0]['mail'][0];

                    $_SESSION['givenname'] = $entries[0]['givenname'][0];
		return true;

	} else {
		// user has no rights
		return false;

	}

} else {
	// invalid name or password
	return false;               
}
}

And this is the code that prints the persons name:


<?php echo print_r($_SESSION['givenname']);?>

Any help with this would be great 😉

    [man]print_r/man returns TRUE, so PHP is converting boolean TRUE to "1" as you requested.

      ahhh that makes sense. I will change it and come back to you either way

        also note, that strpos can return FALSE if the needle isn't in the haystack, but it can also return 0 if its at the first position. In this case, if(strpos()) will evaluate as false even though the string was found. Instead try comparing it strictly to FALSE like:

        if( strpos($grps, $ldap_user_group) !== FALSE )
          bradgrafelman;11038703 wrote:

          [man]print_r/man returns TRUE, so PHP is converting boolean TRUE to "1" as you requested.

          Hi Brad,

          I messed around with it a little and cant seem to remove the 1. Do you have any suggestions that may help me?

            Either don't echo the returned value, or don't call print_r() in the first place (since it seems there's no reason you should be doing so anyway).

              Chris, I think what you should be getting from brad's post is that this:

              echo print_r('');

              Produces the output '1'. This is because print_r('') sends an empty string to output and returns a 1 that it was successful, then echo takes the return value of 1 and sends it to output. You are calling multiple output methods when you clearly only want one.

                bradgrafelman;11038715 wrote:

                Either don't echo the returned value, or don't call print_r() in the first place (since it seems there's no reason you should be doing so anyway).

                Guys thank you so much for pointing that out. I can't believe I made a school boy error and had print and echo on the same line. I thing my eyes need a rest.

                Many thanks

                  And just to round off the notes for others reading this; print_r is a diagnostic function for examining variables, which is inappropriate for public-facing operation (they don't want to know about what types of variables you use in your script (at least you don't want them to want to know)).

                    Write a Reply...