PHP OOP无法回显messages.php文件中的消息

时间:2013-11-14 19:26:04

标签: php arrays oop

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

user.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';
    }
}

?>

messages.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!',
);

?>

BTW连接是:

<?php

class Connection extends PDO {

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

?>

0 个答案:

没有答案