1

I am trying to implement a LDAP connection through PHP for an internal web page. I use Uniform Server for my PHP. http://www.uniformserver.com/ . As a test I am trying to connect to the test server here. http://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/

But I don't understand it seem like it doesn't even go in the if/else statements.

Here's my code :

<?php

$ldapServer = 'ldap.forumsys.com';
$ldapPort = 389;

if(isset($_POST['username']) && isset($_POST['password'])){
    echo "Hello both parameters are set";
    $username = $_POST['username'];
    $password = $_POST['password'];
    echo "<br>" . $username;
    echo "<br>" . $password;
    $ds = ldap_connect($ldapServer, $ldapPort);

    if (ldap_bind($ds, $_POST['username'], $_POST['password'])) {
        // Successful auth
        $_SESSION['username'] = $_POST['username'];
        $_SESSION['password'] = $_POST['password'];
        // log them in!
        echo 'you are logged in ' . $_SESSION['username'] ;    
        //redirect after logged in2
    } else {
        // error message

        echo 'auth failed';
    }
}
else
{
?>
    <form action="login.php" method="POST">
        <label for="username">Username: </label><input id="username" type="text" name="username" required="true" /> 
        <label for="password">Password: </label><input id="password" type="password" name="password" required="true" />        <input type="submit" name="submit" value="Submit" />
    </form>
<?php
 } ?> 

My first echos are shown (password and username) but nothing after that is shown. Anyone sees why? I don' have any errors but here's my response.

Hello both parameters are set
username
password

Thanks

1
  • 1
    Usually, if some of the LDAP functions fail to perform whatever they are supposed to perform, they emit at least a E_WARNING level message. Have you checked your PHP logs? Do you have logging enabled? Commented Mar 23, 2015 at 15:31

1 Answer 1

1

First things first, check if you're getting a connection. The below snippet is one way of checking this:

if ($ds) {
// Successful connection
} else {
echo "Error connecting";
}

My guess is you're seeing the echo portion because they're outside the ldap_bind, while you're not getting a bind because your connection is failing. Double check the server address and/or try using an IP address. Note you can safely drop the port at this stage as 389 is default, so simply using

$ds=ldap_connect("ip/address");

Would work fine.

Now, since you're hitting what appears to be an external domain address it may be LDAPS, so double check that (Or maybe it's not). If this is the case, you will need

$ds = ldap_connect('ldaps://ip', 636);

Let me know if any of this has helped

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer, I tried surrounding my code with the if $ds and trying with the ip but it is not working. It is strange the php works and echos what I asked but it also prints POST http://localhost/phpproject/login.php 500 (Internal Server Error) I also made sure to uncomment all extension=php_ldap.dll in the ini files. Nothing is doing it...
If you use an if....else block on $ds and put an error message in the else section, does it show the error for you? Updated my answer
No it doesn't show , that's actually what I tried. I'm pretty confused.
It sounds like you may have errors disabled - try this - ini_set('error_reporting', E_ALL); It may not work, so if not, check your php.ini and set it there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.