登录操作代码无法正常运行

时间:2018-11-25 18:15:06

标签: php

我正在使用PHP OOP开发登录系统。所以我将其编码为表单:

<form class="BKoodakBold" action="" method="POST">
    <input type="text" class="fadeIn second" name="username" id="username" autocomplete="off" placeholder="username">
    <input type="text" class="fadeIn third" name="password" id="password" autocomplete="off" placeholder="password"></br></br>
    <input type="checkbox" name="remember" id="remember"> Remember </br></br>
    <input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
    <input type="submit" value="Login">
</form>

此代码为操作:

if(Input::exists())
{
    if(Token::check(Input::get('token')))
    {
        $validation = new Validate();
        $validation = $validation->check($_POST, array(
            'username' => array('required' => true),
            'password' => array('required' => true)
        ));
        if($validation->passed())
        {
            $user = new User();

            $remember = (Input::get('remember') === 'on') ? true : false; 
            $login = $user->login(Input::get('username'), Input::get('password'), $remember);

            if($login)
            {
                Redirect::to('user/profile.php?user='.Input::get("username").'');
            }
            else
            {
                echo '<p>Sorry! Logging in failed</p>';
            }
        }
        else
        {
            foreach($validation->errors() as $error)
            {
                echo $error, '</br>';
            }
        }
    }
}

您可以看到我说过,如果基于 Validate 类发生任何错误,则必须在同一页面上看到它们:

foreach($validation->errors() as $error)
        {
            echo $error, '</br>';
        }

这是我称为的 Validate 类:

    <?php 
class Validate
{
    private $_passed = false,
            $_errors = array(),
            $_db = null;

    public function __construct()
    {
        $this->_db = DB::getInstance();
    }
    public function check($source,$items = array())
    {
        foreach($items as $item => $rules)
        {
            foreach($rules as $rule => $rule_value)
            {
                $value = trim($source[$item]);
                $item = escape($item);
                if($rule === 'required' && empty($value))
                {
                    $this->addError("{$item} is required");
                }
                else if(!empty($value))
                {
                    switch($rule)
                    {
                        case 'min':
                            if(strlen($value) < $rule_value)
                            {
                                $this->addError("{$item} must be minimum of {$rule_value} characters.");
                            }
                        break;
                        case 'max':
                            if(strlen($value) > $rule_value)
                            {
                                $this->addError("{$item} must be maximum of {$rule_value} characters.");
                            }
                        break;
                        case 'matches':
                            if($value != $source[$rule_value])
                            {
                                $this->addError("{$rule_value} must match {$item}");
                            }
                        break;
                        case 'unique':
                            $check = $this->_db->get($rule_value, array($item,'=',$value));
                            if($check->count())
                            {
                                $this->addError("{$item} already exists.");
                            }
                        break;
                    }
                }
                else
                {

                }
            }
        }
        if(empty($this->_errors))
        {
            $this->_passed = true;
        }
        return $this;
    }
    private function addError($error)
    {
        $this->_errors[] = $error;
    }
    public function errors()
    {
        return $this->_errors;
    }
    public function passed()
    {
        return $this->_passed;
    }
}
?>

现在的问题是,未显示错误。例如,如果我将表单字段保留为空并按Submit按钮,则不会出现错误,它只会刷新页面!

0 个答案:

没有答案