I am currently trying to connect out to our Active Directory to do some fancy searching magic. I have all the fancy searches written in python, now we are just trying to port it over to PHP. I am having issue getting my connection to the ldap server working. I am not getting any error messages and my informative echo's aren't displaying, neither is my footer. Any help would be appreciated! Thanks!
Here is the code:
<?php
include "src/header.php";
echo "Well Hello-01<br>";
if(isset($_POST['username'])){
$User = 'AD\\' . $_POST['username'];
}
if(isset($_POST['password'])){
$Pass = $_POST['password'];
}
echo "Username: " . $User . "<br>";
echo "Password: " . $Pass . "<br>";
$ldapconn = ldap_connect("ldap://ad.whatever.com")
or die("Could not connect to LDAP server");
if($ldapconn){
echo "Attempting Bind";
//binding to ldap
$ldapbind = ldap_bind($ldapconn, $User, $Pass);
//Verify Bind
if($ldapbind){
echo "LDAP bind successfull...";
}else{
echo "LDAP bind failed...";
}
}else{
echo "Fail";
}
include "src/footer.php";
?>
</body>`
And for whatever it's worth here is what is output to the screen: Screenshot
I made this code a little while ago, maybe it can help you:
<?php
$username = $_POST['USERNAME'];
$password = $_POST['USERPASS'];
$server = 'AD_SERVER_IP_GOES_HERE';
$domain = '#MY_DOMAIN.COM';
$port = 389; //default connection port
$dn = "DC=MY_DOMAIN,DC=COM";
$filter = "(&(samaccountname=".$username."))";
$params = array("sn","givenName","samAccountName",
"mail","displayName","department",
"title","company","streetAddress",
"department","memberOf");
/*these are parameters you want to retrieve from a given user*/
$connection = ldap_connect($server, $port);
if (!$connection) {
echo 'no_server';
}
$bind = #ldap_bind($connection, $username.$domain, $password);
if (!$bind) {
echo 'user_error';
}
else
{
$query_user = ldap_get_entries($connection,ldap_search($connection,$dn,$filter,$params));
print_r($query_user);
}
// Close conection
ldap_close($connection);
}
?>
Related
<?php
$a = $_GET['host'];
$b = $_GET['username'];
$c = $_GET['password'];
$d = $_GET['db_name'];
define("localhost",$a);
define("username",$b);
define("password",$c);
define("db",$d);
$conn = mysqli_connect(localhost,username,password);
if($conn === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt create database query execution
$sql = "CREATE DATABASE $d ";
if(mysqli_query($conn, $sql)){
echo "Database demo created successfully";
header("Location:create_table.php");
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
// Close connection
mysqli_close($conn);
?>
In this i need to send $conn with header to send it to create_table.php page.
because i am getting details of connection from user. so i cannot include this file into create_table.php . please help to find out how can i send connection variable into another file.
<?php
$server= "localhost";
$user= "root";
$pass= "";
$conn= mysqli_connection($server, $user, $pass);
if(!$conn){
echo "Database Connection Install Failed ! ".mysqli_errno() ;
}
else {
echo "Database Connection Establiseh Successfully! ";
}
?>
Morning All,
Im after a bit of advice/help with the below issue.
i have tried looking around and google'ing but I still don't understand where I'm going wrong. Please forgive me as I'm a complete noob at this.
some of you may have seem this code before but I'm want to adapt it. Once the user logs in i want to get the user ID from the MySQL Database and send that to swift in the JSON but i can't get it to work, i have tried putting the query in different places with no luck, JSON is completely new to me :(
Ideally > user logs in> swift sends JSON to verify>JSON-PHP verified by MySQL > PHP - JSON reply with success AND user id from the db.
Any Help would be amazing !!
<?php
header('Content-type: application/json');
require("conn.php");
if($_POST) {
$username = $_POST['username'];
$password = $_POST['password'];
if($username && $password) {
$db_name = '***';
$db_user = '***';
$db_password = '***';
$server_url = 'localhost';
$mysqli = new mysqli('localhost', $db_user, $db_password, $db_name);
/* check connection */
if (mysqli_connect_errno()) {
error_log("Connect failed: " . mysqli_connect_error());
echo '{"success":0,"error_message":"' . mysqli_connect_error() . '"}';
} else {
if ($stmt = $mysqli->prepare("SELECT username FROM users WHERE username = ? and password = ?")) {
$password = md5($password);
/* bind parameters for markers */
$stmt->bind_param("ss", $username, $password);
/* execute query */
$stmt->execute();
/* bind result variables */
$stmt->bind_result($id);
/* fetch value */
$stmt->fetch();
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
if ($id) {
error_log("User $username: password match.");
echo '{"success":1, "ID":0}';
} else {
error_log("User $username: password doesn\'t match.");
echo '{"success":0,"error_message":"Invalid Username/Password"}';
}
}
} else {
echo '{"success":0,"error_message":"Invalid Username/Password."}';
}
}else {echo '{"success":0,"error_message":"Invalid Data."}';
}
?>
Attempting to verify the hash password from the database, with the user input via POST. After some brief research it seems the issue may have something to do with not being able to convert the mysqli_query to a string though I do not know how to do that properly, hence the fetch_object() etc which I added from another SO question. I have also adequately accounted for the length of the has with the column set for varchar(255). Appreciate any help or guidance, thanks.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$password = $_POST['password'];
$db_password = mysqli_query($conn, 'SELECT password FROM sec')->fetch_object()->password;
if (password_verify($password, $db_password)) {
echo 'Password is valid!';
} else {
echo 'Invalid password.';
}
?>
First get the password hash from the database,then convert the user input password in the hash value.Compare these two values by passing through the function password_verify(), then you will get the proper result.
$password = "test";
$hash = "$2y$10$fXJEsC0zWAR2tDrmlJgSaecbKyiEOK9GDCRKDReYM8gH2bG2mbO4e";
if (password_verify($password, $hash)) {
echo "Success";
}
else {
echo "Error";
}
OR
You can use this one too
$verify=password_verify($_POST['passwrd'],$row[2]);
if($verify){
$_SESSION["usrname"]=$usrname;
echo "Correct";
}
else {
echo "user: " . $usrname. "<br>";
echo "pass: " . $hash. "<br>";
echo "db: " . $row[2]."<br>";
echo "Wrong Username or Password";
}
I am trying to get authentication from ldap and I am geting the error
"ldap_bind(): Unable to bind to server: Invalid credentials"
any one can provider any information about this .
Below the code I use:
$ldaphost = "ldap.mydomain.com"; $ldapport = 389;
$ds = ldap_connect($ldaphost, $ldapport) or die("Could not connect to $ldaphost");
if ($ds) {
$username = "myUser";
$upasswd = "*****";
$binddn = "uid=$username,ou=people,dc=yourdomain,dc=com"; $ldapbind = ldap_bind($ds, $binddn, $upasswd);
if ($ldapbind) { echo "login" ; } else { echo " not login"; }
}
$ldaphost = "ldap.mydomain.com";
$ldapport = 389;
$ds = ldap_connect($ldaphost, $ldapport) or die("Could not connect to $ldaphost");
if ($ds) {
$username = "myUser";
$upasswd = "*****";
$binddn = "cn=admin,dc=yourdomain,dc=com"; //cn=admin or whatever you use to login by phpldapadmin
$ldapbind = ldap_bind($ds,$binddn, $upasswd);
//check if ldap was sucessfull
if ($ldapbind) {
echo "LDAP bind successful...";
} else {
echo "LDAP bind failed...";
}
}
I'm trying to connect to an LDAP server to authenticate user credentials.
I've found a few users with this same issue but their solutions did not work for me.
here's what I'm using:
<?php
define('LDAP_SERVER', 'LDAP://pdc.mydomain.com');
define('LDAP_PORT', 389);
define('LDAP_TOP', 'dc=mydomain,dc=com');
if(isset($_POST['username']))
{
if(!($ds = ldap_connect(LDAP_SERVER, LDAP_PORT)))
{
die ("Could not connect to mydomain domain");
}
$un = $_POST['username'].",".LDAP_TOP;
//echo stripslashes($un)."<br>";
$ldapbind = ldap_bind($ds, stripslashes($un), $_POST['password']);
if($ldapbind)
echo "login success";
else
echo "login failed";
}
?>
I've tried using "mydomain\myusername" and just "myusername".
I added the stripslashes() function when neither worked to test that, and still no dice.
the error I get every time is: Warning: ldap_bind(): Unable to bind to server: Invalid credentials
any help would be greatly appreciated
TIA
I know it is a pretty old question and if you still need an answer then what happens if you run this code in a single php file?
$username = 'hello';
$password = '123123';
$server = '192.168.32.4';
$domain = '#yourdomain.local';
$port = 389;
$connection = ldap_connect($server, $port);
if (!$connection) {
exit('Connection failed');
}
// Help talking to AD
ldap_set_option($connection , LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($connection , LDAP_OPT_REFERRALS, 0);
$bind = #ldap_bind($connection, $username.$domain, $password);
if (!$bind) {
exit('Binding failed');
}
// This is where you can do your work
echo 'Hello from LDAP';
ldap_close($connection );
More info is here.
Check whether your login and pass correct.
And before the login add domain. See in example bottom (HQ\login):
<?php
$login = 'HQ\student';
$password = 'MYPASS';
$ldap_link = ldap_connect('pdc.bc) or die("Could not connect to LDAP server.");
$ldapbind = #ldap_bind($ldap_link, $login, $password) or die ("Error trying to bind: ".ldap_error($ldap_link));
?>
I used these functions:
function authenticate($username, $password){
include 'conf/config.inc.php';
$ldap_Userdn = getUserDN($username);
if($ldap_Userdn!=""){
$ldap_con = ldap_connect($ldap_hostname,$ldap_port);
ldap_set_option($ldap_con, LDAP_OPT_PROTOCOL_VERSION, 3);
if(ldap_bind($ldap_con, $ldap_Userdn, $password)){
return true;
} else {
//echo "<br>Error bind checkPassword function<br>";
return false;
}
} else {
echo "Error to find user DN" . ldap_error($ldap_con);
}
ldap_close($ldap_con);
}
function getUserDN($username){
include 'conf/config.inc.php';
$data = "";
$ldap_con = ldap_connect($ldap_hostname,$ldap_port);
ldap_set_option($ldap_con, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap_con, LDAP_OPT_REFERRALS, 0);
if(ldap_bind($ldap_con, $ldap_dn, $ldap_password)){
$filter="(cn=$username)";
$dn=$ldap_search; //even if it seems obvious I note here that the dn is just an example, you'll have to provide an OU and DC of your own
$res = ldap_search($ldap_con, $ldap_search, $filter);
$first = ldap_first_entry($ldap_con, $res);
$data = ldap_get_dn($ldap_con, $first);
} else {
echo "<br>Error bind getUserDN function<br>" . ldap_error($ldap_con);
}
ldap_close($ldap_con);
return $data;
}
an this is my config.inc.php:
<?php
$ldap_hostname = "my openldap IP";
$ldap_port = "389";
$ldap_dn = "cn=Manager,dc=mydomain,dc=com";
$ldap_search = "dc=mydomain,dc=com";
$ldap_password ="my password";
?>