Posted By

willemmulder on 08/19/09


Tagged

login controller kohana


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

umang_nine
jackkeller
NARKOZ
JSawo
jose_506


kohana login controller


 / Published in: PHP
 

  1. <?php
  2.  
  3. class Login_Controller extends Template_Controller {
  4.  
  5. public $template = "templates/template";
  6.  
  7. //make sure every user that uses this Controller is logged in
  8. public function __construct() {
  9. parent::__construct();
  10.  
  11. $this->session = Session::instance();
  12. $this->auth = new Auth();
  13.  
  14. if ($this->auth->logged_in()) {
  15. //logged in
  16. //that's all right
  17. $this->user = $this->auth->get_user();
  18. $this->session->set("userid", $this->user->id);
  19. } else {
  20. //not logged in
  21.  
  22. //try to auto-login
  23. if ($this->auth->auto_login() == TRUE) {
  24. //all right, logged in
  25. $this->user = $this->auth->get_user();
  26. $this->session->set("userid", $this->user->id);
  27. } else {
  28. if (url::current() != "login/login") {
  29. //set this page as 'last requested' and redirect to login-page
  30. $this->session->set("requested_url","/".url::current());
  31. url::redirect("login/login"); //redirect to login-page
  32. } else {
  33. //we're already on the login-page (maybe even with a POST), so no redirect necessary!
  34. }
  35. }
  36. }
  37.  
  38. //create template
  39. //edit: use Template_Controller instead
  40. //$this->template = new View($this->template);
  41.  
  42. //add a Profiler for extra debug information if you want
  43. //$profiler = new Profiler();
  44.  
  45. }
  46.  
  47. public function index() {
  48. url::redirect("login/login");
  49. }
  50.  
  51. public function login() {
  52. $this->template->title = "Login to Wi3";
  53. //try to login user if $_POST is supplied
  54. $form = $_POST;
  55. if($form){
  56. if ($this->auth->login($form['username'], $form['password'], TRUE)) //set remember option to TRUE
  57. {
  58. // Login successful, redirect
  59. if ($this->session->get("requested_url") != null AND $this->session->get("requested_url") != "login/login") {
  60. url::redirect($this->session->get("requested_url")); //return to page where login was called
  61. } else {
  62. url::redirect(""); //redirect to home-page
  63. }
  64. }
  65. else
  66. {
  67. $this->template->content = '<p>Login failed.</p>';
  68. $this->template->content .= new View("login");
  69. return;
  70. }
  71. }
  72.  
  73. if ($this->auth->logged_in()) {
  74. url::redirect(""); //redirect to homepage
  75. //$this->template->content = 'You are logged in as ' . $this->auth->get_user()->username;
  76. return;
  77. }
  78.  
  79. $this->template->content = new View("login");
  80. }
  81.  
  82. public function logout() {
  83. $this->auth->logout(TRUE);
  84. url::redirect("/");
  85. }
  86.  
  87. }
  88.  
  89. ?>

Report this snippet  

You need to login to post a comment.