Browse Source

ユーザのパスワード認証のための DB 追加

main
miteruzo 1 year ago
parent
commit
69194b550a
4 changed files with 65 additions and 10 deletions
  1. +7
    -5
      chat.php
  2. +1
    -0
      migrations/2023_07_01_080700_add_pass_to_chats_table.sql
  3. +7
    -5
      modules/chat.mod.php
  4. +50
    -0
      modules/user.mod.php

+ 7
- 5
chat.php View File

@@ -1,14 +1,16 @@
<?php

var_dump ($_POST);

require_once './modules/chat.mod.php';


$chat = new Chat;

$user_id = $_GET['id'];
$pass = $_GET['pass'];
$chat_message = $_GET['text'];

$chat -> send ($user_id, $chat_message);


$chat = new Chat;
$chat -> user_id = $user_id;
$chat -> text = $chat_message;
$chat -> insert ();


+ 1
- 0
migrations/2023_07_01_080700_add_pass_to_chats_table.sql View File

@@ -0,0 +1 @@
ALTER TABLE `users` ADD `pass` VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'パスワード' AFTER `name`;

+ 7
- 5
modules/chat.mod.php View File

@@ -5,6 +5,10 @@ Chat
{
private $db;

private $id;
var $user_id;
var $pass;

function
__construct ()
{
@@ -20,9 +24,7 @@ Chat
}

function
send (
int $user_id,
string $text):
insert ():
void
{
$sql = "
@@ -32,8 +34,8 @@ Chat
text)
VALUES
(
$user_id,
'$text')";
{$this -> user_id},
'{$this -> text}')";
$this -> db -> query ($sql) or die ("db_insert error $sql");
}
}


+ 50
- 0
modules/user.mod.php View File

@@ -0,0 +1,50 @@
<?php

class
User
{
private $db;

private $id;
var $name;
var $pass;

function
__construct ()
{
$config = include ('./config.php');

$db_host = $config['host'];
$db_port = $config['port'];
$db_user = $config['user'];
$db_pass = $config['pass'];

$this -> db = new mysqli ($db_host, $db_user, $db_pass, 'goatoh_training',
$db_port);
}

public static function
find (
int $id):
Self
{
$sql = "
SELECT
*
FROM
user
WHERE
id = $id";
$result = $this -> db -> query ($sql) or die ("db_select error $sql");

$row = $this -> db -> fetch_array ($result);

$self = new Self;
$self -> id = $id;
$self -> name = $row['name'];
$self -> pass = $row['pass'];

return $self;
}
}


Loading…
Cancel
Save