Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <form action="" method="post">
- <div class="field">
- <label for="username">Username: </label>
- <input type="text" name="username" id="username" autocomplete="off" />
- </div>
- <div class="field">
- <label for="Password">Password: </label>
- <input type="password" name="password" id="password" autocomplete="off" />
- </div>
- <div class="field">
- <label for="remember">
- <input type="checkbox" name="remember" id="remember" value="on"/> Remember Me
- </label>
- </div>
- <input type="hidden" name="token" value="<?php echo Token::generate(); ?>" />
- <input type="submit" value="Login" name="Login"/>
- </form>
- <?php
- require_once 'init.php';
- $user = new User();
- if($user->isLoggedIn()){
- Redirect::to('index.php');
- }
- $validate = new Validate();
- if(Input::exists()) {
- if(Token::check(Input::get('token'))) {
- $validation = $validate->check($_POST["Login"], array(
- 'username' => array('required' => true),
- 'password' => array('required' => true)
- ));
- }
- }
- ?>
- <?php
- class Validate {
- # Set the variables
- private $_passed = false,
- $_errors = array(),
- $_db = null;
- # Construct or establish connection to the database
- public function __construct(){
- $this->_db = Database::getInstance();
- }
- # The validation/checking code or the main brain of the code
- public function check($source, $items = array()){
- # Run a ` for each ` for each item in the fields
- foreach($items as $item => $rules) {
- # Run a ` for each ` for every rule in the items
- foreach($rules as $rule => $rule_value) {
- # Set the variables of `value` and `item`
- $value = $source[$item];
- $item = sanitize($item);
- if($rule === 'required' && empty($value)) {
- $this->addError("{$item} is required");
- } else if (!empty($value)) {
- switch($rule) {
- # Case: Minimum
- case 'min':
- if(strlen($value) < $rule_value) {
- $this->addError("{$item} must be a minimum of {$rule_value} characters.");
- }
- break;
- # Case Maximum
- case 'max':
- if(strlen($value) > $rule_value) {
- $this->addError("{$item} must be a maximum of {$rule_value} characters.");
- }
- break;
- # Case: Match
- case 'matches':
- if($value != $source[$rule_value]) {
- $this->addError("{$rule_value} must match {$item}.");
- }
- break;
- # Case: Unique
- case 'unique':
- $check = $this->_db->get($rule_value, array($item, '=', $value));
- if($check->count()) {
- $this->addError("{$item} already exists.");
- }
- break;
- # Case: Not match
- case 'notmatch':
- if($value === $source[$rule_value]) {
- $this->addError("{$rule_value} must not match {$item}.");
- }
- break;
- }
- }
- }
- }
- if(empty($this->_errors)) {
- $this->_passed = true;
- }
- }
- # ~ ADD ~ and error
- public function addError($error) {
- $this->_errors[] = $error;
- }
- # ~ RETURN ~ the errors
- public function errors() {
- return $this->_errors;
- }
- # ~ CHECK ~ if it is passed
- public function passed() {
- return $this->_passed;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement