Connexion active directory

Fermé
yossra - 31 août 2020 à 12:07
yg_be Messages postés 22717 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 22 avril 2024 - 14 sept. 2020 à 14:27
Bonjour,

je veux connecter a l'active directory avec windows 10
pour collecter les informations sur les personnels connectées mais je ne connais pas comment je fais cette connexion


Configuration: Windows / Edge 84.0.522.63

29 réponses

cbon je résous ce probleme
0
<?php
//Affichage erreurs php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

// Constantes et autres variables
define('USER','*****');
define('PASSWORD','******');
$message='';
$url_page2 = "page2.php"; // variable à modifier selon tes besoins

// récupération propre des variables (POST/GET/SESSION...) AVANT de les utiliser
$user = !empty($_POST['user']) ? trim($_POST['user']) : NULL;
$password = !empty($_POST['password']) ? trim($_POST['password']) : NULL;

  //traitement du submit
if(isset($_POST['submit'])){
  if(!$user  || !$password){
    $message='veuillez indiquer vos identifiants !';
  }elseif($user != USER || $password != PASSWORD ){
    $message='Vos identifiants sont faux. Merci de les vérifier';
  }elseif($user == USER || $password = PASSWORD){
     // tout est ok... donc :
     header('location:'.$url_page2);
     exit; // toujours mettre un exit après une redirection
  }
}

?>
<!DOCTYPE html>
<html lang="fr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    * {
      box-sizing: border-box;
    }

    body {
      margin: 0;
    }

    /* Style the header */
    .header {
      background-color:white;
      padding: 50px;
      text-align: center;
    }

    /* Style the top navigation bar */
    .topnav {
      overflow: hidden;
      background-color: white;
    }
    /* Create three unequal columns that floats next to each other */
    .column {
      float: left;
      padding: 70px;
    }

    /* Left and right column */
    .column.side {
      width: 25%;
      
    }

    /* Middle column */
    .column.middle {
      width: 50%;
      background-color:rgb(0, 100, 255);
    }

    /* Clear floats after the columns */
    .row:after {
      content: "";
      display: table;
      clear: both;
    }
    /* Style the footer */
    .footer {
      background-color: white;
      padding: 40px;
      text-align: center;
    }

    /* Responsive layout - makes the three columns stack on top of each other instead of next to each other */
    @media screen and (max-width: 600px) {
      .column.side, .column.middle {
        width: 100%;
      }
    }
    
    #btn_submit{
      padding-left:60px;
    }
    
    #div_error{
      margin-left:80px;
    }
    
    </style>
  </head>
  <body>
    <div class="header">
     
    </div>
    <div class="topnav">
      
    </div>
    <div class="row">
      <div class="column side">
    </div>
    <div class="column middle">
      <center>
      <h1 style="border:3px solid #ff6347;">welcome</h1>
      <br>
      <form action="" method="post">
        <strong><im><h3>
        <label for="username" >Username: </label>
        <input id="user" type="text" name="user" Size="20" value="<?php echo htmlspecialchars($user);?>"/> 
        <br><br>
        <label for="password" >Password: </label>
        <input id="user" type="text" name="password" Size="20" value="<?php echo htmlspecialchars($password);?>"/>
        <br><br>
        <input type="submit" id="btn_submit" name="submit" value="Submit" />
      </form>

      <?php 
      if(!empty($message)){
        echo '<div id="div_error">';
        echo '<p style="color:red;">'.$message.'</p>';
        echo '</div>';
      }
      ?>
    </div>
    <div class="column side">
       
    </div>
    <div class="footer">
     
    </div>
  </div>

  </body>
</html>
0
page2:
<?php
$user = !empty($_POST['user']) ? $_POST['user'] : NULL;
?>
<html lang="en">
<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
  box-sizing: border-box;
}

body {
  margin: 0;
}

/* Style the header */
.header {
  background-color:white;
  padding: 50px;
  text-align: center;
}

/* Style the top navigation bar */
.topnav {
  overflow: hidden;
  background-color: white;
}





/* Create three unequal columns that floats next to each other */
.column {
  float: left;
  padding: 70px;
}

/* Left and right column */
.column.side {
  width: 25%;
  
}

/* Middle column */
.column.middle {
  width: 50%;
  background-color:rgb(0, 100, 255);
}

/* Clear floats after the columns */
.row:after {
  content: "";
  display: table;
  clear: both;
}
/* Style the footer */
.footer {
  background-color: white;
  padding: 40px;
  text-align: center;
}

/* Responsive layout - makes the three columns stack on top of each other instead of next to each other */
@media screen and (max-width: 600px) {
  .column.side, .column.middle {
    width: 100%;
  }
}
</style>
</head>
<body>

<div class="header">
 
</div>

<div class="topnav">
  
</div>

<div class="row">
  <div class="column side">
   
  </div>
  
  <div class="column middle">
  <center>
 <br>
  <form action="" method="POST">
<strong><im><h3><label for="username" >Username:  </label><input id="username" type="text" name="user" Size="20" /> 

<br><br><br><?php echo"  ";echo"  ";echo"  ";echo"  ";echo"  ";echo"  ";echo"  "; echo"  ";echo"  ";?>
<input type="submit" name="submit" value="Submit" />
    </form>

  </div>
  
  <div class="column side">
    
  </div>
</div>
<div class="footer">
 
</div>

  
</body>
</html>
0
page3:
<?php
 
//LDAP Bind paramters, need to be a normal AD User account.
$ldap_password = '*****';
$ldap_username = '******l';
$ldap_connection = ldap_connect("******");
 
if (FALSE === $ldap_connection){
    // Uh-oh, something is wrong...
 echo 'Unable to connect to the ldap server';
}
 
// We have to set this option for the version of Active Directory we are using.
ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3) or die('Unable to set LDAP protocol version');
ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0); // We need this for doing an LDAP search.
 
if (TRUE === ldap_bind($ldap_connection, $ldap_username, $ldap_password)){
 
 //Your domains DN to query
    $ldap_base_dn = 'DC=*****,DC=******';
  
 //Get standard users and contacts
    $search_filter ='CN=3363';
  
 //Connect to LDAP
 $result = ldap_search($ldap_connection, $ldap_base_dn, $search_filter);
  
    if (FALSE !== $result){
 $entries = ldap_get_entries($ldap_connection, $result);
  
 // Uncomment the below if you want to write all entries to debug somethingthing 
 //var_dump($entries);
  
 //Create a table to display the output 
 echo '<h2>AD User Results</h2></br>';
 echo '<table border = "1"><tr bgcolor="#cccccc"><td>Username</td><td>Last Name</td><td>First Name</td><td>Company</td><td>Department</td><td>Office Phone</td><td>Fax</td><td>Mobile</td><td>DDI</td><td>E-Mail Address</td><td>Home Phone</td></tr>';
  
 //For each account returned by the search
 for ($x=0; $x<$entries['count']; $x++){
  
 //
 //Retrieve values from Active Directory
 //
  
 //Windows Usernaame
 $LDAP_samaccountname = "";
  
 if (!empty($entries[$x]['samaccountname'][0])) {
 $LDAP_samaccountname = $entries[$x]['samaccountname'][0];
 if ($LDAP_samaccountname == "NULL"){
 $LDAP_samaccountname= "";
 }
 } else {
 //#There is no samaccountname s0 assume this is an AD contact record so generate a unique username
  
 $LDAP_uSNCreated = $entries[$x]['usncreated'][0];
 $LDAP_samaccountname= "CONTACT_" . $LDAP_uSNCreated;
 }
  
 //Last Name
 $LDAP_LastName = "";
  
 if (!empty($entries[$x]['sn'][0])) {
 $LDAP_LastName = $entries[$x]['sn'][0];
 if ($LDAP_LastName == "NULL"){
 $LDAP_LastName = "";
 }
 }
  
 //First Name
 $LDAP_FirstName = "";
  
 if (!empty($entries[$x]['givenname'][0])) {
 $LDAP_FirstName = $entries[$x]['givenname'][0];
 if ($LDAP_FirstName == "NULL"){
 $LDAP_FirstName = "";
 }
 }
  
 //Company
 $LDAP_CompanyName = "";
  
 if (!empty($entries[$x]['company'][0])) {
 $LDAP_CompanyName = $entries[$x]['company'][0];
 if ($LDAP_CompanyName == "NULL"){
 $LDAP_CompanyName = "";
 }
 }
  
 //Department
 $LDAP_Department = "";
  
 if (!empty($entries[$x]['department'][0])) {
 $LDAP_Department = $entries[$x]['department'][0];
 if ($LDAP_Department == "NULL"){
 $LDAP_Department = "";
 }
 }
  
 //Job Title
 $LDAP_JobTitle = "";
  
 if (!empty($entries[$x]['title'][0])) {
 $LDAP_JobTitle = $entries[$x]['title'][0];
 if ($LDAP_JobTitle == "NULL"){
 $LDAP_JobTitle = "";
 }
 }
  
 //IPPhone
 $LDAP_OfficePhone = "";
  
 if (!empty($entries[$x]['ipphone'][0])) {
 $LDAP_OfficePhone = $entries[$x]['ipphone'][0];
 if ($LDAP_OfficePhone == "NULL"){
 $LDAP_OfficePhone = "";
 }
 }
  
 //FAX Number
 $LDAP_OfficeFax = "";
  
 if (!empty($entries[$x]['facsimiletelephonenumber'][0])) {
 $LDAP_OfficeFax = $entries[$x]['facsimiletelephonenumber'][0];
 if ($LDAP_OfficeFax == "NULL"){
 $LDAP_OfficeFax = "";
 }
 }
  
 //Mobile Number
 $LDAP_CellPhone = "";
  
 if (!empty($entries[$x]['mobile'][0])) {
 $LDAP_CellPhone = $entries[$x]['mobile'][0];
 if ($LDAP_CellPhone == "NULL"){
 $LDAP_CellPhone = "";
 }
 }
  
 //Telephone Number
 $LDAP_DDI = "";
  
 if (!empty($entries[$x]['telephonenumber'][0])) {
 $LDAP_DDI = $entries[$x]['telephonenumber'][0];
 if ($LDAP_DDI == "NULL"){
 $LDAP_DDI = "";
 }
 }
  
 //Email address
 $LDAP_InternetAddress = "";
  
 if (!empty($entries[$x]['mail'][0])) {
 $LDAP_InternetAddress = $entries[$x]['mail'][0]; 
 if ($LDAP_InternetAddress == "NULL"){
 $LDAP_InternetAddress = "";
 }
 }
  
 //Home phone
 $LDAP_HomePhone = "";
  
 if (!empty($entries[$x]['homephone'][0])) {
 $LDAP_HomePhone = $entries[$x]['homephone'][0];
 if ($LDAP_HomePhone == "NULL"){
 $LDAP_HomePhone = "";
 }
 }
  
 echo "<tr><td><strong>" . $LDAP_samaccountname ."</strong></td><td>" .$LDAP_LastName."</td><td>".$LDAP_FirstName."</td><td>".$LDAP_CompanyName."</td><td>".$LDAP_Department."</td><td>".$LDAP_OfficePhone."</td><td>".$LDAP_OfficeFax."</td><td>".$LDAP_CellPhone."</td><td>".$LDAP_DDI."</td><td>".$LDAP_InternetAddress."</td><td>".$LDAP_HomePhone."</td></tr>";
 
  
 } //END for loop
 } //END FALSE !== $result
  
 ldap_unbind($ldap_connection); // Clean up after ourselves.
 echo("</table>"); //close the table
 
} //END ldap_bind
 
?>
0
yg_be Messages postés 22717 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 22 avril 2024 1 476
9 sept. 2020 à 16:40
très bien! pour finir, c'était tout simple, non?
peux-tu marquer la discussion comme résolue?
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
je veux faire la liaison entre les 3 pages je veux :
dans la page 2
losque j'entre Username la page 3 permet d'afficher les informations sur cet Username
0
ca marche merci
0
page1:
<?php
//Affichage erreurs php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

// Constantes et autres variables
define('USER','******');
define('PASSWORD','******');
$message='';
$url_page2 = "page2.php"; // variable à modifier selon tes besoins

// récupération propre des variables (POST/GET/SESSION...) AVANT de les utiliser
$user = !empty($_POST['user']) ? trim($_POST['user']) : NULL;
$password = !empty($_POST['password']) ? trim($_POST['password']) : NULL;

  //traitement du submit
if(isset($_POST['submit'])){
  if(!$user  || !$password){
    $message='veuillez indiquer vos identifiants !';
  }elseif($user != USER || $password != PASSWORD ){
    $message='Vos identifiants sont faux. Merci de les vérifier';
  }elseif($user == USER || $password = PASSWORD){
     // tout est ok... donc :
     header('location:'.$url_page2);
     exit; // toujours mettre un exit après une redirection
  }
}

?>
<!DOCTYPE html>
<html lang="fr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    * {
      box-sizing: border-box;
    }

    body {
      margin: 0;
    }

    /* Style the header */
    .header {
      background-color:white;
      padding: 50px;
      text-align: center;
    }

    /* Style the top navigation bar */
    .topnav {
      overflow: hidden;
      background-color: white;
    }
    /* Create three unequal columns that floats next to each other */
    .column {
      float: left;
      padding: 70px;
    }

    /* Left and right column */
    .column.side {
      width: 25%;
      
    }

    /* Middle column */
    .column.middle {
      width: 50%;
      background-color:rgb(0, 100, 255);
    }

    /* Clear floats after the columns */
    .row:after {
      content: "";
      display: table;
      clear: both;
    }
    /* Style the footer */
    .footer {
      background-color: white;
      padding: 40px;
      text-align: center;
    }

    /* Responsive layout - makes the three columns stack on top of each other instead of next to each other */
    @media screen and (max-width: 600px) {
      .column.side, .column.middle {
        width: 100%;
      }
    }
    
    #btn_submit{
      padding-left:60px;
    }
    
    #div_error{
      margin-left:80px;
    }
    
    </style>
  </head>
  <body>
    <div class="header">
     
    </div>
    <div class="topnav">
      
    </div>
    <div class="row">
      <div class="column side">
    </div>
    <div class="column middle">
      <center>
      <h1 style="border:3px solid #ff6347;">welcome</h1>
      <br>
      <form action="" method="post">
        <strong><im><h3>
        <label for="username" >Username: </label>
        <input id="user" type="text" name="user" Size="20" value="<?php echo htmlspecialchars($user);?>"/> 
        <br><br>
        <label for="password" >Password: </label>
        <input id="user" type="text" name="password" Size="20" value="<?php echo htmlspecialchars($password);?>"/>
        <br><br>
        <input type="submit" id="btn_submit" name="submit" value="Submit" />
      </form>

      <?php 
      if(!empty($message)){
        echo '<div id="div_error">';
        echo '<p style="color:red;">'.$message.'</p>';
        echo '</div>';
      }
      ?>
    </div>
    <div class="column side">
       
    </div>
    <div class="footer">
     
    </div>
  </div>

  </body>
</html>
0
page2:
<?php

$user = !empty($_POST['user']) ? $_POST['user'] : NULL;
?>
<html lang="en">
<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
  box-sizing: border-box;
}

body {
  margin: 0;
}

/* Style the header */
.header {
  background-color:white;
  padding: 50px;
  text-align: center;
}

/* Style the top navigation bar */
.topnav {
  overflow: hidden;
  background-color: white;
}





/* Create three unequal columns that floats next to each other */
.column {
  float: left;
  padding: 70px;
}

/* Left and right column */
.column.side {
  width: 25%;
  
}

/* Middle column */
.column.middle {
  width: 50%;
  background-color:rgb(0, 100, 255);
}

/* Clear floats after the columns */
.row:after {
  content: "";
  display: table;
  clear: both;
}
/* Style the footer */
.footer {
  background-color: white;
  padding: 40px;
  text-align: center;
}

/* Responsive layout - makes the three columns stack on top of each other instead of next to each other */
@media screen and (max-width: 600px) {
  .column.side, .column.middle {
    width: 100%;
  }
}
</style>
</head>
<body>

<div class="header">
 
</div>

<div class="topnav">
  
</div>

<div class="row">
  <div class="column side">
   
  </div>
  
  <div class="column middle">
  <center>
 <br>
  <form action="" method="POST">
<strong><im><h3><label for="username" >Username:  </label><input id="username" type="text" name="user" Size="20" /> 

<br><br><br><?php echo"  ";echo"  ";echo"  ";echo"  ";echo"  ";echo"  ";echo"  "; echo"  ";echo"  ";?>
<input type="submit" name="submit" value="Submit" />
    </form>

  </div>
  
  <div class="column side">
    
  </div>
</div>
<div class="footer">
 
</div>

  
</body>
</html>
0
page3:
<?php
 
//LDAP Bind paramters, need to be a normal AD User account.
$ldap_password = 'Stage2020';
$ldap_username = '***@***';
$ldap_connection = ldap_connect("onas.local");
 
if (FALSE === $ldap_connection){
    // Uh-oh, something is wrong...
 echo 'Unable to connect to the ldap server';
}
 
// We have to set this option for the version of Active Directory we are using.
ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3) or die('Unable to set LDAP protocol version');
ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0); // We need this for doing an LDAP search.
 
if (TRUE === ldap_bind($ldap_connection, $ldap_username, $ldap_password)){
 
 //Your domains DN to query
    $ldap_base_dn = 'DC=onas,DC=local';
  
 //Get standard users and contacts
    $search_filter ='CN=3363';
  
 //Connect to LDAP
 $result = ldap_search($ldap_connection, $ldap_base_dn, $search_filter);
  
    if (FALSE !== $result){
 $entries = ldap_get_entries($ldap_connection, $result);
  
 // Uncomment the below if you want to write all entries to debug somethingthing 
 //var_dump($entries);
  
 //Create a table to display the output 
 echo '<h2>AD User Results</h2></br>';
 echo '<table border = "1"><tr bgcolor="#cccccc"><td>Username</td><td>Last Name</td><td>First Name</td><td>Company</td><td>Department</td><td>Office Phone</td><td>Fax</td><td>Mobile</td><td>DDI</td><td>E-Mail Address</td><td>Home Phone</td></tr>';
  
 //For each account returned by the search
 for ($x=0; $x<$entries['count']; $x++){
  
 //
 //Retrieve values from Active Directory
 //
  
 //Windows Usernaame
 $LDAP_samaccountname = "";
  
 if (!empty($entries[$x]['samaccountname'][0])) {
 $LDAP_samaccountname = $entries[$x]['samaccountname'][0];
 if ($LDAP_samaccountname == "NULL"){
 $LDAP_samaccountname= "";
 }
 } else {
 //#There is no samaccountname s0 assume this is an AD contact record so generate a unique username
  
 $LDAP_uSNCreated = $entries[$x]['usncreated'][0];
 $LDAP_samaccountname= "CONTACT_" . $LDAP_uSNCreated;
 }
  
 //Last Name
 $LDAP_LastName = "";
  
 if (!empty($entries[$x]['sn'][0])) {
 $LDAP_LastName = $entries[$x]['sn'][0];
 if ($LDAP_LastName == "NULL"){
 $LDAP_LastName = "";
 }
 }
  
 //First Name
 $LDAP_FirstName = "";
  
 if (!empty($entries[$x]['givenname'][0])) {
 $LDAP_FirstName = $entries[$x]['givenname'][0];
 if ($LDAP_FirstName == "NULL"){
 $LDAP_FirstName = "";
 }
 }
  
 //Company
 $LDAP_CompanyName = "";
  
 if (!empty($entries[$x]['company'][0])) {
 $LDAP_CompanyName = $entries[$x]['company'][0];
 if ($LDAP_CompanyName == "NULL"){
 $LDAP_CompanyName = "";
 }
 }
  
 //Department
 $LDAP_Department = "";
  
 if (!empty($entries[$x]['department'][0])) {
 $LDAP_Department = $entries[$x]['department'][0];
 if ($LDAP_Department == "NULL"){
 $LDAP_Department = "";
 }
 }
  
 //Job Title
 $LDAP_JobTitle = "";
  
 if (!empty($entries[$x]['title'][0])) {
 $LDAP_JobTitle = $entries[$x]['title'][0];
 if ($LDAP_JobTitle == "NULL"){
 $LDAP_JobTitle = "";
 }
 }
  
 //IPPhone
 $LDAP_OfficePhone = "";
  
 if (!empty($entries[$x]['ipphone'][0])) {
 $LDAP_OfficePhone = $entries[$x]['ipphone'][0];
 if ($LDAP_OfficePhone == "NULL"){
 $LDAP_OfficePhone = "";
 }
 }
  
 //FAX Number
 $LDAP_OfficeFax = "";
  
 if (!empty($entries[$x]['facsimiletelephonenumber'][0])) {
 $LDAP_OfficeFax = $entries[$x]['facsimiletelephonenumber'][0];
 if ($LDAP_OfficeFax == "NULL"){
 $LDAP_OfficeFax = "";
 }
 }
  
 //Mobile Number
 $LDAP_CellPhone = "";
  
 if (!empty($entries[$x]['mobile'][0])) {
 $LDAP_CellPhone = $entries[$x]['mobile'][0];
 if ($LDAP_CellPhone == "NULL"){
 $LDAP_CellPhone = "";
 }
 }
  
 //Telephone Number
 $LDAP_DDI = "";
  
 if (!empty($entries[$x]['telephonenumber'][0])) {
 $LDAP_DDI = $entries[$x]['telephonenumber'][0];
 if ($LDAP_DDI == "NULL"){
 $LDAP_DDI = "";
 }
 }
  
 //Email address
 $LDAP_InternetAddress = "";
  
 if (!empty($entries[$x]['mail'][0])) {
 $LDAP_InternetAddress = $entries[$x]['mail'][0]; 
 if ($LDAP_InternetAddress == "NULL"){
 $LDAP_InternetAddress = "";
 }
 }
  
 //Home phone
 $LDAP_HomePhone = "";
  
 if (!empty($entries[$x]['homephone'][0])) {
 $LDAP_HomePhone = $entries[$x]['homephone'][0];
 if ($LDAP_HomePhone == "NULL"){
 $LDAP_HomePhone = "";
 }
 }
  
 echo "<tr><td><strong>" . $LDAP_samaccountname ."</strong></td><td>" .$LDAP_LastName."</td><td>".$LDAP_FirstName."</td><td>".$LDAP_CompanyName."</td><td>".$LDAP_Department."</td><td>".$LDAP_OfficePhone."</td><td>".$LDAP_OfficeFax."</td><td>".$LDAP_CellPhone."</td><td>".$LDAP_DDI."</td><td>".$LDAP_InternetAddress."</td><td>".$LDAP_HomePhone."</td></tr>";
 
  
 } //END for loop
 } //END FALSE !== $result
  
 ldap_unbind($ldap_connection); // Clean up after ourselves.
 echo("</table>"); //close the table
 
} //END ldap_bind
 
?>
0
yg_be Messages postés 22717 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 22 avril 2024 1 476
14 sept. 2020 à 14:27
peux-tu marquer la discussion comme résolue?
0