GVN Group

FR EN Print 

   


 

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:


<?php
  require_once("conf.php");
  require_once("login.php");
 
  page_top();
  if (!$LU->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>' . $LU->getProperty('handle') . '</em>!</p>';
    echo '<p>Here is the contents of the "liveuser_users" table:</p>';
    echo '<ul>';
    echo '  <li>auth_user_id:' . $LU->getProperty('auth_user_id') . '</li>';
    echo '  <li>handle:' . $LU->getProperty('handle') . '</li>';
    echo '  <li>passwd:' . $LU->getProperty('passwd') . '</li>';
    echo '  <li>owner_user_id:' . $LU->getProperty('owner_user_id') . '</li>';
    echo '  <li>owner_group_id:' . $LU->getProperty('owner_group_id') . '</li>';
    echo '  <li>lastlogin:' .  date('d.m.Y H:i', $LU->getProperty('lastlogin')) . '</li>';
    echo '  <li>is_active:' . $LU->getProperty('passwd') . '</li>';
    echo '  <li>email: ' . $LU->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


Scheme

The Login Form

The code

Download «login.php»


<?php
 
function page_top()
  {
  echo '<html><head>';
  echo '<link rel="stylesheet" type="text/css" href="test.css" media="screen" />';
  echo '</head><body>';
  }
 
function page_bottom()
  {
  echo '</body></html>';
  }
 
function show_login()
  {
  ini_set("include_path", '../../libs/PEAR/' . PATH_SEPARATOR . ini_get("include_path"));
  require_once ("HTML/QuickForm.php");
  echo '<p>Please log in to access this page</p>';
  $form =& new HTML_Quickform('logon', 'post');
 
  $renderer =& $form->defaultRenderer();
  $renderer->setElementTemplate('<tr><td align="left" valign="top" width="150">
    <p><span class="label">{label}</span><!-- BEGIN required -->
    <span class="mandatory"> (*)</span><!-- END required --></p></td>
    <td valign="top" align="left"><!-- BEGIN error --><span class="error_msg">{error}</span>
    <br /><!-- END error -->{element}</td></tr>');
 
  $note='<span class="mandatory">(*)</span><span style="font-size:80%;"> Mandatory</span>';
  $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:


Scheme

The CSS File

Download «test.css»

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;
}