Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [skrypt] uniwersalny skrypcik logowania
epud
post
Post #1





Grupa: Zarejestrowani
Postów: 5
Pomógł: 0
Dołączył: 5.06.2006

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


jako, że zaczynam dopiero zabawe z obiektowym php prosze o ocene skrypciku na ponize, jest to "uniwersalny" skrypcik logowania uzytkownika do systemu... zalozenie bylo takie abym mogl to prosto przystosowac do innych projetkow bez za duzego grzebania w kodzie, dla tego podajemy nazwe tabeli z uzytkownikami, i nazwe pol z loginem i haslem

co myslicie nadaje sie?

a pod wzgledem obiektowym?

  1. <?php 
  2. class auth{
  3. var $db = NULL;
  4. var $table_name;
  5. var $login_name;
  6. var $pass_name;
  7. var $md5;
  8. var $login;
  9. var $pass;
  10. var $dbpass;
  11. var $errors = array();
  12. public function __construct($db, $table_name, $login_name, $pass_name, $md5 = false){
  13. //$_SESSION['isLoggedIn'] = false;
  14. $this -> db = $db;
  15. $this -> table_name = $table_name;
  16. $this -> login_name = $login_name;
  17. $this -> pass_name = $pass_name;
  18. $this -> md5 = $md5;
  19. }
  20. public function authorize($login, $pass){
  21. if(empty($login) || empty($pass)){
  22. array_push($this -> errors, 'login i hasło nie mogą być puste');
  23. return false;
  24. }
  25. $this -> login = $login;
  26. if($this -> md5){
  27. $this -> pass = md5($pass);
  28. }
  29. else{
  30. $this -> pass = $pass;
  31. }
  32. if($this -> getPass()){
  33. if($this -> pass == $this -> dbpass){
  34. $_SESSION['isLoggedIn'] = true;
  35. $_SESSION['userData'] = $this -> getUserData();
  36. header("Location: index.php");
  37. }
  38. else{
  39. array_push($this -> errors, 'błędny login lub hasło');
  40. }
  41. }
  42. else{
  43. array_push($this -> errors, 'błędny login lub hasło');
  44. }
  45. }
  46. private function getPass(){
  47. $this -> dbpass = $this -> db -> GetOne("SELECT ". $this -> pass_name ." FROM ". $this -> table_name ." WHERE ". $this -> login_name." = '".$this -> login."'");
  48. if($this -> dbpass){
  49. return true;
  50. }
  51. return false;
  52. }
  53. public function isLoggedIn(){
  54. return $_SESSION['isLoggedIn'];
  55. }
  56. private function getUserData(){
  57. return trim_array($this -> db -> GetRow("SELECT * FROM ".$this -> table_name." WHERE ".$this -> login_name." = '".$this -> login."'"));
  58. }
  59. }
  60. ?>


sprawdzanie czy zalogowany czy nie..

  1. <?php 
  2. $auth = new auth($db, PREFIX."users", 'user_name', 'user_pass', true);
  3. /* END AUTH */
  4.  
  5. if($auth -> isLoggedIn()){//jesli zalogowany
  6. $smarty -> display('index.tpl');
  7. }
  8. else{//jesli nie zalogowany
  9. $smarty -> display('loginForm.tpl');
  10. }//if($auth -> isLoggedIn())
  11. ?>

wersyfikacjia usera
  1. <?php
  2. $auth = new auth($db, PREFIX."users", 'user_name', 'user_pass', true);
  3. $auth -> authorize($_POST['login']['user_name'], $_POST['login']['user_pass']);
  4. ?>



z gory dzieki za wszelkie wypowiedzi (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

Ten post edytował epud 5.06.2006, 18:03:37
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
TomASS
post
Post #2





Grupa: Zarejestrowani
Postów: 1 660
Pomógł: 13
Dołączył: 9.06.2004
Skąd: Wrocław i okolice

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


Bardzo mądrze (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) A jak wygląda ta funkcja trim_array?
Go to the top of the page
+Quote Post
epud
post
Post #3





Grupa: Zarejestrowani
Postów: 5
Pomógł: 0
Dołączył: 5.06.2006

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


Cytat(TomASS @ 6.06.2006, 11:30 ) *
Bardzo mądrze (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) A jak wygląda ta funkcja trim_array?



tak o:

  1. <?php 
  2. function trim_array($array){
  3. $new = array();
  4. foreach($array as $a => $row){
  5. if(is_array($row)){
  6. $new[$a] = trim_array($row);
  7. }
  8. else{
  9. $new[$a] = trim($row);
  10. $new[$a] = str_replace("SELECT", " ", $row);
  11. $new[$a] = str_replace("INSERT", " ", $row);
  12. $new[$a] = str_replace("DELETE", " ", $row);
  13. $new[$a] = str_replace("UPDATE", " ", $row);
  14. }
  15. }
  16. return $new;
  17. }
  18. ?>


hm.. tylko wlasnie chyba stwierdzilem ze nie zadziala jak ktos poscic w teks SeLeCt a baza i tak to zinterpetuje chyba (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

ma ktos jakis pomysł jak to porpawic

a powracajac do klasy dodalem TIMEOUTA po ktorym wylogowuje po czasie bezczynnosc:

  1. <?php 
  2. /**
  3.  * Unwersalna autrozacja uzytkownia
  4.  * @author Acid
  5.  * @version 1.0
  6.  * 
  7.  */
  8. class auth{
  9. var $db = NULL;
  10. var $table_name;
  11. var $login_name;
  12. var $pass_name;
  13. var $md5;
  14. var $login;
  15. var $pass;
  16. var $dbpass;
  17. var $sessionTime;
  18. var $errors = array();
  19. /**
  20.  * Konstruktor klasy
  21.  *
  22.  * @param object $db
  23.  * @param string $table_name
  24.  * @param string $login_name
  25.  * @param string $pass_name
  26.  * @param bool $md5
  27.  */
  28. public function __construct($db, $table_name, $login_name, $pass_name, $sessionTime = '1200',$md5 = false){
  29. $this -> db = $db;
  30. $this -> table_name = $table_name;
  31. $this -> login_name = $login_name;
  32. $this -> pass_name = $pass_name;
  33. $this -> sessionTime =$sessionTime;
  34. $this -> md5 = $md5;
  35. }
  36. /**
  37.  * Autoryzacja
  38.  *
  39.  * @param string $login
  40.  * @param string $pass
  41.  * @return bool
  42.  */
  43. public function authorize($login, $pass){
  44. if(empty($login) || empty($pass)){
  45. array_push($this -> errors, 'login i hasło nie mogą być puste');
  46. return false;
  47. }
  48. $this -> login = $login;
  49. if($this -> md5){
  50. $this -> pass = md5($pass);
  51. }
  52. else{
  53. $this -> pass = $pass;
  54. }
  55. if($this -> getPass()){
  56. if($this -> pass == $this -> dbpass){
  57. $_SESSION['isLoggedIn'] = true;
  58. $_SESSION['beginTime'] = time();
  59. $_SESSION['userData'] = $this -> getUserData();
  60. header("Location: index.php");
  61. }
  62. else{
  63. array_push($this -> errors, 'błędny login lub hasło');
  64. }
  65. }
  66. else{
  67. array_push($this -> errors, 'błędny login lub hasło');
  68. }
  69. }
  70. /**
  71.  * pobieranie hasła z bazy
  72.  *
  73.  * @return bool
  74.  */
  75. private function getPass(){
  76. $this -> dbpass = $this -> db -> GetOne("SELECT ". $this -> pass_name ." FROM ". $this -> table_name ." WHERE ". $this -> login_name." = '".$this -> login."'");
  77. if($this -> dbpass){
  78. return true;
  79. }
  80. return false;
  81. }
  82. /**
  83.  * Sprawdzanie czy zalogowany
  84.  *
  85.  * @return bool
  86.  */
  87. public function isLoggedIn(){
  88. if(isset($_SESSION['isLoggedIn']) && $this -> sessionTimeOut()){
  89. return true;
  90. }
  91. return false;
  92. }
  93. /**
  94.  * Pobieranie danych uzytkownia z bazy
  95.  *
  96.  * @return array mixed
  97.  */
  98. private function getUserData(){
  99. return trim_array($this -> db -> GetRow("SELECT * FROM ".$this -> table_name." WHERE ".$this -> login_name." = '".$this -> login."'"));
  100. }
  101. /**
  102.  * Wylogowywanie
  103.  *
  104.  */
  105. public function logOut(){
  106. unset($_SESSION);
  107. header("Location: index.php");
  108. }
  109. private function sessionTimeOut(){
  110. if($_SESSION['beginTime'] + $this -> sessionTime < time()){
  111. unset($_SESSION);
  112. array_push($this ->errors, 'przekroczyłeś czas bezczynnosc');
  113. return false;
  114. }
  115. return true;
  116. }
  117. }
  118. ?>
Go to the top of the page
+Quote Post

Posty w temacie


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: 15.10.2025 - 23:31