Jump to content
Science Forums

My little Intranet project


nikgod

Recommended Posts

So, I've recently took it upon myself to develop a "little" intranet site for my office to use to track things like employee phone numbers, and all the statuses for all of our clients looking to immigrate to the US. You know, the little stuff.

 

Anyway, I'm building most of this system from the ground up and as I do so I'll post the non-sensitive bits up here for you all to look at and critique. Before you do so, however, I'm going to insist that any code or code fragments posted here be treated as open for someone else to copy and put into their own projects. In that spirit, all my code that I post here is licensed under the Apache 2.0 license. If you do use my code, please let me know, it makes me feel all warm and tingly inside.

 

I'll start things off with my login script. I decided that I would use my office's existing Active Directory infrastructure for authentication, and user information (which I put to use in the Employee Directory code which I'll post later). So, the login code takes in the username and password, and then compares it to the LDAP schema running as a part of Active Directory.

 

<?php
require_once('config.php');
session_start();

$_SESSION["lastact"] = time();
if(!isset($_SESSION["login"])) { //THIS IS A NEW SESSION
$_SESSION["login"] = 0;
$_SESSION["message"] = "";
$_SESSION["username"] = "";
$_SESSION["fullname"] = "";
$_SESSION["password"] = "";
}
// connect to LDAP server
$ldapc = ldap_connect($ldap["host"]) or die("Cannot connect to the ldap server :/");
$auth = false;
//look up OU
if (!($res = ldap_bind($ldapc,$ldap["authdn"],$ldap["authpass"])))
{
 print(ldap_error($ldapc) . "<br>");
 die("Could not bind to $dn");
}
else
{
 // set search critia for OU
 $filter = "samaccountname=".$_POST['username'];
 // search OU
 $sr = ldap_search($ldapc,$ldap["rootdc"],$filter);
 if (!$sr)
 {
   die("search failedn");
 }
 else
 {
   // get fields from search
   $info = ldap_get_entries($ldapc,$sr);
   if ($info["count"] == 0)
   {
     $auth = false;
   }
   else
   {
     $auth = true;
     $user_cn = $info[0]["cn"][0];
   }
   // disconnect from LDAP server
   ldap_unbind($ldapc);
 }
}
if ($auth == false)
{
 die("Could not authenticate you to the Active Directory Server.");
}

$ldapc = ldap_connect($ldap["host"]) or die("Cannot connect to AD server :/");
$authdn = "cn=".$user_cn.", ".$ldap["rootdc"];
$authpass = $_POST['password'];

//look up OU
if (!($res = ldap_bind($ldapc,$authdn,$authpass)))
{
 $_SESSION["login"] = 0;
 $_SESSION["message"] = "Invalid Password.";
}
else
{
 $sr = ldap_search($ldapc,$ldap["rootdc"],"cn=".$user_cn);
 $info = ldap_get_entries($ldapc,$sr);
 $_SESSION["login"] = 1;
 $_SESSION["username"] = $info[0]['samaccountname'][0];
 $_SESSION["fullname"] = $info[0]['cn'][0];
 $_SESSION["message"] = "Welcome ".$_SESSION["fullname"];
 $_SESSION["password"] = $authpass;

 header('Location: /index2.php');
}
?>

Link to comment
Share on other sites

require_once('config.php');
session_start();

$_SESSION["lastact"] = time();
if(!isset($_SESSION["login"])) { //THIS IS A NEW SESSION
   $_SESSION["login"] = 0;
   $_SESSION["message"] = "";
   $_SESSION["username"] = "";
   $_SESSION["fullname"] = "";
   $_SESSION["password"] = "";
}
// connect to LDAP server
$ldapc = ldap_connect($ldap["host"]) or die("Cannot connect to the ldap server :/");
$auth = false;
//look up OU
if (!($res = ldap_bind($ldapc,$ldap["authdn"],$ldap["authpass"])))
{
 print(ldap_error($ldapc) . "<br>");
 die("Could not bind to $dn");
}
else
{
 // set search critia for OU
 $filter = "samaccountname=".$_POST['username'];
 // search OU
 $sr = ldap_search($ldapc,$ldap["rootdc"],$filter);
 if (!$sr)
 {
   die("search failedn");
 }
 else
 {
   // get fields from search
   $info = ldap_get_entries($ldapc,$sr);
   if ($info["count"] == 0)
   {
     $auth = false;
   }
   else
   {
     $auth = true;
     $user_cn = $info[0]["cn"][0];
   }
   // disconnect from LDAP server
   ldap_unbind($ldapc);
 }
}
if ($auth == false)
{
 die("Could not authenticate you to the Active Directory Server.");
}

$ldapc = ldap_connect($ldap["host"]) or die("Cannot connect to AD server :/");
$authdn = "cn=".$user_cn.", ".$ldap["rootdc"];
$authpass = $_POST['password'];

//look up OU
if (!($res = ldap_bind($ldapc,$authdn,$authpass)))
{
 $_SESSION["login"] = 0;
 $_SESSION["message"] = "Invalid Password.";
}
else
{
 $sr = ldap_search($ldapc,$ldap["rootdc"],"cn=".$user_cn);
 $info = ldap_get_entries($ldapc,$sr);
 $_SESSION["login"] = 1;
 $_SESSION["username"] = $info[0]['samaccountname'][0];
 $_SESSION["fullname"] = $info[0]['cn'][0];
 $_SESSION["message"] = "Welcome ".$_SESSION["fullname"];
 $_SESSION["password"] = $authpass;

 header('Location: /index2.php');
}

Crash, for future reference, using

tags for php code wields a prettier result :D

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...