pulling records from AD

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

pulling records from AD

Post by Obadiah »

I recenntly downloaded PHP LDAP CLASS FOR MANIPULATING ACTIVE DIRECTORY Version 3.3 and I got the authenticate test (the test allows you to enter a username and password in a input box and log in based on your username and password in AD)to work. I have never used this before and I am confused as to how to pull records out of AD.

what I have tried is this

Code: Select all

<?php
$ldaprdn = 'username for ad';
$ldappass = 'password for ad';
// connect to ldap server
$ldapconn = ldap_connect("localhost")
    or die("Could not connect to LDAP server.");

if ($ldapconn) {

    // binding to ldap server
    $ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);

    // verify binding
    if ($ldapbind) {
        echo "LDAP bind successful...";
    } else {
        echo "LDAP bind failed...";
    }

}
//*****I have also tried this*****
$ldapconn = ldap_connect("localhost")
    or die("Could not connect to LDAP server.");

if ($ldapconn) {

    // binding anonymously
    $ldapbind = ldap_bind($ldapconn);

    if ($ldapbind) {
        echo "LDAP bind anonymous successful...";
    } else {
        echo "LDAP bind anonymous failed...";
    }

}
?>
As of right now I cant even get past the binding. I an error that states Warning: ldap_bind() [function.ldap-bind]: Unable to bind to server: Can't contact LDAP server in C:\index.php on line 11
LDAP bind anonymous failed...

I am usig xampp if that matters. Any help on this issue would be more than greatly apprieciated
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: pulling records from AD

Post by mikosiko »

are you sure that your LDAP server (or AD) is in "localhost" ?
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Re: pulling records from AD

Post by Obadiah »

even when I changed the name of the Ldap server it would not bind im using xampp if that has anything to do with it....is it possible however, to go from that sort of a login and connect it to a page using no ldap connections at all? I figured what I could do is have the user log in so they didnt have to register a new password but pull their information out of a MySQL database based on the users username. here is what i have in both scripts

Code: Select all

<?php
//log them out
$logout=$_GET['logout'];
if ($logout=="yes"){ //destroy the session
	session_start();
	$_SESSION = array();
	session_destroy();
}

//force the browser to use ssl (STRONGLY RECOMMENDED!!!!!!!!)
if ($_SERVER["SERVER_PORT"]!=443){ header("Location: https://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']); exit(); }

//you should look into using PECL filter or some form of filtering here for POST variables
$username=strtoupper($_POST["username"]); //remove case sensitivity on the username
$password=$_POST["password"];
$formage=$_POST["formage"];

if ($_POST["oldform"]){ //prevent null bind

	if ($username!=NULL && $password!=NULL){
		//include the class and create a connection
		include ("../adLDAP.php");
        try {
		    $adldap = new adLDAP();
        }
        catch (adLDAPException $e) {
            echo $e; exit();   
        }
		
		//authenticate the user
		if ($adldap -> authenticate($username,$password)){
			//establish your session and redirect
			session_start();
			$_SESSION["username"]=$username;
			$redir="Location: https://".$_SERVER['HTTP_HOST']."/gotofile.php";
			header($redir);
			//echo $username;
			exit;
		}
	}
	$failed=1;
}

?>

<html>
<head>
<title>adLDAP example</title>
</head>

<body>

This area is restricted.<br>
Please login to continue.<br>

<form method='post' action='<?php echo $_SERVER["PHP_SELF"]; ?>'>
<input type='hidden' name='oldform' value='1'>

Username: <input type='text' name='username' value='<?php echo ($username); ?>'><br>
Password: <input type='password' name='password'><br>
<br>

<input type='submit' name='submit' value='Submit'><br>
<?php if ($failed){ echo ("<br>Login Failed!<br><br>\n"); } ?>
</form>

<?php if ($logout=="yes") { echo ("<br>You have successfully logged out."); } ?>


</body>

</html>
redirects them to

Code: Select all

<?php
session_start();
function doDB()
{
	$conn = mysql_connect("localhost","username","password") or die(mysql_error());
	mysql_select_db("qmdb",$conn) or die(mysql_error());
	return $conn;
}

	$_SESSION["username"]=$username; //session seems to be ended
	echo $_SESSION["username"];// for some reason the username doesnt echo
	
	$query = mysql_query("SELECT * FROM admin WHERE user_id='$username'");
	$numrows = mysql_num_rows($query);
	if ($numrows!=0)
	{		
		while ($row = mysql_fetch_assoc($query))
		{
			$dbusername = $row['user_id'];
			$dbpassword = $row['user_pass'];
			$name = $row['fusername'];
		}
	}
$questnum = $_POST['questnum']; 
$how = "Enter number of Records your reporting on (1-10)";
include("txtdefine.inc");

?>

<html>
<head>
<title>testing</title>
</head>
<body>
<?php echo $display_block; ?>
<form action="qmform.php" method="post">
<input type="text" maxlength="2" size="2" name="questnum">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
it doesnt want to connect...i get
Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\xampp\htdocs

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs
is there a way to go about doin it this way or should i just scrap the whole AD idea?
Post Reply