Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [CI] Początki
Forum PHP.pl > Forum > PHP > Frameworki
boddah85
Witam

Długo się przymierzałem do wyboru jakiegoś FW i trafiło w końcu na CI. Na początek postanowiłem więc pobawić się w prościutki system do autoryzacji. Jako że jest to moje pierwsze podejście do FW to prosiłbym o info czy w ogóle idę w dobrym kierunku. Mam nadzieję, że to dobre miejsce na tego typu temat smile.gif

Kontrolery
  1. class Main extends CI_Controller
  2. {
  3. public function index()
  4. {
  5. $data['title'] = 'Ogłoszenia | ver 0.1b';
  6. $data['link']['main'] = anchor(base_url(),'strona główna');
  7. if($this->session->userdata('s_loggedin')==FALSE)
  8. {
  9. $data['link'][] = anchor('/user/login', 'zaloguj');
  10. $data['link'][] = anchor('/user/register', 'zarejestruj konto');
  11. }
  12. else
  13. {
  14. $data['link'][] = anchor('/user', 'panel użytkownika');
  15. $data['link'][] = anchor('/user/logout', 'wyloguj');
  16. }
  17. $this->load->view('template.php',$data);
  18. }
  19. }


  1. class User extends CI_Controller
  2. {
  3. /*
  4.   * Konstruktor glownie po to, zeby zaladowac model
  5.   */
  6. function __construct()
  7. {
  8. parent::__construct();
  9. $this->load->model('user_model');
  10. }
  11. /*
  12.   *
  13.   */
  14. public function index()
  15. {
  16. if($this->session->userdata('s_loggedin')==FALSE)
  17. {
  18. redirect('/user/login', 'location');
  19. }
  20. $query = $this->user_model->get_user_by_id($this->session->userdata('s_userid'));
  21. if($query->num_rows()==1)
  22. {
  23. $data = array();
  24. $data['user'] = $query->row();
  25. $this->load->view('user.php', $data);
  26. }
  27. else
  28. {
  29. redirect('/user/register','location');
  30. }
  31. }
  32. /*
  33.   * LOGOWANIE
  34.   */
  35. public function login()
  36. {
  37. if($this->session->userdata('s_loggedin')==TRUE)
  38. {
  39. redirect('/user', 'location');
  40. }
  41. $this->form_validation->set_rules('username', 'Login', 'trim|required|min_length[5]|maxlength[15]|alpha_numeric');
  42. $this->form_validation->set_rules('password', 'Hasło', 'trim|required|alpha_numeric|md5|min_length[5]');
  43. if($this->form_validation->run()==TRUE)
  44. {
  45. $query = $this->user_model->get_logindata($this->input->post('username'),$this->input->post('password'));
  46. if($query->num_rows()==1)
  47. {
  48. $data = $query->row();
  49. $s_data = array(
  50. 's_userid' => $data->user_id,
  51. 's_username' => $data->user_name,
  52. 's_loggedin' => TRUE
  53. );
  54. $this->session->set_userdata($s_data);
  55. redirect('/user', 'location');
  56. }
  57. else
  58. {
  59. redirect('/user/login', 'location');
  60. }
  61. }
  62. else
  63. {
  64. $this->load->view('form_login');
  65. }
  66. }
  67. /*
  68.   * WYLOGOWANIE
  69.   */
  70. public function logout()
  71. {
  72. if($this->session->userdata('s_loggedin')==TRUE)
  73. {
  74. $this->session->sess_destroy();
  75. redirect('/user', 'location');
  76. }
  77. else
  78. {
  79. redirect('/user/login', 'location');
  80. }
  81. }
  82. /*
  83.   * REJESTRACJA
  84.   */
  85. public function register()
  86. {
  87. if($this->session->userdata('s_loggedin')==TRUE)
  88. {
  89. redirect('/user', 'location');
  90. }
  91. $this->form_validation->set_rules('username', 'Login', 'trim|required|min_length[5]|maxlength[15]|alpha_numeric|callback__username_check');
  92. $this->form_validation->set_rules('password', 'Hasło', 'trim|required|alpha_numeric|md5|min_length[5]');
  93. $this->form_validation->set_rules('passconf', 'Potwierdzenie hasła', 'trim|required|matches[password]');
  94. //$this->form_validation->set_rules('captcha', 'Kod z obrazka', 'trim|required|callback__check_captcha');
  95. $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
  96.  
  97. if($this->form_validation->run()==FALSE)
  98. {
  99. $this->load->view('form_reg');
  100. }
  101. else
  102. {
  103. $data = array(
  104. 'user_name' => strtolower($this->input->post('username')),
  105. 'user_pass' => $this->input->post('password'),
  106. 'user_mail' => $this->input->post('email')
  107. );
  108. $this->user_model->insert_user($data);
  109. $this->load->view('form_reg_succ');
  110. }
  111. }
  112. #############################
  113. /*
  114.   * CALLBACKI
  115.   */
  116. #############################
  117. /*
  118.   * funkcja spr czy user wystepuje - potrzebna przy walidacji formularza przy rejestracji
  119.   */
  120. function _username_check($str)
  121. {
  122. if($this->user_model->get_user_by_login($this->input->post('username')))
  123. {
  124. $this->form_validation->set_message('_username_check', 'Wybrany login jest już w bazie danych! Wybierz inny!');
  125. return FALSE;
  126. }
  127. else
  128. {
  129. return TRUE;
  130. }
  131. }
  132.  
  133. }


Model
  1. class User_model extends CI_Model
  2. {
  3. function __construct()
  4. {
  5. parent::__construct();
  6. }
  7. /*
  8.   * pobierz usera po loginie - do spr aut.
  9.   */
  10. function get_user_by_login($login)
  11. {
  12. $q = "SELECT user_id FROM ci_users WHERE user_name = ?";
  13. $r = $this->db->query($q, $login);
  14. if($r->num_rows()==1)
  15. return TRUE;
  16. else
  17. return FALSE;
  18. }
  19. /*
  20.   * pobierz usera po id
  21.   */
  22. function get_user_by_id($id)
  23. {
  24. $q = "SELECT * FROM ci_users where user_id = ?";
  25. return $this->db->query($q,$id);
  26. }
  27.  
  28. /*
  29.   * sprawdz czy login i haslo sie zgadzaja
  30.   */
  31. function get_logindata($login,$pass)
  32. {
  33. $q = "SELECT user_id, user_name FROM ci_users WHERE user_name = ? AND user_pass = ?";
  34. return $this->db->query($q, array($login,$pass));
  35. }
  36.  
  37. /*
  38.   * dodaj usera do bazy
  39.   */
  40. function insert_user($data)
  41. {
  42. return $this->db->insert('ci_users', $data);
  43. }
  44. }


Być może komuś się będzie chciało to prześledzić. Za wszelkie uwagi/sugestię etc z góry dzięki.
krzotr
Jedna mała uwaga ode mnie:
  1. function __construct()
  2. public function index()
  3. function _username_check($str)


Zdecyduj się, czy stosujesz modyfikatory dostępu (public, private, protected), czy nie.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.