Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [CodeIgniter]Błędy przy nadaniu roli _check_perm
Forum PHP.pl > Forum > PHP > Frameworki
agata
Witam,
mam problem z rolami. Ogólnie role są rozpoznawane (admin/user) tylko w przypadku danej roli mam problem z wyświetleniem contentu z kodu poniżej:

Błąd dotyczy fuction index w którym otrzymuję komunikat : Message: Undefined index: content, Filename: controllers/site.php, Line Number: 107 (patrz komentarz).

Dziwne jest to że defakto wszystko jest wyświetlane poprawnie, tzn. jeżeli $perm = user -> wyświetla tylko "Zaloguj" (oraz błąd) a jeśli $perm = admin -> zawartość if (w tym momencie nie ma żadnego błędu).

Błąd jest tylko przy pierwszym ekranie i po wylogowaniu.

  1. class Site extends Controller
  2. {
  3. function Site()
  4. {
  5. parent::Controller();
  6. $this->load->model('User');
  7. $this->response = array();
  8. }
  9. // bazowy kontroler
  10. function index()
  11. {
  12.  
  13.  
  14. if(Site::_check_perm('admin')){
  15. //echo 'admin';
  16.  
  17.  
  18.  
  19. $users = $this->User->get_users()->result_array();
  20. if (isset($users[0]))
  21. {
  22. $this->response['content'] = $this->load->view('user_list', array('users' => $users), True);
  23. //echo 'user_list';
  24. }
  25. else
  26. {
  27. $this->response['content'] = 'Brak użytkowników<br /><div style="text-align:center;"><h2><a href="'.site_url('site/user_add/').'">Dodaj Użytkownika</a></h2></div>';
  28. }
  29. }
  30.  
  31.  
  32. IF(Site::_check_login())
  33. {
  34. $this->response['content'] .= '<br /><div style="text-align:center;"><h2><a href="'.site_url('site/logout/').'">Wyloguj</a></h2></div>';
  35. }
  36. else
  37. {
  38. $this->response['content'] .= '<br /><div style="text-align:center;"><h2><a href="'.site_url('site/login/').'">Zaloguj</a></h2></div>'; //------LINIA 107 !!
  39. }
  40. $this->load->view('index', $this->response);
  41. }
  42.  
  43. // dodanie użytkownika
  44. function user_add()
  45. {
  46. $data["login"] = array('name' => 'login');
  47. $data['haslo'] = array('name' => 'haslo');
  48. $data['email'] = array('name' => 'email');
  49.  
  50. $rules['login'] = "required|max_length[100]|xss_clean";
  51. $rules['haslo'] = "required|max_length[100]|xss_clean";
  52. $rules['email'] = "required|max_length[100]|xss_clean|valid_email";
  53. $this->validation->set_rules($rules);
  54. $ar = $this->User->get_user_by_login($this->input->post('login'))->result_array();
  55. if ($this->validation->run() == FALSE)
  56. {
  57. $data['login']['value'] = $this->input->post('login');
  58. $data['haslo']['value'] = $this->input->post('haslo');
  59. $data['email']['value'] = $this->input->post('email');
  60. $this->response['content'] = $this->load->view('user_add', $data, True);
  61. }
  62. else IF(!isset($ar[0]))
  63. {
  64. $this->User->add_user(array('user_login' => $this->input->post('login'), 'user_email' => $this->input->post('email'), 'user_password' => sha1(md5($this->input->post('haslo'))), 'user_active' => '1'));
  65. $this->response['content'] = '<h1>Dane zapisane</h1><META HTTP-EQUIV="Refresh" CONTENT="1; URL='.site_url('site').'">';
  66. }
  67. else
  68. {
  69. $this->response['content'] = '<h1>Użytkownik już istnieje w bazie danych</h1>';
  70. }
  71. $this->load->view('index', $this->response);
  72. }
  73. //edycja konta
  74. function user_edit()
  75. {
  76. $id = $this->uri->segment(3);
  77. IF(isset($id) and is_numeric($id))
  78. {
  79. $ar = $this->User->get_user_by_id($id)->result_array();
  80.  
  81. $data["login"] = array('name' => 'login');
  82. $data['haslo'] = array('name' => 'haslo');
  83. $data['email'] = array('name' => 'email');
  84. $data['perms'] = array('name' => 'perms');
  85.  
  86. $rules['login'] = "required|max_length[100]|xss_clean";
  87. $rules['perms'] = "xss_clean";
  88. $rules['haslo'] = "max_length[100]|xss_clean";
  89. $rules['email'] = "required|max_length[100]|xss_clean|valid_email";
  90. $this->validation->set_rules($rules);
  91.  
  92. if ($this->validation->run() == FALSE)
  93. {
  94. IF(strlen($this->input->post('tytul')) > 0)
  95. {
  96. $data['login']['value'] = $this->input->post('login');
  97. $data['haslo']['value'] = $this->input->post('haslo');
  98. $data['email']['value'] = $this->input->post('email');
  99. $data['perms']['value'] = $this->input->post('perms');
  100. }
  101. else
  102. {
  103. $data['login']['value'] = $ar[0]['user_login'];
  104. $data['email']['value'] = $ar[0]['user_email'];
  105. $data['perms']['value'] = $ar[0]['user_perms'];
  106. }
  107. $this->response['content'] = $this->load->view('user_edit', $data, True);
  108. }
  109. else
  110. {
  111. IF(strlen($this->input->post('haslo')) > 0)
  112. {
  113. $this->User->update_user($id, array('user_login' => $this->input->post('login'), 'user_email' => $this->input->post('email'), 'user_perms' => $this->input->post('perms'), 'user_password' => sha1(md5($this->input->post('haslo')))));
  114. }
  115. else
  116. {
  117. $this->User->update_user($id, array('user_login' => $this->input->post('login'), 'user_email' => $this->input->post('email'), 'user_perms' => $this->input->post('perms')));
  118. }
  119. $this->response['content'] = '<h1>Zmiany zapisane</h1><META HTTP-EQUIV="Refresh" CONTENT="1; URL='.site_url('site').'">';
  120. }
  121. }
  122. else
  123. {
  124. $this->response['content'] = '<h1>Niepoprawny URL</h1>';
  125. }
  126. $this->load->view('index', $this->response);
  127. }
  128.  
  129. // usunięcie usera
  130. function user_delete()
  131. {
  132. $id = $this->uri->segment(3);
  133. IF(isset($id) and is_numeric($id))
  134. {
  135. $this->User->delete_user($this->uri->segment(3));
  136. $this->response['content'] = '<h1>User skasowany</h1><META HTTP-EQUIV="Refresh" CONTENT="1; URL='.site_url('site').'">';
  137. $this->load->view('index', $this->response);
  138. }
  139. }
  140. ################################################################
  141. // czy jestem zalogowany
  142. function _check_login()
  143. {
  144. IF($cookie = $this->input->cookie('cicookie', True))
  145. {
  146. $dane = unserialize(base64_decode($cookie));
  147. IF(is_array($dane))
  148. {
  149. $ar = $this->User->get_user_by_login($dane['user'])->result_array();
  150. //echo $ar[0]['user_perms'];
  151. //echo 'sesja '.$this->session->set_userdata($ar[0]['user_perms']);
  152. // echo $rola;
  153.  
  154. IF(isset($ar[0]) and $ar[0]['user_last_login_ip'] == $this->input->ip_address() and sha1(md5($ar[0]['user_login'].$ar[0]['user_password'])) == $dane['pass'])
  155. {
  156. return true;
  157. }
  158. else
  159. {
  160. return false;
  161. }
  162. }
  163. else
  164. {
  165. return false;
  166. }
  167. }
  168. else
  169. {
  170. return false;
  171. }
  172. }
  173. // czy mam uprawnienie $perm
  174. function _check_perm($perm)
  175. {
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182. IF($cookie = $this->input->cookie('cicookie', True))
  183. {
  184. $dane = unserialize(base64_decode($cookie));
  185. IF(is_array($dane))
  186. {
  187. $ar = $this->User->get_user_by_login($dane['user'])->result_array();
  188.  
  189. IF(isset($ar[0]) and $ar[0]['user_last_login_ip'] == $this->input->ip_address() and sha1(md5($ar[0]['user_login'].$ar[0]['user_password'])) == $dane['pass'])
  190. {
  191. IF(strrchr($ar[0]['user_perms'], $perm))
  192. {
  193. return true;
  194. }
  195. else
  196. {
  197. return false;
  198. }
  199. }
  200. else
  201. {
  202. return false;
  203. }
  204. }
  205. else
  206. {
  207. return false;
  208. }
  209. }
  210. else
  211. {
  212. return false;
  213. }
  214. }
  215. // logowanie
  216. function login()
  217. {
  218. $data["login"] = array('name' => 'login');
  219. $data['pass'] = array('name' => 'pass');
  220.  
  221. $rules['login'] = "required|max_length[100]|xss_clean";
  222. $rules['pass'] = "required|max_length[100]|xss_clean";
  223. $this->validation->set_rules($rules);
  224.  
  225. $fields['login'] = 'Login';
  226. $fields['pass'] = 'Hasło';
  227.  
  228. $this->validation->set_fields($fields);
  229.  
  230.  
  231. if ($this->validation->run() == FALSE)
  232. {
  233. $data['login']['value'] = $this->input->post('login');
  234. $data['pass']['value'] = $this->input->post('pass');
  235. $this->response['content'] = $this->load->view('login', $data, True);
  236. }
  237. else
  238. {
  239. $ar = $this->User->get_user_by_login($this->input->post('login'))->result_array();
  240. IF(isset($ar[0]) and $ar[0]['user_password'] == sha1(md5($this->input->post('pass'))))
  241. {
  242. $a = array('user' => $this->input->post('login'), 'pass' => sha1(md5($this->input->post('login').$ar[0]['user_password'])));
  243. setcookie("cicookie", base64_encode(serialize($a)), time()+172800, '/', '', '0');
  244. $this->User->update_login_ip($this->input->post('login'), $this->input->ip_address());
  245. $this->response['content'] = '<h1>Zalogowany</h1><META HTTP-EQUIV="Refresh" CONTENT="1; URL='.site_url('site').'">';
  246. }
  247. else
  248. {
  249. $this->response['content'] = '<h1>Błąd logowania</h1><META HTTP-EQUIV="Refresh" CONTENT="1; URL='.site_url('site').'">';
  250. }
  251. }
  252. $this->load->view('index', $this->response);
  253. }
  254. //wylogowanie
  255. function logout()
  256. {
  257. setcookie("cicookie", NULL, time()-172800, '/', '', '0');
  258. header('Location: '.site_url('site'));
  259. }
  260. }


Nie mam pojęcia jak się pozbyć tego błędu proszę o pomoc i czym ten błąd jest spowodowany.

edit 1 :
udało mi się rozwiązać problem, co prawda metodą prób i błędów ale teraz działa.

W funkcji index() usunęłam kropki (.) przy response content a także usunięcie wartości True przy response->content = load->view.

Jak ktoś ma pomysł dlaczego tak się dzieje i czy może to zostać proszę o opinie.
derdiusz
Jeżeli wyłączysz raportowanie błędów to nie zobaczysz błędu "Undefined index". Dlaczego tak się dzieje? Gdy używasz operatora .= to dopisujesz wartość do już istniejącej.

W przypadku :

  1. $this->response['content'] .= 'coś';


zakładasz, że index/klucz "content" już znajduje się w tablicy $this->response. Jest to równoznaczne z:

  1. $this->response['content'] = $this->response['content'].'coś';


W Twoim kontrolerze zdefiniowane jest tylko:

  1. $this->$response = array();


Wystarczy, że konstruktorze zdefiniujesz index/klucz 'content' i problem zniknie:

  1. $this->response = array();
  2. $this->response['content'] = '';


lub

  1. $this->response = array(
  2. 'content' => ''
  3. );


Prawda taka, że błąd typu "undefined index" nie powoduje błędów w funkcjonowaniu aplikacji, informuje tylko programistę, że odwołuje się do wartości, której nie zdefiniował, a to może prowadzić do wycieków pamięci.

Ormin
Taka mała dygresja - HTML w Kontrolerze to nienajlepszy pomysł.

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.