/controllers/SessionController.php

https://github.com/cwooijoon/rmitwda-digicel-59432e7abb0f68236a8f955756408c64412accce · PHP · 71 lines · 47 code · 9 blank · 15 comment · 5 complexity · 3cfd1212222e5c86104b31e2e48e9e65 MD5 · raw file

  1. <?php
  2. /**
  3. * This is the Session Controller class.
  4. *
  5. * @author donal.ellis@rmit.edu.au
  6. */
  7. session_start();
  8. require_once(LIBRARY_PATH . DS . 'Template.php');
  9. require_once(APP_PATH . DS . 'models/User.php');
  10. class SessionController {
  11. public function __construct() {
  12. $this->template = new Template;
  13. $this->template->template_dir = APP_PATH . DS . 'views' . DS . 'session' . DS;
  14. $this->template->title = 'Log in';
  15. }
  16. public function add() {
  17. if (isset($_SESSION['session']['error'])) {
  18. $this->template->error = $_SESSION['session']['error'];
  19. unset($_SESSION['session']['error']);
  20. }
  21. $this->template->display('add.php');
  22. }
  23. public function create() {
  24. // TODO
  25. // get username and password
  26. // and validate them against values in db
  27. if (!$user = User::retrieve(array('username' => $_POST['username']))) {
  28. // user doesn't exist
  29. // redirect back to login page
  30. $_SESSION['session']['error'] = "You cannot login with those details";
  31. header("Location: /Test/session/new");
  32. exit;
  33. }
  34. if ($_POST['password'] != $user->password) {
  35. // password is wrong
  36. // redirect back to login page
  37. $_SESSION['session']['error'] = "You cannot login with those details";
  38. header("Location: /Test/session/new");
  39. exit;
  40. }
  41. // credentials are correct
  42. // add user to session
  43. // redirect to users show page
  44. $_SESSION['user']['user_id'] = $user->user_id;
  45. $_SESSION['user']['account_type_id'] = $user->account_type_id;
  46. header("Location: /Test/users/{$user->user_id}");
  47. exit;
  48. }
  49. public function destroy() {
  50. $_SESSION = array();
  51. if (ini_get('session.use_cookies')) {
  52. $params = session_get_cookie_params();
  53. setcookie(session_name(), '', time() - 42000,
  54. $params['path'], $params['domain'],
  55. $params['secure'], $params['httponly']
  56. );
  57. }
  58. session_destroy();
  59. header("Location: /Test/session/new", false);
  60. exit;
  61. }
  62. }