LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-12-2008, 02:18 AM   #1
ajeetraina
Member
 
Registered: Jun 2007
Location: India
Distribution: Ubuntu,Red Hat, Fedora
Posts: 292

Rep: Reputation: 30
PHP Login through LDAP Authentication??


I have written a PHP Script for user login as we generally retrieve from database.Now I want to allow user to login through LDAP.
How can I go for the same?
 
Old 05-12-2008, 05:31 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Here's what I once used:
PHP Code:
define('LDAP_SERVER''localhost');
define('LDAP_PORT'389);
define('LDAP_TOP''dc=domain,dc=com');

function 
LDAPconnect()
{
    
$ds ldap_connect(LDAP_SERVERLDAP_PORT);
    if (!
$ds) {
        return 
FALSE;
    }
    if (!
ldap_set_option($dsLDAP_OPT_PROTOCOL_VERSION3)) {
        @
ldap_close($ds);
        return 
FALSE;
    }
    return 
$ds;
}


function 
LDAPauth($rdn$passwd)
{
    
$ds LDAPconnect();
    
$dn $rdn .","LDAP_TOP;

    if (!
ldap_bind($ds$dn$passwd)) {
        return 
FALSE;
    }
    return 
$ds;

Needless to say, here's where the details are: http://php.net/ldap
 
Old 05-12-2008, 07:07 AM   #3
ajeetraina
Member
 
Registered: Jun 2007
Location: India
Distribution: Ubuntu,Red Hat, Fedora
Posts: 292

Original Poster
Rep: Reputation: 30
I have this code written for login:
----------------------------
File: login.php
---------------------------
Code:
<html><table width="300" border="1" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<p>
<body bgcolor="#d0d0d0">
<img src="helpcore.jpg" align="center">
</p>
<br><iframe src="http://free.timeanddate.com/clock/iz28p1x/n438/fc969/tccff/pcff9/ftb/bas2/bat6/bacf00/tt0/td1/tb3" frameborder="1" width="426" height="22"></iframe>
</br><br>
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="password" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
<td><br><a href="forget.php"><i>Forgot your password?</i></a><td><a href="register.php">Register</a></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</html>
~
File:
Code:
<?php
session_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="mysql123"; // Mysql password
$db_name="helpcore"; // Database name
$tbl_name="users"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string ($mypassword);

$sql="SELECT * FROM $tbl_name WHERE loginname='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername']=$myusername;
$_SESSION['mypassword']=$mypassword;


#session_register("myusername");
#session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
//echo "You need to register to login.Go Back and click on "Register" button.";
}

?>
Pls help me where should I modify???Since I am newbie for PHP.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ldap authentication help ... sir-lancealot Linux - Server 2 10-11-2007 04:00 PM
help in LDAP authentication chickenjoy Linux - Server 3 06-14-2007 05:28 AM
LDAP Authentication - no Gnome login possible azrael808 Linux - Software 3 01-11-2007 05:43 AM
ldap authentication goestin Linux - Networking 2 12-27-2005 11:27 AM
LDAP Authentication and su da_kidd_er Linux - Networking 1 12-27-2005 11:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration