Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Wskazówki do klasy
GameMaker
post
Post #1





Grupa: Zarejestrowani
Postów: 149
Pomógł: 2
Dołączył: 31.01.2010
Skąd: Konstancin-Jeziorna

Ostrzeżenie: (0%)
-----


Wczoraj przysiadłem do OOP i napisałem klasę , która sprawdza czy user jest zalogowany i nadaje mu poziomy dostępu.

Proszę o wskazówki :

  1.  
  2.  
  3.  
  4.  
  5. class User {
  6. global $db;
  7. public $logged_in = null;
  8. public $user_level = null;
  9.  
  10.  
  11. function __construct()
  12. {
  13.  
  14. $this->startSession();
  15. }
  16.  
  17.  
  18. private function loginCheck()
  19. {
  20. if (isset($_SESSION['id']) && $_SESSION['id'] > 0) {
  21.  
  22.  
  23. return true;
  24. } else {
  25. return false;
  26. }
  27. }
  28.  
  29. private function levelCheck()
  30. {
  31. $id = intval($_SESSION['id']);
  32. $q = $db->query("Select * from mpa_users where id='$id'");
  33.  
  34. if($db->num_rows($q) == 1)
  35. {
  36. $row = $db->fetch_array($q);
  37.  
  38. if($row['admin'] == 1)
  39. {
  40. return 7;
  41. }elseif($row['admin'] == 0 && $row['premium'] == 1 && $row['premium_time'] > time())
  42. {
  43.  
  44. return 2;
  45.  
  46. }elseif($row['admin'] == 0 && $row['premium'] == 1 && $row['premium_time'] < time())
  47. {
  48. $db->query("Update mpa_users set premium='0' where id='$id'");
  49.  
  50. return 1;
  51.  
  52. }elseif($row['admin'] == 0 && $row['premium'] == 0)
  53. {
  54.  
  55. return 1;
  56. }
  57.  
  58. }
  59. else
  60. {
  61.  
  62. }
  63.  
  64. }
  65.  
  66.  
  67. private function startSession()
  68. {
  69. $this->logged_in = $this->loginCheck();
  70. if($this->logged_in)
  71. {
  72. $this->user_level = $this->levelCheck();
  73. }
  74. else
  75. {
  76. $this->user_level = 0;
  77. }
  78.  
  79. }
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88. }
  89.  


Ten post edytował GameMaker 9.10.2011, 07:56:42
Go to the top of the page
+Quote Post
IceManSpy
post
Post #2





Grupa: Zarejestrowani
Postów: 1 006
Pomógł: 111
Dołączył: 23.07.2010
Skąd: Kraków

Ostrzeżenie: (0%)
-----


1. Nie ma już zmiennych global.
2. Wszystkie prywatne funkcje, jak chcesz to w kodzie wywołać?
Go to the top of the page
+Quote Post
GameMaker
post
Post #3





Grupa: Zarejestrowani
Postów: 149
Pomógł: 2
Dołączył: 31.01.2010
Skąd: Konstancin-Jeziorna

Ostrzeżenie: (0%)
-----


deklaruje klase , a potem zmienna logged_in i user_level sprawdzam dostep do aplikacji //

z tego co wyczytałem global daje dostep innej klasie zadeklarowanej w zmiennej (wczesniej) do danej klasy

Ten post edytował GameMaker 9.10.2011, 09:47:24
Go to the top of the page
+Quote Post
Spawnm
post
Post #4





Grupa: Moderatorzy
Postów: 4 069
Pomógł: 497
Dołączył: 11.05.2007
Skąd: Warszawa




Wszystko źle (IMG:style_emoticons/default/smile.gif)
$db przekazujesz przez dependency injection a nie global (dziwne że ci błędu nie wywaliło).
Czemu klasa autoryzacji zajmuje się zadaniami klasy Session?
Daj
  1. __construct(Db $db, Session $session)
  2. {
  3. $this -> _db = $db;
  4. $this -> _session = $session;
  5. }

Go to the top of the page
+Quote Post
GameMaker
post
Post #5





Grupa: Zarejestrowani
Postów: 149
Pomógł: 2
Dołączył: 31.01.2010
Skąd: Konstancin-Jeziorna

Ostrzeżenie: (0%)
-----


Na razie sesje zostawie w tej klasie , po poprawkach :

  1.  
  2. class User {
  3.  
  4. public $logged_in = null;
  5. public $user_level = null;
  6.  
  7.  
  8. function __construct($db)
  9. {
  10. $this->_db = $db;
  11. $this->startSession();
  12. }
  13.  
  14.  
  15. private function loginCheck()
  16. {
  17. if (isset($_SESSION['id']) && $_SESSION['id'] > 0) {
  18.  
  19.  
  20. return true;
  21. } else {
  22. return false;
  23. }
  24. }
  25.  
  26. private function levelCheck()
  27. {
  28. $id = intval($_SESSION['id']);
  29. $q = $this->_db->query("Select * from mpa_users where id='$id'");
  30.  
  31. if($this->_db->num_rows($q) == 1)
  32. {
  33. $row = $this->_db->fetch_array($q);
  34.  
  35. if($row['admin'] == 1)
  36. {
  37. return 7;
  38. }elseif($row['admin'] == 0 && $row['premium'] == 1 && $row['premium_time'] > time())
  39. {
  40.  
  41. return 2;
  42.  
  43. }elseif($row['admin'] == 0 && $row['premium'] == 1 && $row['premium_time'] < time())
  44. {
  45. $this->_db->query("Update mpa_users set premium='0' where id='$id'");
  46.  
  47. return 1;
  48.  
  49. }elseif($row['admin'] == 0 && $row['premium'] == 0)
  50. {
  51.  
  52. return 1;
  53. }
  54.  
  55. }
  56. else
  57. {
  58.  
  59. }
  60.  
  61. }
  62.  
  63.  
  64. private function startSession()
  65. {
  66. $this->logged_in = $this->loginCheck();
  67. if($this->logged_in)
  68. {
  69. $this->user_level = $this->levelCheck();
  70. }
  71. else
  72. {
  73. $this->user_level = 0;
  74. }
  75.  
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. }
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.08.2025 - 08:44