我收到这种错误信息,知道第9行的代码有什么问题吗?基本上,代码是从表单中搜索用户输入,然后从ldap中搜索用户名。
PHP注意:未定义的索引:第9行的C:\ inetpub \ wwwroot \ LDAP \ New3 \ search2.php中的用户名
<?php
$ds = @ldap_connect("ldap.xxxxx.my");
if (!$ds) {
die("Unable to connect to test server.");
}
$res = @ldap_bind($ds); # Anonymous bind
$userid = $_POST['username']; // User key their userid or email
$srch = @ldap_search($ds,
"o=company, o=CompanyNet",
"uid=$userid", # Search on username
array('uid'), # We want username
0, # We want values and types (see docs)
10 # We want, at most, 10 results
);
if (ldap_errno($ds) == 4) { # Error code for "too many results"
print "<B>More than 10 results were returned. Only 10 displayed.</B><BR>\n";
}
if ($srch) {
$results = @ldap_get_entries($ds, $srch); # Retrieve all results
for ($i = 0; $i < $results["count"]; $i++) { # Iterate over all results
print "<B>".$results[$i]["uid"][0]."</B> exist in directory "."<BR>\n";
}
}
else {
print "<B>Directory lookup failed: ".ldap_error($ds)."</B><BR>\n";
}
@ldap_close($ds); # Close off my connection
?>
<html>
<head>
</head>
<body>
<form action="search.php" method="POST">
<input type="text" name="username" length="30">
<input type="submit" name="submit" value = "Search">
</form>
</body>
</html>
答案 0 :(得分:1)
您收到错误是因为您希望在加载页面时设置$_POST
数组。
事实并非如此。它只会在提交表单时设置,因此您可以通过添加此if
条件来处理:
if(isset($_POST['submit'])) {
$ds = @ldap_connect("ldap.xxxxx.my");
if (!$ds) {
die("Unable to connect to test server.");
}
$res = @ldap_bind($ds); # Anonymous bind
$userid = (!empty($_POST['username'])) ? $_POST['username'] : ''; // User key their userid or email
// ... the rest of your code
}
正如弗雷德在评论中所说,你可能希望通过使用php的内置函数来确保username
不为空 - empty()