Home arrow PHP arrow Page 4 - Building a Logout Class

Testing the Classes - PHP

The main function of the logout class is to log out a user and then redirect the user to the login page. On a programmatic level, the logout class terminates and destroys the session variables that the login class created for the user and also logs the exit time to the database. This article will show you how to create a logout class. It is part of a series on application framework components.

  1. Building a Logout Class
  2. Recording the Logout Session
  3. The Database Tables
  4. Testing the Classes
By: Chris Neeman
Rating: starstarstarstarstar / 6
September 05, 2007

print this article



Let's test the login/logout classes and see if they work. Create a new PHP document and add the following code. I've commented on the code that I used here so it should be easy to understand:

error_reporting(E_ALL &~E_NOTICE);
// First load the DB.php class from PEAR
require_once 'DB.php';
// Now load our DBAL class
//load the config file for the login class
//finally load the login class itself.
require_once('login.class.php'); /*
$emails =mysql_escape_string($_POST['email']);
$pass=mysql_escape_string($_POST['pass']); */
  echo "<b>Contents of db connection variable:</b>";
  echo '<br>';
  $a = new authorization($emails,$dp,$pass);
  $y = $a->check_user();
    echo "<b>Userid:</b> ".$myid." <b>Username: </b>".$myname;
    echo '<table>
    echo '</table>';
    echo $err."ERR";
  <form id="form1" name="form1" method="post" action="<? $_SERVER
    <table width="100%" border="0">
        <td colspan="2"><center>Please login below:</center></td>
      <?php if(!empty($errmsg)){ ?>
        <td width="10%" bgcolor="#FF0000"><b>ERROR!</b></td>
        <td width="90%"><?php echo $errmsg;?></a></td>
<?php } ?>
<?php if(isset($lmsg)){ ?>
        <td width="10%" ><b>Logout message</b></td>
        <td width="90%"><?php echo "You've been logged out.'";?
<?php } ?>
        <td><input name="email" type="text" class="input200"
id="email" value="mubasen.gaseb@damaranet.com" size="50"/></td>
          <input name="pw" type="password" class="input200"
id="pw" value="pass" />
          <input name="sub" type="hidden" value="sub" />
          <input type="submit" name="Submit" value="Login" />

The HTML form collects the username and password and then compares it with the information in the database. This is done through the checkuser() function defined in the login class. Upon successful authentication the username and id are displayed together with the contents of the connection variable. The id of the user is received through the getid() function of the login class; similarly, the username is received through the getname() function. Below is a screenshot of the login test:


There's a couple of things that I haven't done here in terms of security. The first is that I did not hash the passwords that are stored in the database, so please make sure that you implement this safety measure. Use either MD5() or Crypt() to encrypt the passwords. If there are other things that I have not done correctly than please, by all means, correct them before using the code. It is also a good idea to keep both the login application and users table separate from the other applications and databases. The users table should be in a separate database from the rest of the tables.

>>> More PHP Articles          >>> More By Chris Neeman

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: