Napisałem sobie klasę, która sprawdza poprawność danych między formularzem a bazą. Jak jest poprawnie to wyświetla jakieś dane z bazy.
Taki wstęp mniej więcej do systemu logowania. Ale mam wrażenie, że jak patrzę na całość kodu to wydaje mi się że jest bałagan przez np. while i if
, czy zapytanie SELECT.
Macie jakiś pomysł jak poprawić jakość tego kodu w sensie żeby było go mniej, a taki sam efekt.
Czy include() pomoże tutaj trochę, czy trzymanie np. konfiguracji bazy w innym pliku i include() tego pliku z bazą do klasy?

  1. <?php
  2.  
  3.  
  4. class auth
  5. {
  6.    public $name;
  7.    public $content;
  8.    public $message;
  9.    
  10.    public function get()
  11.    {
  12.        $pdo= new PDO('mysql:host=localhost;dbname=trial','root','');
  13.        $stmt= $pdo-> prepare ('SELECT name,content FROM experiment WHERE name=:name');
  14.        $stmt-> bindValue (':name', $_POST['subject'], PDO::PARAM_STR);
  15.        $stmt-> execute();
  16.        
  17.        while ($row= $stmt-> fetch())
  18.        {
  19.            $this-> name= $row['name'];
  20.            $this-> content= $row['content'];
  21.        }
  22.        $stmt-> closeCursor();
  23.    }
  24.    
  25.    public function login($login, $content)
  26.    {
  27.        if ($this-> name== $login && $this-> content== $content)
  28.        {
  29.            $_SESSION['user']= $login;
  30.            $this-> message= 'Welcome: ' . $_SESSION['user'];
  31.        }
  32.        else
  33.        {
  34.            $this-> message= 'Invalid information';
  35.        }
  36.      }
  37.      
  38.    public function show()
  39.    {
  40.        return $this-> message;
  41.    }
  42. }
  43.  
  44. $user= new auth;
  45. $user-> get();
  46. $user-> login ($_POST['subject'], $_POST['content']);
  47. echo $user-> show();
  48.  
  49. ?>


  1. <!DOCTYPE html
  2. PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
  5. <title>Title</title>
  6. </head>
  7.  
  8. <form action="execute.php" method="post">
  9. <div id="system">
  10. <input type="text" name="subject" />
  11. <input type="text" name="content" />
  12. <input type="submit" value="Send" />
  13. </div>
  14. </form>
  15.  
  16. </body>
  17. </html>