login.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * File for the login action.
  4. *
  5. * Implements an action function to be called from the {@see Controller}.
  6. *
  7. * @category Action
  8. */
  9. /**
  10. * Logs the user in.
  11. *
  12. * Reads the POST parameters looking for the following KEYS:
  13. * uname
  14. * password
  15. * If there is a match, it sets cookies and session variables, and
  16. * redirects to the user homepage.
  17. *
  18. * @return int 0 on success, negative values on error.
  19. * @category Action
  20. * @global resource Database connection.
  21. */
  22. function action(){
  23. global $db;
  24. if (!isset($_POST['uname'], $_POST['password'])){
  25. header("Location: " . URL::BASE);
  26. exit();
  27. }
  28. $uname = SQLite3::escapeString($_POST['uname']);
  29. if (strlen($uname) == 0){
  30. header("Location: " . URL::BASE);
  31. exit();
  32. }
  33. $password = SQLite3::escapeString($_POST['password']);
  34. if (strlen($password) == 0){
  35. header("Location: " . URL::BASE);
  36. exit();
  37. }
  38. $password = hash('sha256', $password);
  39. $statement = $db->prepare("
  40. SELECT uid
  41. FROM player
  42. WHERE
  43. upper(name) = upper(:name) AND
  44. password = :password;
  45. ");
  46. $statement->bindValue(":name", $uname, SQLITE3_TEXT);
  47. $statement->bindValue(":password", $password, SQLITE3_TEXT);
  48. $statement->execute();
  49. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  50. if (!$r){
  51. header("Location: " . URL::BASE);
  52. exit();
  53. die();
  54. return 401;
  55. }
  56. $uid = $r["uid"];
  57. setcookie("uid", $uid, time() + 5 * 24 * 60 * 60); // 5 Days
  58. session_regenerate_id();
  59. $_SESSION['session'] = true;
  60. $_SESSION['name'] = $uname;
  61. $_SESSION['uid'] = $uid;
  62. header("Location: " . URL::BASE . "/$uid/");
  63. exit();
  64. die();
  65. return 0;
  66. }
  67. ?>