0

我有三个文件:login.php、User.php(用户类)和messages.php(消息数组)。问题是:如何在用户类中使用消息?我尝试了几种方法,但都没有奏效...我需要回显错误消息,例如“帐户被阻止”等...

用户.php

<?php

include_once 'connection.php';
include_once 'messages.php';

class User {

    private $db;
    private $messages;

    public function __construct() {
        $this->db = new Connection();
    }

    public function setMessages($messages) {
        $this->messages = $messages;
    }
    /*
    public function getMessage() {
        return $this->$messages;
    }
    */

    public function authenticateUser($username, $password) {
        if(empty($username) OR empty($password)) {
            echo $this->messages[fieldsMustBeFilled]; // <- doesn't echo     anything
        } else {
            $username = $_POST['username'];
            $password = MD5($_POST['password']);

            $stmt = $this->db->prepare('SELECT * FROM users WHERE username=? AND     password=?');
            $stmt->bindParam(1, $username, PDO::PARAM_STR);
            $stmt->bindParam(2, $password, PDO::PARAM_STR, 32);
            $stmt->execute();

            if($stmt->rowCount() == 1) {
                $row = $stmt->fetch(PDO::FETCH_OBJ);
                if($row->blocked == 0) {
                    $userId = $row->userId;             
                    if($row->admin == 1) {
                        session_start();
                        $_SESSION['userId'] = $userId;
                        $_SESSION['admin'] = $row->admin;
                        $_SESSION['username'] = $username;
                        header('Location: index.php');

                    } else {
                        session_start();
                        $_SESSION['userId'] = $userId;
                        $_SESSION['admin'] = $row->admin;
                        $_SESSION['username'] = $username;
                        header('Location: index.php');
                    }
                } else {
                    echo $this->messages[accountBlocked]; // <- doesn't      echo anything
                }
            } else {
                echo $this->messages[wrongUsernameOrPassword]; // <- doesn't     echo anything
            }
        }
    }

    public function logout() {
        session_start();
        session_destroy();
        header('location: login.php');
    }

    public function onlineList() {
        echo 'I use this in future';
    }
}

?>

消息.php

<?php

$messages = array(
    'fieldsMustBeFilled' => 'Mõlemad väljad peavad olema täidetud!',
    'wrongUsernameOrPassword' => 'Vale kasutajanimi või parool!',
    'accountBlocked' => 'Sinu kontole on kohaldatud blokeering!',
);

?>

顺便说一句,连接是:

<?php

class Connection extends PDO {

    public function __construct() {
        parent::__construct ("mysql:host=localhost; dbname=vestleja", "root", "");
    }
}

?>
4

0 回答 0