1
私は、MySQLデータベースのユーザーのIDを検索しようとするユーザークラスを構築しました。見つかった場合は、変数SQL_IDをこの値に設定し、それ以外の場合は空にします。より洗練されたユーザーオブジェクトを設計する
後で呼び出される別のメソッド(IsValid)は、ユーザーが実際に存在するかどうかを示すブール値を返します。
他の誰も私のデザインにコメントしたいと思っていて、もっと洗練されたソリューションを提供したいと思っていました。私はPHPが私の主要言語ではないことを認めています。静的でない型の言語で時間を過ごした後、少しOCDishを感じるかもしれません。おそらく、私はこのデザインが正気であることの検証を求めています。
// User -> class for passing around user information. Should only pass around the UserID (a unqiue SQL ID), for security reasons, in a Session object.
class User {
private $SQL_ID = "";
//@todo: Get the User object to actually talk to the other classes. Lol.
public function __construct($Username, $Password) {
// Probably want to Base64 encode the values going into and out of the MySQL database, to prevent a SQL Injection attack.
$query = "SELECT [UserID] FROM [Users] WHERE [Username] = '" . base64_encode($Username) . "' AND [Password] = '" . base64_encode($Password) . "';";
$data = SQL::DataQuery($query);
$this->SQL_ID = $data["UserID"];
}
// Boolean function to tell us if we have a valid user. Might be able to merge this into the constructor.
public function IsValid() {
if($this->SQL_ID == "") {
return false;
}
return true;
}
public function GetUserID() {
return $this->SQL_ID;
}
// private $Query = "SELECT [UserID] FROM [Users] WHERE [Username] = '' AND PASSWORD = '';"; // Prototype User query (for selecting a UserID).
}
ええと、私はクラスの構築にログイン機能を持たないでしょう。それは私には奇妙に思える。 –