IoT+WEBのためのホームサーバ計画の実装編11。普通のWeb部分。ログイン関連3。DB連携させる。

DBにテーブルを作成から始まり始まり~

ユーザの管理をするために、DBを作ってみます。
ということで、こんな感じで。

CREATE TABLE [T_user] (
	[id] INTEGER PRIMARY KEY AUTOINCREMENT,
	[username] VARCHAR(256) NOT NULL UNIQUE,
	[passmd5] VARCHAR(40) NOT NULL,
	[regist] INTEGER NOT NULL DEFAULT '0',
	[registdate] INTEGER NOT NULL,
	[lastdate] INTEGER NOT NULL,
	[note] VARCHAR(256),
	[etc] TEXT
);

接頭辞【T_】をつけるあたり、いかにもおっさん(o^^o)
うぅーーん。マスターっぽいんだけど、、日頃UPDATEされつづけるからやっぱT_で。^^;

PHPでDBにレコードを追加するテスト

とりあえずこんな感じで。
なんとなく簡単なクラスをつくりーーの、、

class hiraDB{
	private $dbname;
	private $db;
	//-------------------------
	/*	初期値	*/
	//-------------------------
	function __construct($dbname="/etc/*****/*****.db"){
		$this->dbname	= $dbname;
	}
	//-------------------------
	//	
	//-------------------------
	function open(){
		$this->db = new SQLite3($this->dbname);
		$this->db->exec("BEGIN DEFERRED;");
	}
	//-------------------------
	//	
	//-------------------------
	function close($b=true){
		if($b)	$this->db->exec("COMMIT;");
		$this->db->close();
	}
	//-------------------------
	function execute($sql){
		$results = @$this->db->query($sql);
		if(!$results){
			return	false;
		}
		return	$results;
	}
	//-------------------------
	function query($sql){
		$AR	= array();
		$results = @$this->db->query($sql);
		if(!$results){
			return	false;
		}
		while ($record = $results->fetchArray(SQLITE3_ASSOC)){
			$AR[] = $record;
		}
		return	$AR;
	}
	//-------------------------
}

INSERTしーーの、、

	$db = new hiraDB();
	$db->open();
	$nowdate		= time().PHP_EOL;
	$lastdate		= $nowdate;
	$sql			= "insert into T_user (username,passmd5,regist,registdate,lastdate) VALUES ('hiraide4','passMD','0',{$nowdate},{$lastdate});";
	$results = $db->execute($sql);
	$db->close();

確認するためのPHP書きーーの、、

	$db = new hiraDB();
	$db->open();
	$sql			= "select * from T_user;";
	$AR = $db->query($sql);
	$a = Var_Dump::display($AR,true);
	echo $a;
	$db->close();

確認しーーの、、、

array(4) {
  0 => array(8) {
    id => int 1
    username => string(7) username
    passmd5 => string(6) passWordMd5
    regist => int 0
    registdate => int 1583326080
    lastdate => int 1583326080
    note => NULL
    etc => NULL
  }
  1 => array(8) {
    id => int 2
    username => string(8) username2
    passmd5 => string(6) passWordMd5
    regist => int 0
    registdate => int 1583326153
    lastdate => int 1583326153
    note => NULL
    etc => NULL
  }
  2 => array(8) {
    id => int 3
    username => string(8) username3
    passmd5 => string(6) passWordMd5
    regist => int 0
    registdate => int 1583326356
    lastdate => int 1583326356
    note => NULL
    etc => NULL
  }
  3 => array(8) {
    id => int 4
    username => string(8) username4
    passmd5 => string(6) passWordMd5
    regist => int 0
    registdate => int 1583326870
    lastdate => int 1583326870
    note => NULL
    etc => NULL
  }
}

あーー、ここまでは何の問題も無く(o^^o)

LOGINでDBを参照してみる

それでは、ユーザ、パスワードでSQLで件数を見てみましょう。

	require_once	"hiraDB.php";

	$userid			= $_POST["userid"];
	$userpass		= $_POST["userpass"];
	$retV			= array();

	$db				= new hiraDB();
	$tmp			= $db->open();
	$sql			= "select count(*) as hira from T_user where username='{$userid}'and passmd5='{$userpass}';";
	$AR				= $db->query($sql);
	$n				= $AR[0]["hira"];

	if($n == 0){
		$sql			= "select count(*) as hira from T_user where username='{$userid}';";
		$BR				= $db->query($sql);
		$n2				= $BR[0]["hira"];
	}

	$db->close();
	if($n == 0){
		if($n2==0){
			$retV["status"]		= "NG";
			$retV["message"]	= "user not found.";
		}else{
			$retV["status"]		= "NG";
			$retV["message"]	= "password falt.";
		}
	}else{
		$retV["status"]		= "OK";
	}

とりあえず、登録してテストなので、登録します。
本来は、認証のメールやら何やらを経てやるんですが、便宜的にSQLで追加してみます。

	if($n == 0){
		$nowdate		= time().PHP_EOL;
		$lastdate		= $nowdate;
		$sql			= "insert into T_user (username,passmd5,regist,registdate,lastdate) VALUES ('{$userid}','{$userpass}','0',{$nowdate},{$lastdate});";
		$results		= $db->execute($sql);
	}

そして、実行。

めでたしめでたし(o^^o)

コメントする

メールアドレスが公開されることはありません。 が付いている欄は必須項目です