Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] Połączenie dwóch funkcji
topcio
post
Post #1





Grupa: Zarejestrowani
Postów: 140
Pomógł: 0
Dołączył: 14.01.2017

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


Witajcie

Czy można jakoś połączyć poniższe funkcje w jedną, jakoś samemu nie potrafię, dlatego zrobiłem dwie oddzielne, ale chyba lepiej jest jak jest mniej zapytań do bazy zwłaszcza takich samych.

  1. public function login($data) {
  2. $data['user_pass'] = $this->code_passwd($data['user_pass']);
  3. $result = $this->sql->query("SELECT user_id FROM users WHERE user_login = '{$data['user_login']}' AND user_pass = '{$data['user_pass']}' LIMIT 1");
  4. if($result->num_rows > 0) {
  5. return $result->fetch_row()[0];
  6. } else {
  7. return ['Podany login i/lub hasło są niepoprawne. Spróbuj Ponownie. <br />' .
  8. 'Zapomniałem <a href="index.php?action=forgetten">Hasła.</a>'];
  9. }
  10. }
  11.  
  12. public function loged_in($data) {
  13. $data['user_pass'] = $this->code_passwd($data['user_pass']);
  14. $result = $this->sql->query("SELECT user_id FROM users WHERE user_login = '{$data['user_login']}' AND user_pass = '{$data['user_pass']}' LIMIT 1");
  15. $value = mysqli_fetch_object($result);
  16. $user_id = $value->user_id;
  17. $query = "
  18. UPDATE users
  19. SET
  20. user_last_login_IP = '" . ip2long($this->getClientIP()) . "'
  21. WHERE user_id = '{$user_id}'
  22. ";
  23. $result = $this->sql->query($query);
  24. if($result === false) {
  25. $errors[] = "Wystąpił błąd z bazą danych.";
  26. return $errors;
  27. }
  28. }


A może po prostu w drugiej funkcji sprawdzę czy pierwsza jest true (IMG:style_emoticons/default/questionmark.gif) ?
Zaraz to wypróbuję.
Go to the top of the page
+Quote Post
Tomplus
post
Post #2





Grupa: Zarejestrowani
Postów: 1 883
Pomógł: 231
Dołączył: 20.03.2005
Skąd: Będzin

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


Nie jest czymś złym mieć dużo funkcji.
Szczególnie jeżeli każda robi co innego.

dlatego kod poniżej powinien być w osobnej funkcji
  1. $data['user_pass'] = $this->code_passwd($data['user_pass']);
  2. $result = $this->sql->query("SELECT user_id FROM users WHERE user_login = '{$data['user_login']}' AND user_pass = '{$data['user_pass']}' LIMIT 1");


W ogóle, nie ma potrzeby tworzyć zmiennych ponad stan, jeżeli ich nie używasz w innych miejscach skryptu.
Go to the top of the page
+Quote Post
topcio
post
Post #3





Grupa: Zarejestrowani
Postów: 140
Pomógł: 0
Dołączył: 14.01.2017

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


Dobrze zrozumiałem, że w tej drugiej funkcji powinienem odnieść się do funkcji login, która de facto sprawdza i loguje klienta
mniej więcej coś takiego
  1. if($this->login() === true) {
  2. echo "OK" ;
  3. } else {
  4. echo "NIE OK";
  5. }

Czy dobrze rozumiem
Go to the top of the page
+Quote Post
nospor
post
Post #4





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Twoja funkcja login() nie zwraca ani true ani false. Wiec albo popraw to co ona zwraca albo popraw warunek. Lepiej jakbys poprawil to co zwraca
Go to the top of the page
+Quote Post
topcio
post
Post #5





Grupa: Zarejestrowani
Postów: 140
Pomógł: 0
Dołączył: 14.01.2017

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


Muszę to jutro na spokojnie przemyśleć, bo w chwili obecnej funkcja login() zwraca ID użytkownika, szkoda że nie da się zrobić tak by zwracała dwie wartości, tzn ID i true
Go to the top of the page
+Quote Post
nospor
post
Post #6





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




A po co ma zwracac ID i true? Bez sensu.
Niech zwraca ID lub null gdy usera nie ma.

  1. if($result->num_rows > 0) {
  2. return (int) $result->fetch_row()[0];
  3. }
  4. return null;


A potem sprawdzanie

  1. if($this->login() !== null) {
  2. echo "OK" ;
  3. } else {
  4. echo "NIE OK";
  5. }
Go to the top of the page
+Quote Post
topcio
post
Post #7





Grupa: Zarejestrowani
Postów: 140
Pomógł: 0
Dołączył: 14.01.2017

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


Zrobiłem tak
  1. case 'log_in':
  2. if($users->is_logged()) {
  3. $data = $users->get_data();
  4. if($data['user_range'] == 1) {
  5. header("Location: index.php?action=user_login");
  6. }
  7. if ($data['user_range'] == 2) {
  8. header("Location: index.php?action=admin_login");
  9. }
  10. if ($data['user_range'] == 3) {
  11. header("Location: index.php?action=devel_login");
  12. }
  13. } else {
  14. echo "<form action=\"index.php?action=log_in_failed\" method=\"post\">";
  15. echo "<input type=\"text\" placeholder=\"Login\" name=\"user_login\">";
  16. echo "<input type=\"password\" placeholder=\"Hasło\" name=\"user_pass\">";
  17. echo "<input type=\"submit\" value=\"Zaloguj\">";
  18. echo "</form>";
  19. }
  20. break;
  21.  
  22. case 'log_in_failed':
  23. if(!empty($_POST)) {
  24. $result = $users->login($_POST);
  25. if(is_numeric($result)) {
  26. $_SESSION['user_id'] = $result;
  27. $users->loged_in($_SESSION['user_id']);
  28. header("Location: index.php?action=log_in");
  29. } else {
  30. echo "<form action=\"index.php?action=log_in_failed\" method=\"post\">";
  31. echo "<input type=\"text\" placeholder=\"Login\" name=\"user_login\">";
  32. echo "<input type=\"password\" placeholder=\"Hasło\" name=\"user_pass\">";
  33. echo "<input type=\"submit\" value=\"Zaloguj\">";
  34. echo "</form>";
  35. echo implode('<br>',$result).'</p>';
  36. }
  37. }
  38. break;


Może być?
Bo gdybym zrobił, że null jak nie zalogowany, to by mi nie wyświetliło komunikatu, że błędny login albo hasło.
A tak sprawdza czy jest numer.
A drugą funkcję skróciłem do takiej postaci
  1. public function loged_in() {
  2. $query = "
  3. UPDATE users
  4. SET
  5. user_last_login_IP = '" . ip2long($this->getClientIP()) . "'
  6. WHERE user_id = '{$_SESSION['user_id']}'
  7. ";
  8. $result = $this->sql->query($query);
  9. if($result === false) {
  10. $errors[] = "Wystąpił błąd z bazą danych.";
  11. return $errors;
  12. }
  13. }


Ten post edytował topcio 29.04.2017, 17:09:20
Go to the top of the page
+Quote Post
Tomplus
post
Post #8





Grupa: Zarejestrowani
Postów: 1 883
Pomógł: 231
Dołączył: 20.03.2005
Skąd: Będzin

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


Jak chcesz skracać, to dlaczego formularz powielasz zamiast wrzucić do osobnej metody?

Go to the top of the page
+Quote Post
topcio
post
Post #9





Grupa: Zarejestrowani
Postów: 140
Pomógł: 0
Dołączył: 14.01.2017

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


A wiesz, nie zastanowiłem się nad tym, ale jest to dobry pomysł.

Zrobiłem tak
  1. case 'log_in':
  2. if($users->is_logged()) {
  3. $data = $users->get_data();
  4. if($data['user_range'] == 1) {
  5. header("Location: index.php?action=user_login");
  6. }
  7. if ($data['user_range'] == 2) {
  8. header("Location: index.php?action=admin_login");
  9. }
  10. if ($data['user_range'] == 3) {
  11. header("Location: index.php?action=devel_login");
  12. }
  13. }
  14. else if(!empty($_POST)) {
  15. $result = $users->login($_POST);
  16. if(is_numeric($result)) {
  17. $_SESSION['user_id'] = $result;
  18. $users->loged_in($_SESSION['user_id']);
  19. header("Location: index.php?action=log_in");
  20. } else {
  21. $users->login_form($result);
  22. }
  23. }
  24. else if(empty($_POST)) {
  25. $result = null;
  26. $users->login_form($result);
  27. }
  28. break;
  29.  
  30.  
  31. public function login_form($result) {
  32. echo "<form action=\"index.php?action=log_in\" method=\"post\">";
  33. echo "<input type=\"text\" placeholder=\"Login\" name=\"user_login\">";
  34. echo "<input type=\"password\" placeholder=\"Hasło\" name=\"user_pass\">";
  35. echo "<input type=\"submit\" value=\"Zaloguj\">";
  36. echo "</form>";
  37. if(isset($result)) {
  38. echo implode('<br>',$result).'</p>';
  39. }
  40. }
  41.  
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: 15.09.2025 - 05:20