0
votes

register.php

    <?php
    require_once 'core/init.php';

    if(Input::exists())
    {
        $validate = new Validate();
        $validation = $validate->check($_POST, array(
            'username' => array(
                'required' => true,
                'min' => 2,
                'max' => 20,
                'unique' => 'users'
            ),
            'password' => array(
                'required' => true,
                'min' => 6,

            ),
            'password_again' => array(
                'required' => true,
                'matches' => 'password'
            ),
            'name' => array(
                'required' => true,
                'min' => 2,
                'max' => 50
            ),
        ));

        if($validation->passed())
        {
            echo "Passed";
        }
        else
        {
            foreach ($validation->errors() as $error)
            {
                echo $error, '<br>';
            }
        }
    }
    ?>

    <form action="" method="post">
        <div class="field">
            <label for="username">Username</label>
            <input type="text" name="username" id="username" value="<?php echo escape(Input::get('username')) ?>" autocomplete="off">
        </div>
        <div class="field">
            <label for="password">Choose a password</label>
            <input type="password" name="password" id="password" value="<?php echo escape(Input::get('password')) ?>" autocomplete="off">
        </div>
        <div class="field">
            <label for="password_again">Enter your password again</label>
            <input type="password" name="password_again" id="password_again" value="<?php echo escape(Input::get('password_again')) ?>" autocomplete="off">
        </div>
        <div class="field">
            <label for="name">Your name</label>
            <input type="text" name="name" id="name" value="<?php echo escape(Input::get('name')) ?>" autocomplete="off">
        </div>
        <input type="submit" name="register">
    </form>

db.php

    <?php
    class db
    {
        private static $_instance = null;
        private $_pdo,
                $_query,
                $_error = false,
                $_results,
                $_count = 0;

        private function __construct()
        {
            try
            {
                $this->_pdo = new PDO('mysql:host='.config::get('mysql/host').';dbname='.config::get('mysql/db'), config::get('mysql/username'), config::get('mysql/password'));
            }
            catch(PDOException $e)
            {
                die($e->getMessage());
            }
        }

        public static function getInstance()
        {
            if(!isset(self::$_instance))
            {
                self::$_instance = new db();
            }
            return self::$_instance;
        }

        public function query($sql, $params = array())
        {
            $this->_error = false;
            if($this->_query = $this->_pdo->prepare($sql))
            {
                $x = 1;
                if(count($params))
                {
                    foreach($params as $param)
                    {
                        $this->_query->bindValue($x, $param);
                        $x++;
                    }
                }

                if($this->_query->execute())
                {
                    $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                    $this->_count = $this->_query->rowCount();
                }
                else
                {
                    $this->_error = true;
                }
            }
            return $this;
        }

        public function action($action, $table, $where = array())
        {
            if(count($where) === 3)
            {
                $operators = array('=', '>', '<', '>=', '<=');
                $field = '$where[0]';
                $operator = '$where[1]';
                $value = '$where[2]';

                if(in_array($operator, $operators))
                {
                    $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";

                    if(!$this->query($sql, array($value))->error())
                    {
                        return $this;
                    }
                }
            }
            return false;
        }

        public function get($table, $where)
        {
            return $this->action('SELECT * ', $table, $where);
        }

        public function delete($table, $where)
        {
            return $this->action('DELETE', $table, $where);
        }

        public function insert($table, $fields = array())
        {
            $keys = array_keys($fields);
            $values = '';
            $x = 1;

            foreach($fields as $field)
            {
                $values .= '?';
                if($x < count($fields))
                {
                    $values .= ', ';
                }
                $x++;
            }

            $sql = "INSERT INTO users (`".implode('`, `', $keys)."`) VALUES ({$values})";

            if(!$this->query($sql, $fields)->error())
            {
                return true;
            }
            return false;
        }

        public function update($table, $id, $fields)
        {
            $set = '';
            $x = 1;

            foreach($fields as $name => $value)
            {
                $set .= "{$name} = ?";
                if($x < count($fields))
                {
                    $set .= ', ';
                }
                $x++;
            }

            $sql = "UPDATE {$table} SET {$set} WHERE id = {$id}";

            if(!$this->query($sql, $fields)->error())
            {
                return true;
            }
            return false;
        }

        public function results()
        {
            return $this->_results;
        }

        public function error()
        {
            return $this->_error;
        }

        public function count()
        {
            return $this->_count;
        }
    }

validate.php

    <?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 be a 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($this->count())
                                {
                                    $this->addError("{$item} already exists");
                                }
                            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;
        }
    }

init.php

    <?php
    session_start();

    $GLOBALS['config'] = array
    (
        'mysql' => array
        (
            'host' => '127.0.0.1',
            'username' => 'root',
            'password' => '',
            'db' => 'lr'
        ),
        'remember' => array
        (
            'cookie_name' => 'hash',
            'cookie_expiry' => 604800
        ),
        'session' => array
        (
             'session_name' => 'user'
        )
    );

    spl_autoload_register(function($class) 
    {
        require_once 'classes/'.$class.'.php';
    });

register.php contains a registration form. i have not yet wrote the code to store it in the database, i just did the validation of the input. register.php return the error saying undefined method count(). the exact error is

Fatal error: Uncaught Error: Call to undefined method Validate::count() in C:\xampp\htdocs\LoginRegistrationSystem\classes\validate.php:50 Stack trace: #0 C:\xampp\htdocs\LoginRegistrationSystem\register.php(8): Validate->check(Array, Array) #1 {main} thrown in C:\xampp\htdocs\LoginRegistrationSystem\classes\validate.php on line 50

please help, i went through the code 3-4 times, but unable to find the error. thanks!

1
Yes, that's correct, a function called count() appears not to be defined in that class, what more can we say? your problem is wtih $this->count()... but $this->_db->count() exists though - Scuzzy

1 Answers

0
votes

Imho you don't have implemented function count() in Validate class (you are calling it on line 50 of Validate.php with if($this->count())).

Implement that function or change calling it the function should be from different class. ($this->_db->count() most probably)