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?
<?php class auth { public $name; public $content; public $message; public function get() { $pdo= new PDO('mysql:host=localhost;dbname=trial','root',''); $stmt= $pdo-> prepare ('SELECT name,content FROM experiment WHERE name=:name'); $stmt-> bindValue (':name', $_POST['subject'], PDO::PARAM_STR); $stmt-> execute(); while ($row= $stmt-> fetch()) { $this-> name= $row['name']; $this-> content= $row['content']; } $stmt-> closeCursor(); } public function login($login, $content) { if ($this-> name== $login && $this-> content== $content) { $_SESSION['user']= $login; $this-> message= 'Welcome: ' . $_SESSION['user']; } else { $this-> message= 'Invalid information'; } } public function show() { return $this-> message; } } $user= new auth; $user-> get(); $user-> login ($_POST['subject'], $_POST['content']); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us"> <head> </head> <body> <form action="execute.php" method="post"> <div id="system"> <input type="text" name="subject" /> <input type="text" name="content" /> <input type="submit" value="Send" /> </div> </form> </body> </html>