ActiveDirectoryのユーザ一覧を取得するPHPスクリプト

投稿日: 2007年04月14日 更新日: 2017年07月22日

ActiveDirectoryのユーザ一覧を取得するPHPスクリプト。

PHPのLDAPサポートが有効になっている必要があります。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ActiveDirectoryユーザ取得結果</title>
</head>
<body>
<h1>取得結果</h1>
<?php

$server = 'ldap://ad.example.com';
$username = 'user@example.com';
$password = 'pass';
$dn = "OU=hoge,DC=example,DC=com";

// コネクト
$ad = @ldap_connect($server);
if(!$ad){
    echo "Couldn't connect to AD!";
    exit;
}

// バインド
$bd = @ldap_bind($ad, $username, $password);
if(!$bd){
    echo "Couldn't bind to AD!";
    exit;
}

// 検索
$filter = "(cn=*)";
$result = @ldap_search($ad, $dn, $filter);
if(!$result){
    echo "search error!";
    exit;
}

// オプションを設定
ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ad, LDAP_OPT_REFERRALS, 0);

$entries = ldap_get_entries($ad, $result);

echo "現在のユーザ数は" . $entries["count"] . "人です。<br />";

$accounts = array();
for ($i=0; $i < $entries["count"]; $i++){
    $display_name = mb_convert_encoding($entries[$i]["displayname"][0], mb_internal_encoding(), 'SJIS');
    $account_name = $entries[$i]['samaccountname'][0];

    echo $display_name . " " . $account_name . "<br />\n";
}

?>
</body>
</html>

この記事へのコメント

※ このコメントは旧ブログシステム(tDiary)からの移行です。

テストさんからのコメント(2007-04-14 18:55:42)

テストツッコミ

名前:宮内 はじめ

Code for Nagoya名誉代表

E2D3名古屋支部長

プログラマーです。GISやデータビズが好きです。このサイトは宮内の個人的なメモです。

プロフィール

お問い合わせ