WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search


Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - השוואת מחירים בסופר
ZeroLag.com
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Authentication script to authenticate users in Active Directory through LDAP.
Categories : LDAP, Authentication, Cookies, PHP Update Picture
Patrick Dooley
Date : Feb 25th 2002
Grade : 3 of 5 (graded 12 times)
Viewed : 134469
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Patrick Dooley
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

Simple and fast authentication module when you want to authenticate your users
againt Active Directory through LDAP. The script will also store the username,
Display Name, and fully qualified DN ie;
DN=user,ou=users,ou=account,dc=domain,dc=com in a cookie. You could also
store any other attribute of the user in the cookie as well (email address, office
phone, etc...)

Note: you will need to change the server, basedn and filter variables for your
environment.

Just place an include "auth.in";
at the top of every file that you want protected.


<?
$server="XXX.XXX.XXX.XXX";        //change to ip address of ldap server
$basedn="ou=users, ou=accounts, dc=domain, dc=com";        //change to reflect the ou
and
domain that your users are in.
$script=$_SERVER['SCRIPT_NAME'];
if (isset($HTTP_COOKIE_VARS['cookie'])) {         //If cookie exists, retrieve it and
put it in an
array for use.
        $cookie=$HTTP_COOKIE_VARS['cookie'];
        }
if (isset($cookie)) {                                 
        $username=$cookie['user'];
        $password=($cookie['token']);
        $fullname=$cookie['fullname'];
        $fqdn=$cookie['fqdn'];
        $dn = "cn=$username, ";
                if (!($connect = ldap_connect($server))) {
                        die ("Could not connect to LDAP server");
                }

                if (!($bind = ldap_bind($connect, "$dn" . "$basedn", $password))) {
                        die ("Could not bind to $dn$basedn");
                }
        } else {
                if ((isset($_POST['username'])) && (isset($_POST['password']))) {
                        $username=$_POST['username'];
                        $password=$_POST['password'];
                        $filter="(&(|(!(displayname=Administrator*))(!
(displayname=Admin*)))(cn=$username))";        //define an appropriate ldap search filter
to
find your users, and filter out accounts such as administrator(administrator should
be
renamed anyway!).
                        $dn = "cn=$username, ";
                                if (!($connect = ldap_connect($server))) {
                                        die ("Could not connect to LDAP server");
                                }

                                if (!($bind = ldap_bind($connect, "$dn" . "$basedn",
$password))) {
                                        die ("Could not bind to $dn");
                                }
                $sr = ldap_search($connect, $basedn,"$filter");
                $info = ldap_get_entries($connect, $sr);
                $fullname=$info[0]["displayname"][0];
                $fqdn=$info[0]["dn"];
                setcookie("cookie[user]",$username);
                setcookie("cookie[token]",$password);
                setcookie("cookie[fullname]",$fullname);
                setcookie("cookie[fqdn]", $fqdn);
        } else {
?>


<html>
<head>
<title>Portal Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="expires" content="0">
<meta http-equiv="pragma" content="no-cache">
</head>
<SCRIPT LANGUAGE="JavaScript">
        <!--
                document.onmousedown=click;
                function click()
                {
                        if (event.button==2) {alert('Right-clicking has been
disabled by
the administrator.');}
                }
                
        //-->
        </SCRIPT>
<div align="center">
<form method="post" action="<? echo $script; ?>">
<div align="center">

<table width="210" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center">
<fieldset>
<Legend><font face="Verdana,Tahoma,Arial,sans-serif" size="1"
color="gray">Enter Credentials</font></Legend>
<table border="0" cellspacing="3" cellpadding="0">
<tr>
<td align="right" valign="middle"><b><font
face="Verdana,Tahoma,Arial,sans-
serif" size="1" color="gray">Username:</font></td>
<td align="center" valign="middle">
<input class="clear" type="text" size="15" name="username">
</td>
</tr>
<tr>
<td align="right" valign="middle"><b><font
face="Verdana,Tahoma,Arial,sans-
serif" size="1" color="gray">Password:</font></td>
<td align="center" valign="middle">
<input class="pass" type="password" size="15"
name="password">
</td>
</tr>
</table>
<input type=image src="images/login.gif" alt="Login"
name="image">
<br>
</div>
</td>
</tr>
         </fieldset>
</table>
<br>
<table width="640"><tr><td align="center">
<font face="Verdana,Tahoma,Arial,sans-serif" size="1"
color="silver">This System is
for the use of authorized users only. Individuals using this computer system
without
authority, or in excess of their authority, are subject to having their activities
on this system
monitored and recorded by system personnel. In the course of monitoring individuals
improperly using this system, or in the course of system maintenance, the activities
of
authorized users may also be monitored. Anyone using this system expressly consents
to
such monitoring and is advised that if such monitoring reveals possible criminal
activity,
system personnel may provide the evidence of such monitoring to law enforcement
officals.
This warning has been provided by the United States Department of Justice and is
intended to
ensure that monitoring of user activity is not in violation of the Communications
Privacy Act of
1986.</font>
</td></tr></table>

</div>
</form>

</div>
</body>
</html>
<?
die ();
}
}
?>



Authenticator for Exchange Server LDAP
Categories : PHP, Authentication, LDAP, Security, Sessions
Complete, simple working example of login screen and check on a unique page using php functions, cookies and mysql database.
Categories : PHP, Cookies, MySQL, HTML and PHP, Authentication
PHP4 MYSQL Authentication Script with cookie. Short & Sweet
Categories : Authentication, Apache, Cookies, PHP, MySQL
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
Store, retrieve and delete cookies using JavaScript.
Categories : Java Script, Cookies, Beginner Guides, Cookies
PHP4 AND MySQL Authentication
Categories : PHP, MySQL, Authentication, Databases
Basic WWW-Authentication example
Categories : PHP, Authentication
Simple PHP cookie counter
Categories : PHP, Cookies, Beginner Guides
Import the yahoo address book.
Categories : PHP, CURL, Authentication
SQL / PHP based Integrated Authentication
Categories : PHP, Authentication, Databases, MySQL
Validation function for LUHNMod10 and variant. Can discriminate credit card numbers of varying lengths. Uses [Double >> Sum-of-Digits] transform.
Categories : Credit Cards, Authentication, Ecommerce, PHP
Use of bitmasks to represent permissions
Categories : PHP, Authentication, Bitwise Operators, Security, PHP Classes
Authorize Me! An authentication script.
Categories : MySQL, Databases, Authentication, PHP
ldap_add -- Add entries to LDAP directory
Categories : PHP, PHP Functions, LDAP
MD5 secured login
Categories : PHP, Java Script, Authentication, Security
 Patrick Dooley wrote : 786
Ooops, just copy this text into a file called "auth.inc" and include it at the top of every page you want protected.
 
 Bruce Truman wrote : 1023
Hello,

I am having a problem with this script. Can you please help 
me out. I am not sure if I am setting all the variables 
correctly and I get the following error:


Fatal error: Call to undefined function: ldap_connect() in 
D:\intranet\loginnt.php on line 28

 
 Jeremy Polley wrote : 1061
include "auth.inc";

all that simply did was added the code to the page that was 
already there, also, it is not contacting active directory 
properly, how can this be fixed?
 
 Gus Kilkenny wrote : 1352
Hi

May I first say thanks for a very useful script. I have a 
slight problem with it though in that I can`t get some 
users to authenticate. The reason is that we have one 
parent OU with child ou`s that brake off containing a 
branch with computers,users,printers in there. so at the 
$basedn="ou=users, ou=accounts, dc=domain, dc=com";    
section the first ou contains users but the second only 
allows us to specify one ou (or branch in our structure)

Can you tel me a way (bar moving all users in to one ou) 
that I can authenticate across multiple branch ou`s?

Many thanks
Gus 
 
 Ryan Dyck wrote : 1830
This code is incomplete!

" } else {
?>
"

whould be way more code between { and ?>
 
 Deac Karns wrote :1913
nice code, it worked like a charm with little
 modification.  Now, How do you destroy the cookie so that 
you can log out via a button?