/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php class Login_Controller extends Template_Controller { public $template = "templates/template"; //make sure every user that uses this Controller is logged in public function __construct() { parent::__construct(); $this->session = Session::instance(); $this->auth = new Auth(); if ($this->auth->logged_in()) { //logged in //that's all right $this->user = $this->auth->get_user(); $this->session->set("userid", $this->user->id); } else { //not logged in //try to auto-login if ($this->auth->auto_login() == TRUE) { //all right, logged in $this->user = $this->auth->get_user(); $this->session->set("userid", $this->user->id); } else { //set this page as 'last requested' and redirect to login-page url::redirect("login/login"); //redirect to login-page } else { //we're already on the login-page (maybe even with a POST), so no redirect necessary! } } } //create template //edit: use Template_Controller instead //$this->template = new View($this->template); //add a Profiler for extra debug information if you want //$profiler = new Profiler(); } public function index() { url::redirect("login/login"); } public function login() { $this->template->title = "Login to Wi3"; //try to login user if $_POST is supplied $form = $_POST; if($form){ if ($this->auth->login($form['username'], $form['password'], TRUE)) //set remember option to TRUE { // Login successful, redirect if ($this->session->get("requested_url") != null AND $this->session->get("requested_url") != "login/login") { url::redirect($this->session->get("requested_url")); //return to page where login was called } else { url::redirect(""); //redirect to home-page } } else { $this->template->content = '<p>Login failed.</p>'; $this->template->content .= new View("login"); return; } } if ($this->auth->logged_in()) { url::redirect(""); //redirect to homepage //$this->template->content = 'You are logged in as ' . $this->auth->get_user()->username; return; } $this->template->content = new View("login"); } public function logout() { $this->auth->logout(TRUE); url::redirect("/"); } } ?>