PHP get user infomation from window AD

<?php
	error_reporting(0);
	$errorMessage = false;
	$server = 'ldap://192.168.0.11';
	$dn = 'dc=next.com';//'CN=Users,DC=next.com,DC=com';
	$bind_user = '[email protected]';
	$bind_pass = 'test123';
	$d = $_SERVER['LOGON_USER'] ;
	echo $d;
	if(isset($_POST['login'])){
		$username = $_POST['username'];
		$password = $_POST['password'];
		$ldapconnect = ldap_connect($server,389);
		if(!$ldapconnect){
			$error_message = '<p>error: connection to server failed...</p>';
		}else{
			$ldapbind = ldap_bind($ldapconnect, $bind_user, $bind_pass);
			if(!$ldapbind){
				$errormessage = '<p>error: binding to server failed...</p>';
				echo "binding failed";
			}else{
				$filter = '(samaccountname='.$username.')';
				$ldapsearch = ldap_search($ldapconnect, $dn, $filter);
				if(!$ldapsearch){
					$errormessage = '<p>error: search on server failed...</p>';
					echo "search failed";
				}else{
					echo "search passed";
					$userdetails = ldap_get_entries($ldapconnect,$ldapsearch);
					if(!$userdetails){
						$errormessage = '<p>error: entries not retreived...</p>';
						echo "entries not retreived";
					}
					if($userdetails["count"] == 0){
						$errormessage = '<p>unknown user, please try again.</p>';
						echo "unknown user";
					}
					if($userdetails["count"] > 1){
						$errormessage = '<p>more then one such user. please report to it support</p>';
						echo $errormessage;
					}else{
						$info= ldap_get_entries($ldapconnect, $ldapsearch);
						$userDetails= ldap_get_entries($ldapconnect, $ldapsearch);
						$user_dn = $userDetails[0]["dn"];
						echo $user_dn;
						echo "<table border='1'>";
						for ($i=0; $i<$info["count"]; $i++) {
							print ("<TR>");
							print ("<TD width=15%>" . $info[$i]["cn"][0] . " " . $info[$i]["sn"][0]. "</TD>");
							print ("<TD width=85%>" . $info[$i]["mail"][0] . "</TD>");
							print ("<TD width=85%>" . $info[$i]["phno"][0] . "</TD>");
							print ("<TD>" . $info[$i]["dn"][0] . "</TD>");
							print ("</TR>");
						}
						echo "</table>";
						//echo "entries retreived";
						//echo $user_dn;
						echo $auth_password;
						$userBind = ldap_bind($ldapConnect, $user_dn, $password);
						if(!$userBind){
							$errorMessage = '<p>Invalid Username/Password!</p>';
						}else{
							// Do Something…
						}
					}
				}
			}
		}
		ldap_close($ldapConnect);
	} 
?><html>
<head>
</head>
<body>
	<div id="login">
	<h2>login</h2>
	<p>please enter your username and password.</p>
	<form method="post" action="ldap.php">
		<p>Username:</p>
		<input type="text" name="username" tabindex="1" />
		<p>Password:</p>
		<input type="password" name="password" tabindex="2" />
		<input type="submit" name="login" value="login" tabindex="3" />
	</form>
	<?php echo $errorMessage;?>
	</div>
</body>
</html>

 

你可能感兴趣的:(window)