Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [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
 
Start new topic
Odpowiedzi
topcio
post
Post #2





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

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: 10.10.2025 - 08:19