My Project
 All Data Structures Files Functions Variables Pages
Public Member Functions
Watchperson Class Reference

Public Member Functions

 __construct ($dbhost, $dbuser, $dbpass, $database, $loginPage='/', $homepage='/')
 pbkdf2_hash ($password, $salt, $iterations=1025, $key_length=256)
 createUserTable ()
 createUser ($username, $password, $level=null)
 login ($username, $password, $path='', $remember=false)
 logout ($path='')
 startSession ($requiredLevel=0, $path='')
 endSession ()
 getLevel ()

Data Fields

const LVL_NONE = 0
const LVL_USER = 1
const LVL_ADMIN = 2
const LVL_OWNER = 3
const LOGIN_SUCCESS = 0
const LOGIN_REQUIRED = 1
const LOGIN_EXPIRED = 2
const LOGIN_UNAUTHORIZED = 3
const LOGIN_FAILED = 4

Detailed Description

This class provides simple tools for handling login and authentication.

Usage Example

require_once 'Watchperson.class.php';
$sentry = new Watchperson('localhost', 'login', '*****', 'database', '/login.php', '/');
$sentry->startSession();

if ($_POST['logout']) $sentry->logout(); elseif (!empty($_POST['username']) && !empty($_POST['password'])) $sentry->login($_POST['username'], $_POST['password'], $_SERVER['REQUEST_URI']);

if ($sentry->getLevel()) echo "level: " . $sentry->getLevel() . "<hr>";


Constructor & Destructor Documentation

__construct (   $dbhost,
  $dbuser,
  $dbpass,
  $database,
  $loginPage = '/',
  $homepage = '/' 
)

Creates a new instance of a Watchman object.

Parameters:
string$dbhostMySQL hostname
string$dbuserMySQL username
string$dbpassMySQL password
string$databaseMySQL database name
string$loginPage(optional) The path to redirect to when login is required. Defaults to the web root.
string$homepage(optional) The path to the site's main page. Defaults to the web root.

Member Function Documentation

createUser (   $username,
  $password,
  $level = null 
)

Creates a user with the specified name, password, and optionally, access level

Parameters:
string$usernameThe username
string$passwordThe raw password string
int$level(optional) The user's access level, as defined at the top of the class. Defaults to the unprivileged LVL_USER
Returns:
TRUE if the insert succeeded, otherwise FALSE
createUserTable ( )

Creates the users table

Returns:
TRUE if the table creation succeeded, otherwise FALSE
endSession ( )

Ends a session and logs the user out, if logged in

getLevel ( )

Returns the access level of the user.

Returns:
0 if no user is logged in, otherwise the user level as defined at the top of the class
login (   $username,
  $password,
  $path = '',
  $remember = false 
)

Attempt to log a user in

Parameters:
string$usernameThe username
string$passwordThe raw password string
string$path(optional) The path to redirect to, on successful login. Default value is the homepage defined on instanciation.
bool$remember(optional) Currently unimplemented
logout (   $path = '')

Creates a user with the specified name, password, and optionally, access level

Parameters:
string$path(optional) The path to redirect to on logout. Default value is the homepage defined on instanciation.
pbkdf2_hash (   $password,
  $salt,
  $iterations = 1025,
  $key_length = 256 
)

Runs a key (password) through the PBKDF2 algorithm and returns it as a hex string

Parameters:
string$passwordThe password
string$saltA salt that is unique to the password.
int$iterations(optional) Iteration count. Higher is better, but slower. Recommended: At least 1024
int$key_length(optional) The length of the derived key
Returns:
A $key_length sized hex string derived from the password and salt
startSession (   $requiredLevel = 0,
  $path = '' 
)

Starts a session for a page. Optionally, setting the required level Call this on page before any output, as it may send HTTP headers

Parameters:
int$requiredLevel(optional) Optionally, the previous path the user was on; useful for preserving the page when logging in
string$path(optional) The path to use for redirects

Field Documentation

const LOGIN_SUCCESS = 0

Error codes, as integers

const LVL_NONE = 0

User access levels, as integers, so you can easily test if a user's level < the required level


The documentation for this class was generated from the following file: