LiveUser Authentication Example

The example below will allow us to illustrate the topics developped in the previous article.

Let's try to put all together.

In order to have it working, you'll need the following:

  • a good installation of all libraries
  • the example 4 of the LiveUser package to have the database structure created
  • the configuration file for LiveUser (seen in the previous page)
  • the test file itself
  • a file dealing with the display of the login form
  • a css file for the presentation

Installation

You'll need to install PEAR and some PEAR packages to have the example up and running. You'll find the list of required stuff below:

The Configuration File

In the beginning, we ve taken the configuration from one of the LiveUser examples. Some changes have been done to match our configuration.

You'll need to do the same for your environment:

  • the ini_set for PEAR (may be needed when using shared hosting services)
  • the information to connect to the database: username, password, db name and server

The Test File

The code

Download «LU_test.php»


Here is the PHP code for the test file:


Code
<?php
require_once("../../../../includes/doc/liveuser/example/conf.php");
require_once("../../../../includes/doc/liveuser/example/login.php");

page_top();
if (!->isLoggedIn())
{
echo '<h1>Login</h1>';
show_login();
}
else
{
echo '<h1>LiveUser Example</h1>';
echo '<a href="?logout=1">Logout</a><br /><br />';
echo '<p>Welcome <em>' . ->getProperty('handle') . '</em>!</p>';
echo '<p>Here is the contents of the "liveuser_users" table:</p>';
echo '<ul>';
echo ' <li>auth_user_id:' . ->getProperty('auth_user_id') . '</li>';
echo ' <li>handle:' . ->getProperty('handle') . '</li>';
echo ' <li>passwd:' . ->getProperty('passwd') . '</li>';
echo ' <li>owner_user_id:' . ->getProperty('owner_user_id') . '</li>';
echo ' <li>owner_group_id:' . ->getProperty('owner_group_id') . '</li>';
echo ' <li>lastlogin:' . date('d.m.Y H:i', ->getProperty('lastlogin')) . '</li>';
echo ' <li>is_active:' . ->getProperty('passwd') . '</li>';
echo ' <li>email: ' . ->getProperty('email') . '</li>';
echo '</ul>';
}
page_bottom();
?>

What is it doing?

The example above performs the following:

  • load the configuration file
  • load the file dealing with the login form
  • 'page_top()' just makes the link with the css file
  • if the user is not logged in, the login form is displayed. Otherwise, some user information from the liveuser_users table is displayed
  • then final HTML tags are set

Example of test result

Some user information displayed from the «liveuser_users» table after a successfull login


The Login Form

The code

Download «login.php»


Code
';
  echo '';
  echo '';
  }

function page_bottom()
  {
  echo '';
  }

function show_login()
  {
  ini_set("include_path", '../../libs/PEAR/' . PATH_SEPARATOR . ini_get("include_path"));
  require_once ("HTML/QuickForm.php");
  echo '

Please log in to access this page

'; $form =& new HTML_Quickform('logon', 'post'); $renderer =& $form->defaultRenderer(); $renderer->setElementTemplate('

{label} (*)

{error}
{element}'); $note='(*) Mandatory'; $form->setRequiredNote($note); $form->addElement('text', 'handle', 'Userid', array('class'=>'text')); $form->addRule('handle', 'Userid is mandatory', 'required'); $form->addElement('password', 'passwd', 'Password', array('class'=>'text')); $form->addRule('passwd','Password is mandatory', 'required'); $form->registerRule('check_pswd', 'callback', 'is_pswd_ok', $form); $form->addRule('passwd', 'Logon failed', 'check_pswd'); $form->addElement('advcheckbox', 'rememberMe', '', ' Remember me', array('class'=>'text'), array(false,true)); $form->addElement('submit', 'btnSubmit', 'Login'); if (!$form->validate()) { $form->display(); } } function is_pswd_ok(&$passwd) { global $LU; $LU->login($LU->getProperty('handle'),$LU->getProperty('passwd')); if ($LU->isLoggedIn()) { return true; } else { return false; } } ?>

I will not (yet) detail the PEAR::Quickform package here.

In brief:

  • the renderer is used to have an improved presentation of the form
  • the form elements are added: userid (or handle), password and the «Remember Me» checkbox
  • some validation rules are added as well
  • if the information entered is correct, the login occurs

The form

Here is a sample output of the login form:


The CSS File

Download «test.css»

Code
body, table {
  font-family: verdana, arial, helvetica, sans-serif;
  font-size: 12px;
}

h1 {
  font-size: 16px;
  color: #6495ED;
}

h2 {
  font-size: 14px;
  padding-left: 10px;
  color: #6495ED;
}

h3 {
  font-size: 12px;
  padding-left: 20px;
  color: #6495ED;
}

label, .label {
  font-style: italic;
}

em {
  color: blue;
}

input.text, textarea, select {
  background-color: #F5F3F4;
  border: 1px;
  border-style: solid;
  border-color: #7F9DB9;
}

.mandatory {
  font-size: 80%;
  color: #ff0000;
}

.error_msg {
  font-size: 11px;
  color: #ff0000;
}

.forbidden {
  color: #ff0000;
  font-weight: bold;
}

Get in touch!

We'd love to hear from you, what you think about this page or what we can do for you.

Contact Us

View this page with IE8+, Mozilla Firefox, Safari, Chrome and Mobiles

Last modificaton on 21 May 2015 by St├ęphane Van Nerom

© 2006-2014 - GVN - All rights reserved
v2.0.14