<?php
/* project: AUTH demo, file: auth.php */
//Include pclib framework
require 'pclib/pclib.php';
//Create pclib application
$app = new PCApp('demo');
$datasource = 'pdo_mysql://user:password@localhost/test';
session_start();
$db = new PCDb($datasource);
$app->db = $db;
//Set auth secret. Required. Use some hard-to-guess random string.
$app->config['pclib.auth']['secret'] = 'a#zBxy9a45%';
//Create auth object. Session is initialized here.
$auth = new PCAuth;
//1. If user is logged-in, show securepage:
if ($auth->isLogged()) {
//user click on "logout"
if ($_GET['r'] == 'auth/logout') {
$auth->logout();
reload();
}
//show page with username
$user = $auth->getUser()->getValues();
$page = new PCTpl('tpl/securepage.tpl');
$page->values['USERNAME'] = $user['USERNAME'];
//test user permissions
if ($auth->hasRight('demo/auth/testright'))
$page->values['INFO'] = 'You have permission demo/auth/testright';
else
$page->values['INFO'] = 'You have NOT permission demo/auth/testright!';
print $page;
return;
}
//2. Otherwise show loginform and perform login
$loginForm = new PCForm('tpl/loginform.tpl');
if ($loginForm->submitted) {
$userName = $loginForm->values['username'];
$password = $loginForm->values['password'];
$ok = $auth->login($userName, $password);
if ($ok) reload();
else {
foreach ($auth->errors as $message) {
$app->message($message, 'warning');
}
}
}
print $loginForm;
// -- END --
function reload() {
header("Location: index.php?r=auth");
exit();
}
?>
Elapsed time: 13.26 ms