javascriptcursus/php/adminpage.php
<?php
require_once 'page.php';
// Login details:
define("ADMIN_NAME", 'admin');
define("ADMIN_PASS", 'javascript');
session_start();
class adminpage extends page
{
// Public:
public function __construct($title = 'Geen titel')
{
parent::__construct($title);
$this->loggedIn = false;
$this->justLoggedOut = false;
}
public function isLoggedIn()
{ return $this->loggedIn; }
public function printAll()
{
if(!$this->justLoggedOut && !$this->checkLogin())
$this->setLoginPage();
parent::printAll();
}
public function logOut()
{
setcookie('admin_name', $_COOKIE['admin_name'] = null, time()-1); // Verwijder cookie
setcookie('admin_pass', $_COOKIE['admin_pass'] = null, time()-1); // Verwijder cookie
$_SESSION['admin_name'] = null;
$_SESSION['admin_pass'] = null;
$this->justLoggedOut = true;
$this->loggedIn = false;
}
// Protected:
protected $loggedIn;
protected $justLoggedOut;
protected function checkLogin()
{
if(isset($_COOKIE['admin_name']) && isset($_COOKIE['admin_pass']))
{
$_SESSION['admin_name'] = $_COOKIE['admin_name'];
$_SESSION['admin_pass'] = $_COOKIE['admin_pass'];
}
if(isset($_POST['admin_name']) && isset($_POST['admin_pass']))
{
$_SESSION['admin_name'] = $_POST['admin_name'];
$_SESSION['admin_pass'] = crypt($_POST['admin_pass']);
}
if(isset($_SESSION['admin_name']) && isset($_SESSION['admin_pass']) && strcasecmp($_SESSION['admin_name'], ADMIN_NAME) == 0 && crypt(ADMIN_PASS, $_SESSION['admin_pass']) == $_SESSION['admin_pass'])
{
if(isset($_POST['remember_me']))
{
setcookie('admin_name', $_SESSION['admin_name'], time()+3600*24*30); // Verloopt in 30 dagen
setcookie('admin_pass', $_SESSION['admin_pass'], time()+3600*24*30); // Verloopt in 30 dagen
}
return $this->loggedIn = true;
}
else
return $this->loggedIn = false;
}
protected function setLoginPage()
{
$this->title = 'Login';
$this->content = null;
if(isset($_POST['admin_name']))
{
$this->content .= $this->createWhiteBlock('Verkeerde login gegevens!', 'login-error');
$this->content .= '<br />';
}
$this->content .= $this->createWhiteBlock(
'<form id="login" action="'.$_SERVER['REQUEST_URI'].'" method="post">'.
'<table>'.
'<tr><td><b>Naam:</b></td><td><input type="text" name="admin_name" /></td></tr>'.
'<tr><td><b>Wachtwoord:</b></td><td><input type="password" name="admin_pass" /></td></tr>'.
'<tr><td><b>Onthoud me:</b></td><td><input type="checkbox" name="remember_me" id="remember_me" /><label for="remember_me"> Onthoud mijn login gegevens op deze computer</label></td></tr>'.
'</table>'.
'<input type="submit" value="Login" />'.
'</form>');
}
protected function generateIndex()
{
if($this->loggedIn)
{
return '<h2>Navigatie</h2>'.
'<ol id="index-list">'.
'<li><a href="admin.php">Home</a></li>'.
'<li><a href="admin.php?page=assignments">Opdrachten</a></li>'.
'<li><a href="admin.php?page=new_assignment">Nieuwe opdracht</a></li>'.
'<li><a href="admin.php?page=answers">Antwoorden</a></li>'.
'<li><a href="admin.php?page=logout">Uitloggen</a></li>'.
'</ol>';
}
else
return parent::generateIndex();
}
};
?>
Resultaat
Made by Thijs Aarnoudse