If then on php and LDAP

Posted on 2011-05-09
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=''></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
    LVL 51

    Accepted Solution

    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

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

    Author Comment

    no your right that worked.

    Author Closing Comment

    works great

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    ADFS Queries 3 23
    AD FS DNS 4 23
    generate a dynamic mysql query 2 23
    php explode 3 28
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    This article is very specific and is only intended to help if you are installing Dreamweaver 8 in a Windows 7 environment with Office 2007 installed.   I'm not sure why Microsoft tends to release OS' that should not be released but they do.  Windows…
    This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
    This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now