php - Stuck trying to create a login using object-orientation -
i following tutorial create login , register using oo php.
https://www.youtube.com/watch?v=ativjv-kx5c&list=plfdtiltirhwf5rhuk7k4uau1_ylazzhwc#t=364
when enter in correct username , password displaying blank screen. far have been able follow videos minor issues have been able fix.
this code have login page, can see wrong it?
<?php require_once 'core/init.php'; if(input::exists()) { $validate = new validate(); $validation = $validate->check($_post, array( 'username'=> array('required' => true), 'password'=> array('required' => true) )); if($validation->passed()) { $user = new user(); $login = $user->login(input::get('username'), input::get('password')); if($login) { echo 'success'; } else { echo 'loggin failed'; } } else { foreach($validation->errors() $error) { echo $error . '<br>'; } } } ?> <form action="login.php" 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"> </div> <input type="submit" value="log in"> </form>
i'm trying display word 'success' @ moment when use wrong log in details. @ moment displays blank page if put in wrong log in details, if leave black give me error had wrote.
---- edit ----
some more code on different files make bit more sense of i'm doing...
<?php class input { public static function exists($type = 'post') { switch($type) { case 'post': return (!empty($_post)) ? true : false; break; case 'get': return (!empty($_get)) ? true : false; break; defult: return false; break; } } public static function get($item) { if(isset($_post[$item])) { //check post data return $_post[$item]; //if available return data } else if(isset($_get[$item])) { return $_get[$item]; //if not return data } return ''; } } ?>
and...
<?php class user { private $_db, $_data; public function __construct($user = null) { $this->_db = db::getinstance(); } public function create($fields = array()) { if(!$this->_db->insert('users', $fields)) { throw new exception('there problem when creating account.'); } } public function find($user = null) { //find user id if($user) { $field = (is_numeric($user)) ? 'id' : 'username'; $data = $this->_db->get('users', array($field, '=', $user)); if($data->count()) { $this->_data = $data->first(); return true; } } return false; } public function login($username = null, $password = null) { $user = $this->find($username); print_r($user); return false; } } ?>
and...
<?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 $item => $rules) { foreach($rules $rule => $rule_value) { $value = trim($source[$item]); $item = escape($item); if($rule === 'required' && empty($value)) { $this->adderror("{$item} required"); } else if(!empty($value)) { switch($rule) { case 'min': if(strlen($value) < $rule_value) { $this->adderror("{$item} must minimum of {$rule_value} characters."); } break; case 'max': if(strlen($value) > $rule_value) { $this->adderror("{$item} must not more {$rule_value} characters."); } break; case 'matches': if($value != $source[$rule_value]) { $this->adderror("{$item} must match {$rule_value}."); } break; case 'unique': $check = $this->_db->get($rule_value, array($item, '=', $value)); if($check->count()) { $this->adderror("{$item} exists. please enter different username."); } break; } } } } 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; } }
Comments
Post a Comment