I getting this kind of error message, any idea what is wrong with the code on line 9? Basically, the code is to search the user input from the form, then search the username from ldap.
PHP Notice: Undefined index: username in C:\inetpub\wwwroot\LDAP\New3\search2.php on line 9
<?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>
You're getting the error because you're expecting the
$_POST
array to be set whenever you load the page.That is not the case. It will only ever be set when the form is submitted, so you can handle that by adding this
if
condition:As Fred stated in the comments, you might want to make sure that
username
is not empty, by using php's inbuilt function -empty()