I have not done OOP in PHP before. This class is used to get basic user information. It will be used in other classes later on to perform actions relating to the users account as well as setting up sessions.

The only thing I have used it for atm is on a login page. I place the object within a try statement and any errors are caught and return back to the user on the login page. My DIR structure is / , /includes and /includes/classes. The includes DIR is for processing stripts mainly.

I have a login.php page and a login.process.php page which runs a few functions (strlen, preg_match and trim) before it passes the username and password into the class to create an object.

The DB script is a singleton class.

Viewing Page
PHP Code:
<?php 
    
// This page is accessible
    
define("ACCESS"true);
    
    
// Include the various scripts
    
include('includes/login.php');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">

    <head>
        <title>No 8 Catering Events - Login Test</title>
    </head>
    
    <body>    
    <?php if(isset($error)): ?>
        <p class="error">Login Error: <?php print $error ?></p>
    <?php endif; ?>
        
        <form method="POST" action="<?php print basename(__FILE__?>">
            <fieldset>
                <legend>Client Log In</legend>
                <div>
                    <label for="username">Username</label>
                    <input type="text" id="username" name="username">
                </div>
                <div>
                    <label for="password">Password</label>
                    <input type="password" id="password" name="password">
                </div>
                <div>
                    <label for="rememberMe">Remember Me</label>
                    <input type="checkbox" name="rememberMe" id="rememberMe" value="rememberMe">
                </div>
                <button type="submit" name="logIn" value="Log In">Log In</button>
            </fieldset>
        </form>
    </body>
</html>
User Class
PHP Code:
<?php
    
// Restricted outside access to the script
    
defined('ACCESS') or die('Restricted Access');

    
// Class responsible for retriving user information
    // that can be used in queries.

    
include_once('dbConn.class.php');
    include_once(
'session.class.php');

    class 
User
    
{
        private 
$userId;
        private 
$username;
        private 
$password;
        private 
$userLevel;
        
        public function 
__construct($username$password)
        {
            
$this->setUsername($username);
            
$this->setPassword($password);
            
$this->setUID();
            
$this->setUserLevel();
        }
        
        private function 
setUsername($username)
        {
            
$prepare DB::getInstance()->prepare("SELECT username FROM users WHERE username = '$username'");
            
$prepare->execute();
            
$result $prepare->fetch(PDO::FETCH_ASSOC);
            
            if(!
$result['username']) throw new Exception("User does not exist!");

            
$this->username $result['username'];
        }
        
        private function 
setPassword($password)
        {
            
$prepare DB::getInstance()->prepare("SELECT username FROM users WHERE username = '{$this->username}' AND password = '$password'");
            
$prepare->execute();
            
$result $prepare->fetch(PDO::FETCH_ASSOC);
            
            if(!
$result['username']) throw new Exception("Username found but password is incorrect.");
            
            
$this->password $result['username'];
        }
        
        private function 
setUID()
        {
            
$prepare DB::getInstance()->prepare("SELECT user_id FROM users WHERE username = '{$this->username}'");
            
$prepare->execute();
            
$result $prepare->fetch(PDO::FETCH_ASSOC);
        
            
$this->userId $result['user_id'];
        }
        
        private function 
setUserLevel()
        {
            
$prepare DB::getInstance()->prepare("SELECT userlevel FROM users WHERE username = '{$this->username}'");
            
$prepare->execute();
            
$result $prepare->fetch(PDO::FETCH_ASSOC);
        
            
$this->userLevel $result['userlevel'];
        }
        
    
        
    }

?>
Processing Page
PHP Code:
<?php
    
include_once('classes/User.class.php');

    
// Start the session
    
session_start();

    
// Restricted outside script to the script
    
defined('ACCESS') or die('Restricted Access');

    
// Check that the form has been submitted
    
if(isset($_POST['username']))
    {
        
$username     = (empty($_POST['username'])) ? null $_POST['username'];
        
$password     = (empty($_POST['password'])) ? null $_POST['password'];
        
        try
        {            
            
// Check to see if the username or password fields are blank
            
if($username == null || $password == null) { throw new Exception("You have left one or more fields blank."); }
            
            
// Check the length of the username and password fields
            
if(strlen($username) > 12 || strlen($password) > 12) { throw new Exception("Username or Password is too long."); }
    
            
// Check that the input only contains letters and/or numbers
            
if(preg_match("/[^A-Za-z0-9]/"$username) || preg_match("/[^A-Za-z0-9]/"$password)) 
            {
                throw new 
Exception("Username and Password can only contain letters and/or numbers");
            }
            
            
// Create the user class
            
$user = new User(trim($username), trim($password));
            
        }
        catch(
Exception $e)
        {
            
$error $e->getMessage();
        }
    }
?>