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 |
This class provides simple tools for handling login and authentication.
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>";
__construct | ( | $dbhost, | |
$dbuser, | |||
$dbpass, | |||
$database, | |||
$loginPage = '/' , |
|||
$homepage = '/' |
|||
) |
Creates a new instance of a Watchman object.
string | $dbhost | MySQL hostname |
string | $dbuser | MySQL username |
string | $dbpass | MySQL password |
string | $database | MySQL 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. |
createUser | ( | $username, | |
$password, | |||
$level = null |
|||
) |
Creates a user with the specified name, password, and optionally, access level
string | $username | The username |
string | $password | The 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 |
createUserTable | ( | ) |
Creates the users table
endSession | ( | ) |
Ends a session and logs the user out, if logged in
getLevel | ( | ) |
Returns the access level of the user.
login | ( | $username, | |
$password, | |||
$path = '' , |
|||
$remember = false |
|||
) |
Attempt to log a user in
string | $username | The username |
string | $password | The 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
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
string | $password | The password |
string | $salt | A 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 |
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
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 |
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