If then on php and LDAP

Posted on 2011-05-09
Medium Priority
Last Modified: 2012-06-27
I have a php script that pull info out of LDAP it works great.

But the telephone number on the users are just the ext 9555

on line 218 is the ext that's being pulled from ldap

I want to have a if then that reads the ext and if it's a

95xx put 555-555-95xx

if it is a

96xx put 666-666-96xx

and if it is a

67xx put 777-777-67xx

and if it is anything else don't do anything to it.


$myUsername = user;

$myPassword = pass;

// specify the LDAP server to connect to
$conn = ldap_connect("") or die("Could not connect to server");

ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3);

$bind2 = ldap_bind( $conn, $myUsername, $myPassword );

    if ($bind2) {
       // echo "LDAP bind successful...<br>" ;
    } else {
        echo "LDAP bind failed...";

//this looks in the term OU
//$base_dn = 'OU=Terminated,DC=corp,DC=ceo,DC=org';

$base_dn = 'OU=User-Dept,DC=corp,DC=ceo,DC=org';


//This looks in the department in the AD and ONLY gettes the user is they have a department set up.

//This i took out the department so it will find all users even if they dont have a department set in AD.


$result = ldap_search($conn, $base_dn, $filter) or die ("ldap search error");

$info = ldap_get_entries($conn, $result);

//Puts the search in abc order from company then cm can be changed easly.

$attribs = array('givenname','cn','company');

for ($i=1; $i<$info["count"]; $i++)
$index = $info[$i];
do {

//create comparison variables from attributes:

$a = $b = null;
foreach($attribs as $attrib){
$a .= $info[$j-1][$attrib][0];
$b .= $index[$attrib][0];

// do the comparison

if ($a > $b){
$is_greater = true;
$info[$j] = $info[$j-1];
$j = $j-1;
$is_greater = false;
} while ($j>0 && $is_greater);

$info[$j] = $index;

echo "<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'></script>";

echo "<script type='text/javascript' src='js/jquery.magnifier.js'></script>";

echo "<form action='conect.php' method='POST'>";
echo "Look For: ";
echo "<select name='name'>";
echo "  <option value='cn' selected>First Name:</option>";
echo "  <option value='sn'>Last Name:</option>";
echo "  <option value='mail'>Email:</option>";
echo "  <option value='title'>Title:</option>";

echo "</select>";

echo " Type Here:  <input type='text'  name='Key-name' length='30'><br><br> Look in what Department: ";

echo "<select name='department'>";
echo "  <option value='Information Technology'>IT:</option>";
echo "  <option value='' selected>All:</option>";
echo "  <option value='Development'>Development:</option>";
echo "  <option value='Fiscal Services'>Fiscal Services:</option>";
echo "  <option value='department'>Citrix:</option>";
echo "  <option value='Human Resources'>HR:</option>";
echo "  <option value='department'>Marketing:</option>";
echo "  <option value='department'>Recepionists:</option>";
echo "  <option value='department'>Admissions:</option>";
echo "  <option value='Clinical Support'>Clinical Support:</option>";
echo "  <option value='department'>Education:</option>";
echo "  <option value='department'>Home Care:</option>";
echo "  <option value='department'>Home:</option>";
echo "  <option value='department'>Long Care:</option>";
echo "  <option value='department'>Medicial Doctors:</option>";
echo "  <option value='Patient Care Services'>Palliative Care:</option>";
echo "  <option value='department'>Pediatrics:</option>";

echo "</select> ";

echo "<input type='submit' name='submit' value = 'Search'>";

echo "</form>";

// iterate over array and print data for each entry
echo "<html>";
echo "<head>";

echo "<style type=text/css>";
echo "<!--.text {color: #000}-->";
echo "</style>";
echo "<body bgcolor=#6d7f3f>";

print "<table cellpadding='4' cellspacing='2' border='0' width='100%'>";

for ($i=0; $i<$info["count"]; $i++)
      // this code make the people display side by side.
	   print ($i==0?"<tr>":(!($i%2)?"</tr><tr>":""));

	   print "<td>";


	echo "<form action=manager.php method=POST >";
	echo "<input type=text  size='1' height='1' style='display:none' name=name  >";
	echo "<textarea type=text size='1' height='1'style='display:none' name=Key-name >". $info[$i]["manager"][0] ."</textarea> ";
	echo "<input type=text size='1' height='1' style='display:none' name=department2 value=><br> ";

	echo "<input type=text  size='1' height='1' style='display:none' name=name2 value=cn >";

	echo "<textarea type=text size='1' height='1'style='display:none' name=Key-name2 >". substr($info[$i]["manager"][0],3,stripos($info[$i]["manager"][0],",OU")-3)  ."</textarea> ";

	echo "<input type=text size='1' height='1' style='display:none' name=department value=> ";

	//echo "<input type=text size='1' height='1'  name=description value=". $info[$i]["description"][0] ."> ";

	echo "<table width=420  bgcolor=#cadba1 bordercolor=#000000 style=border-style:solid;color:#602860 align='center'>";

	echo "<td align=center width=130>";

											//echo '<pre>' . print_r($info[$i]['memberof'], true) . '</pre>';
									//echo "State is: ". $info[$i]["o"][0] ."<br>";
									//echo "samaccountname: ". $info[$i]["samaccountname"][0] ."<br>";
									//echo "mailNickname is: ". $info[$i]["mailnickname"][0] ."<br>";

							//echo "ou is: ". $info[$i]["ou"][0] ."<br>";

	echo "<img hight=100px width=100px boards=0 src=". $info[$i]["samaccountname"][0] .".jpg class='magnify' >"."<br>";
	echo "<span class=text>". $info[$i]["cn"][0] . "</span><br><strong> ". $info[$i]["description"][0] ."</strong><br>";

											//echo "Hair: ".  substr($info[$i]["whencreated"][0],0,stripos($info[$i]["whencreated"][0],",OU")-13) ."<br>";


	echo "<td  width=280>";

											//echo "city is: ". $info[$i]["l"][0] ."<br>";
		echo "<strong>Manager </strong>: <span class=text> ". substr($info[$i]["manager"][0],3,stripos($info[$i]["manager"][0],",OU")-3)  ."</a> <input type=submit name=submit value = Find></span><br>";
		echo "<strong>Title</strong>:<span class=text> ". $info[$i]["title"][0] ."</span><br>";

	//echo "<strong>Department</strong>: <span class=text>". $info[$i]["department"][0] ."</span><br/>";
	//echo "<strong>Company is</strong>: <span class=text>". $info[$i]["company"][0] ."</span><br>";

									//echo "Last Name is: ". $info[$i]["sn"][0] ."<br>";
											//echo "Last Name is: ". $info[$i]["description"][0] ."<br>";
											//echo "Given Name is: ". $info[$i]["givenname"][0] ."<br>";
											//echo "streetAddress is: ". $info[$i]["streetaddress"][0] ."<br>";
											//echo "Zip is: ". $info[$i]["postalcode"][0] ."<br>";
											//echo "st is: ". $info[$i]["st"][0] ."<br>";

	echo "<strong>Extension</strong>: <span class=text>" . $info[$i]["telephonenumber"][0] ."</span><br>";
	echo "<strong>Mobile number</strong>: <span class=text>". $info[$i]["mobile"][0] ."</span><br>";
	echo "<strong>Office Location</strong>:<span class=text> ". $info[$i]["physicaldeliveryofficename"][0] ."</span><br>";

											//echo "<strong>Member </strong>: <span class=text>". $info[$i]["memberof"] ."</span><br>";
											//echo "<strong>Members of</strong>: <span class=text>". substr($info[$i]["memberof"][0],3,stripos($info[$i]["memberof"][0],",OU")-3)  ."</span><br>";

	echo "<strong>Email address </strong>: <A href=mailto:".$info[$i]["mail"][0] .">".$info[$i]["mail"][0] ."</a>";

	//echo "<br>st is: ". $info[$i]["manager"][0] ."<br>";

	echo "</td>";
	echo "</table>";
	echo "</form>";

	print "</td>";


  print "</table>";

echo "</Body>";
echo "</html>";
echo "</head>";

// print number of entries found
echo "<p>";
echo "Number of entries found: " . ldap_count_entries($conn, $result) ."<p>";
//echo "filter".$filter.


Open in new window

Question by:mikearmas
  • 3
LVL 61

Accepted Solution

HainKurt earned 2000 total points
ID: 35722424
replace that line with

if (substr($info[$i]["telephonenumber"][0],0,2)=="95") echo "<strong>Extension</strong>: <span class=text>555-555-" . $info[$i]["telephonenumber"][0] ."</span><br>";
if (substr($info[$i]["telephonenumber"][0],0,2)=="96") echo "<strong>Extension</strong>: <span class=text>666-666-" . $info[$i]["telephonenumber"][0] ."</span><br>";
if (substr($info[$i]["telephonenumber"][0],0,2)=="67") echo "<strong>Extension</strong>: <span class=text>777-777-" . $info[$i]["telephonenumber"][0] ."</span><br>";

Author Comment

ID: 35722600
I see the logic but it doesn't display anything and doesn't display any errors.

Author Comment

ID: 35722621
no your right that worked.

Author Closing Comment

ID: 35722624
works great

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Group policies can be applied selectively to specific devices with the help of groups. Utilising this, it is possible to phase-in group policies, over a period of time, by randomly adding non-members user or computers at a set interval, to a group f…
It’s time for spooky stories and consuming way too much sugar, including the many treats we’ve whipped for you in the world of tech. Check it out!
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question