Use Windows Login in PHP Applications

81
rate or flag this page
Facebook

By Alpho011

As a developer you may run into a situation such as what I had, numerous applications deployed and or created and deployed.

With multiple logins for all the apps, we had to find way to integrate all logins for better management and maintenance.

Since we were using Apache with a PHP server, the solution was to find a way to consolidate the logins usin PHP.

The answer is ldap() (Lightweight Directory Access Protocol), this built in function through PHP, allows you to create username and password schemes using existing Windows authentication.

Take this snippet:

$ldaprdn = 'myName@timbuckTwo.com'; // ldap rdn or dn
$ldappass = 'password'; // associated password

// connect to ldap server
$ldapconn = ldap_connect("Ip address or domain name")or die("Could not connect to LDAP server.");

That is all it takes to connect to your windows login using PHP.

Now for the practical example:

$ldaprdn = $_POST['username']; // ldap rdn or dn
$ldappass = $_POST['password']; // associated password

// connect to ldap server
$ldapconn = ldap_connect("Ip address or domain name")or die("Could not connect to LDAP server.");

The values can be passed on using a web form with the text field names of username and password:

See form code below:

<form name="form1" method="post" action="">
<table>
<tr>
<th colspan="2"><div align="center"><?=$errorMess;?></div></th>
</tr>
<tr>
<td width="76">Username</td>
<td width="324"><input class="<?=$aClass;?>" type="text" name="username" id="username" value="<?=$_POST['username'];?>"></td>
</tr>
<tr>
<td>Password</td>
<td><input class="<?=$aClass;?>" type="password" name="password" id="password" value="<?=$_POST['password'];?>"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="Submit" id="Submit" value="Submit"></td>
</tr>
</table>
</form>

Using the multi purpose pages described in my other tutorial about forms, you can see how the logic of before we push Submit and after we push Submit.

After pushing Submit this snippet runs:

// using ldap bind
$ldaprdn = $_POST['username']; // ldap rdn or dn
$ldappass = $_POST['password']; // associated password


//lets concatenate the proper username:

$ldaprdn = $_POST['username'] . "@whatever the domain name is";

// connect to ldap server
$ldapconn = ldap_connect("ldap.example.com")
or die("Could not connect to LDAP server.");

if ($ldapconn) {

// binding to ldap server
$ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);

// verify binding
if ($ldapbind) {
echo "LDAP bind successful...";
} else {
echo "LDAP bind failed...";
}

}

We contactenated or connected the username and the domain extension to create the proper credentials to pass on to Active Directory.

Now using this knowledge, you can create authentication using the built in Windows login, no creating usernames and passwords, Windows can manage that, leaving you to script and create.

Recap:

We went over basic authentication using a small script and how you can incorporate such a script in a real life scenario.

I invite you to look at the ldap() function and its companions, it does alot more than what we went over, you can search, create and delete and modify users listed on Active Directory using the function.

I will be listing other real world scenarios in the articles to come, please come and do book mark me.

Thank you.

PHP CookBook

PHP Cookbook: Solutions and Examples for PHP Programmers
Amazon Price: $25.30
List Price: $44.99

Comments

Lgali profile image

Lgali 2 years ago

another good hub

Alpho011 profile image

Alpho011 Hub Author 2 years ago

Thank you Lgali, I hope that what I write can benefit anyone who chooses to use it.

Thank you

ZeeaD 2 years ago

this is wonderful tutorial .. i read it 3 times and get a fantastic results and sure i put a copy of this lesson on my site herewww.7wass.com

Alpho011 profile image

Alpho011 Hub Author 2 years ago

Thank you ZeeaD

Jim Gaudet 2 years ago

Nice, now we mix the open source apps into the local Intranet..Thanks

:)

INDRANI profile image

INDRANI 2 years ago

Thanks for sharing your excellent information.Very informative.

doross 2 years ago

this is wonderful tutorial .. i read it 3 times and get a fantastic results and sure i put a copy of this lesson on my site herewww.doross.org

al3boshi 2 years ago

his is wonderful tutorial .. i read it 3 times and get a fantastic results and sure i put a copy of this lesson on my site here

http://www.al3boshi.com/vb

dolo 2 years ago

good

Alpho011 profile image

Alpho011 Hub Author 2 years ago

Just wanted to thank you all for reading and hopefully getting something out of these how tos, once again, thanks.

sayed 2 years ago

this is wonderful tutorial .. i read it 3 times and get a fantastic results and sure i put a copy of this lesson on my site here

www.alhmaya.com

www.ay300.com

sdsds 2 years ago

<!-- /* Font Definitions */ @font-face {font-family:"Microsoft Sans Serif"; panose-1:2 11 6 4 2 2 2 2 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:1627421663 -2147483648 8 0 66047 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:right; mso-pagination:widow-orphan; direction:rtl; unicode-bidi:embed; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} --> this is wonderful tutorial .. i read it 3 times and get a fantastic results and sure i put a copy of this lesson on my site here

amrozaki 2 years ago

this is wonderful tutorial .. i read it 3 times and get a fantastic results and sure i put acopy of this lesson on my site here www.7asryat.prnamg.net

osama 2 years ago

Thank you wonderful information

http://www.moshreq.com/forum

Felix 22 months ago

may i ask hub?

i was build a website using PHP and active directory..

what i want to ask,

could you fixed my code..because i am newbie and wanna try it..

this is my form code

Nama

Password

and this is my connection code (PS : my domain name's is ABC.com and i have OU name's abc and the password aaa)

and this is my dologin.php code

i still confused where's the error..

and one..if i wanna connect this php to active directory?

could i install php in windows server first?

or i can cross it with windows xp to windows server?

(PS: my php file was in windows xp)

thanks before

benedictine191 22 months ago

may i ask hub?

i was build a website using PHP and active directory..

what i want to ask,

could you fixed my code..because i am newbie and wanna try it..

this is my form code

Nama

Password

and this is my connection code (PS : my domain name's is ABC.com and i have OU name's abc and the password aaa)

$ldaprdn = 'abc@CTO.com'; // ldap rdn or dn

$ldappass = 'aaa'; // associated password

// connect to ldap server

$ldapconn = ldap_connect("LDAP.CTO.com")or die("Could not connect to LDAP server.");

and this is my dologin.php code

$ldaprdn = $_POST['username']; // ldap rdn or dn

$ldappass = $_POST['password']; // associated password

//lets concatenate the proper username:

$ldaprdn = $_POST['username'] . "@CTO.com";

// connect to ldap server

$ldapconn = ldap_connect("ldap.CTO.com")

or die("Could not connect to LDAP server.");

if ($ldapconn) {

// binding to ldap server

$ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);

// verify binding

if ($ldapbind) {

echo "LDAP bind successful...";

} else {

echo "LDAP bind failed...";

}

}

i still confused where's the error..

and one..if i wanna connect this php to active directory?

could i install php in windows server first?

or i can cross it with windows xp to windows server?

(PS: my php file was in windows xp)

thanks before

snkhan120 profile image

snkhan120 18 months ago

very nice article about php

george_ro 14 months ago

is is posible to read the username and password from windows log in and use this to authenticate against AD ?

SarahB722 8 weeks ago

Thank you so very much I have been trying to make this work for a week now and this was SO helpful!!

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    • No HTML is allowed in comments, but URLs will be hyperlinked
    • Comments are not for promoting your Hubs or other sites

    working