Return to Snippet

Revision: 16967
at August 19, 2009 11:26 by willemmulder


Initial Code
<?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 {
                    if (url::current() != "login/login") {
                        //set this page as 'last requested' and redirect to login-page
                        $this->session->set("requested_url","/".url::current());
                        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("/");
        }
        
    }
    
?>

Initial URL

                                

Initial Description

                                

Initial Title
kohana login controller

Initial Tags
login

Initial Language
PHP