Using PHP and LDAP to Authenticate Against BYU's Servers

Been working on a BYU project at the law school where I only want to let BYU students post on a message board.  I needed an authentication solution and decided to give LDAP a try since I've heard it's relatively simple.  Here's the two step process: 1) Create a form that accepts a username and password as follows.

<form action=login.php method=post name=Auth>

Please log in using your NetID and password:<p>

<table cellspacing=3 cellpadding=3> <tr> <td>Username: </td> <td><input type=text name=username size=16 maxlength=15></td> </tr> <tr> <td>Password: </td> <td><input type=password name=password size=16 maxlength=15></td> </tr> <tr> <td colspan=2><input type=submit value=Authenticate style='width:100'></td> </tr> </table> </form>

 2) Create a login.php file.  This file accepts the username and password from the form, then connects to BYU's LDAP server (ldap://ldap.byu.edu - port 389).  After connecting, it binds the connection using the username (NetID) and if successful returns true.  Below, the authenticate function is called and if successful sets the 'loggedin' session variable to 'true' (not a boolean).  It then redirects back to the previous page, which, in this example is the message board I'm working on.

<head> <?php session_start(); ?> </head>

<?php

echo "test"; echo "</br>";

// get username and password from form $username = $_POST['username']; $password = $_POST['password'];

/* * checks the credentials against the LDAP server * $user - RouteY * $pass - password */ function authenticate($user,$pass){

echo "</br>"; echo "Authenticating..." . $user;

// prevents guest account access if($pass == ""){ return false; }

try{

$Yldap_location = "ldap://ldap.byu.edu"; $ldap_port = 389;

// call the ldap connect function $Ydatabase = ldap_connect($Yldap_location, $ldap_port);

// bind the connection $good = @ldap_bind($Ydatabase, "uid=".$user.",ou=People,o=BYU.edu", $pass);

if($good){ // valid credentials return true; } else{ // invalid credentials return false; }

} catch(Exception $e){ return false; } }

// call authenticate function if(authenticate($username,$password)){

// authenticate successful echo "</br>"; echo "SUCCESS";

// set session $_SESSION['loggedin'] = 'true';

// redirect echo $_SESSION['loggedin']; $url = "http://www.law2.byu.edu/page/messageboard.php"; //$url = "http://www.law2.byu.edu/page/messageboard.php"; header("Location: ".$url); } else{

// authenticate fails echo "</br>"; echo "FAIL";

// redirect to login header("Location: http://www.law2.byu.edu/page/messageboard.php"); } ?>

Dropdown Menu in PHP

Browsing around the web you'll notice tons of drop-down menus that let you navigate to different areas of that web site.   Drop down menu navigation is great because it lets you add a lot of navigation options, without taking up much space.  I know, for the web dev buffs out there you would probably never want to do this in PHP alone, but I found a cool tidbit of info on this and wanted to share.  So if you're looking for how to do this on a large-scale you probably want to turn to JavaScript.

With that said, one way to replicate a drop down menu with PHP is to setup an HTML form somewhere on your page and have it pass the site's URL to a PHP page when you submit it.  For example:

<form action="jump.php" method="post"> <select name=url> <option value="http://php.about.com">About PHP</option> <option value="http://www.identity.st">Identity</option> </select> <input type="submit" value="Go"> </form>

Then have the PHP file jump.php use basic redirection to send them to the right page.  For example:

<?php $url = $_POST["url"]; header("Location: $url"); ?>

Give it a shot!

PHP - What and Why

Who invented PHP? Rasmus Lerdorf, a guy who wrote it as CGI binaries in C in order to help manage his personal webpages (back in 1994). Lerdorf named it PHP for "Personal Home Page". Later it was renamed "PHP: Hypertext Preprocessor" by some of the colleagues Lerdorf teamed up with in order to improve on PHP and make it more standardized. Why use PHP?

  • Open source/FREE
  • Cross platform (Windows, Mac, Linux)
  • Powerful, robust, scalable (it'll be able to handle the traffic growth)
  • Web development specific (unlike java, ruby on rails, c, and others)
  • Large developer community
Resources:

Getting Started Project as a "Web Specialist"

Starting my new job as a web specialist for the BYU Law School this week.  The getting started project is as below... super stoked. Create a page using php, mysql and html where users can login or sign up and fill in forms about themselves (first name, last name, age, email address). Provide a spot for them to upload a document of type pdf, reject other types of uploads. Also, create a field that uses CKeditor to get a quick "user blurb" or description of themselves. After they are logged in, have a page in which all users are listed and linked to their profiles which will appear in a Colorbox when clicked upon. Be sure to style everything using CSS. Things you will need to know to do this:

    • How to create a form and use php post variables
    • How to upload and check uploaded documents
    • Session variables in php
    • How to create and use a correctly structured MYSQL database
    • How to use Colorbox
    • How to use CKeditor
    • How to do CSS styling

Tip: We highly recommeded thoroughly completing Lynda.com's PHP with MySQL Essential Training course to assist in preparing for the training exercise.

 

Dec 2011 update: See my mini content management system - still a work in progress.