I am following this tutorial to create a login and register using OO PHP.
https://www.youtube.com/watch?v=AtivJV-kx5c&list=PLfdtiltiRHWF5Rhuk7k4UAU1_yLAZzhWc#t=364
When I enter in the correct username and password it is just displaying a blank screen. So far I have been able to follow the videos with only minor issues which I have been able to fix.
This is the code I have for the login page, can anyone see something obviously wrong with 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() as $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 just trying to get it to display the word 'success' at the moment when I use the wrong log in details. As it is at the moment it just displays a blank page if i put in the wrong log in details, but if i leave it black it will give me the error I had wrote.
---- EDIT ----
some more code that are on different files to help you make a bit more sense of what 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 for post data
return $_POST[$item]; //if available return data
} else if(isset($_GET[$item])) {
return $_GET[$item]; //if not return get 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 was a problem when creating the account.');
}
}
public function find($user = null) { //find a user by their 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 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 a minimum of {$rule_value} characters.");
}
break;
case 'max':
if(strlen($value) > $rule_value) {
$this->addError("{$item} must not be more than {$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} already exists. Please enter a 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;
}
}