[PHP-NOTES] note 130324 added to pdo.construct

[email protected] ("[email protected]")
Newsgroups php.notes
Message-ID <[email protected]>
Is a nice idea? For example in my work is this the code to implement us:
class ModelLoginNC {
    private PDO $pdo;

    public function __construct() {
        $dsn = "mysql:host=XXX;dbname=XXX;charset=utf8mb4";
        $user= "admin";
        $pass= "admin";
        $options= [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES => false,
            PDO::ATTR_CASE => PDO::CASE_LOWER
        ];
        $this->pdo = new PDO($dsn, $user, $pass, $options);
    }

    public function auth(string $user, string $password): bool {

        $sql = "SELECT password FROM cliente WHERE user= :user";
        $stmt = $this->pdo->prepare($sql);
        $stmt->bindParam(':user', $user);
        $stmt->execute();
        $row = $stmt->fetch();

        if ($fila && isset($fila['password'])) {
            if (password_verify($password, $row['password'])) {
                return true;
            }
        }
        return false;
    }
}

and controller is this:
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['user'], $_POST['password'])) {
    $modelLogin = new \model\ModelLoginNC();
    $user= $_POST['user'];
    $password = $_POST['password'];
    if ($modelLogin->auth($user, $password)) {
        // Auth correct
        // we can create the token for the cookie
        // $token = \util\seguridad\JWT::takeToken(['user' => $user]);
        // ...
        echo "Wellcome";
    } else {
        echo "user or password failed!!";
    }
}
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 87.223.130.93)
----
Manual Page -- https://php.net/manual/en/pdo.construct.php
Edit        -- https://main.php.net/note/edit/130324
Del: integrated  -- https://main.php.net/note/delete/130324/integrated
Del: useless     -- https://main.php.net/note/delete/130324/useless
Del: bad code    -- https://main.php.net/note/delete/130324/bad+code
Del: spam        -- https://main.php.net/note/delete/130324/spam
Del: non-english -- https://main.php.net/note/delete/130324/non-english
Del: in docs     -- https://main.php.net/note/delete/130324/in+docs
Del: other reasons-- https://main.php.net/note/delete/130324
Reject      -- https://main.php.net/note/reject/130324
Search      -- https://main.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.