dongzhuang5741 2015-03-23 10:43
浏览 30
已采纳

PHP注册错误[重复]

I'm getting a really weird error from a file that I haven't changed at all during both these file creations. It was working fine and only started happening when I added the match for the passwords - then I deleted it to see if the error was resolved and the error was still there.

Also it appears in some of my text boxes with BR tags in between the words. however the script still does what its supposed to if I delete the errors from the text boxes and add some test data.

The guy in the tutorial I'm watching does not get these errors.

Notice: Undefined index: username in /Applications/XAMPP/xamppfiles/htdocs/xampp/ooplr/classes/Input.php on line 23
Notice: Undefined index: name in /Applications/XAMPP/xamppfiles/htdocs/xampp/ooplr/classes/Input.php on line 23

register.php

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

    if(Input::exists()) {
        $validate = new Validate();
        $validation = $validate->check($_POST, array(
            'username' => array(
                'required' => true,
                'min' => 6,
                '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 {
            print_r($validation->errors());
        }
    }
    ?>
    <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">

        <div class="field">
            <label for="password_again">Confirm password</label>
            <input type="password" name="password_again" id="password_again">

        <div class="field">
            <label for= "name">Name</label>
            <input type= "text" name= "name" value="<?php echo escape(Input::get('name')); ?>" id ="name">
        </div>
        <input type="submit" value="Register">
    </form>

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]);

                    if($rule === 'required' && empty($value)) {
                        $this->addError("{$item} is required");
                    } else if(!empty($value)) {
                        switch($rule) {
                            case 'max':
                                if (strlen($value) > $rule_value) {
                                    $this->addError("{$item} must be a maximum of {$rule_value} charcters");
                                }
                            break;
                            case 'min':
                                if (strlen($value) < $rule_value) {
                                    $this->addError("{$item} must be a minimum of {$rule_value} characters");
                                }
                            break;
                            case 'matches':
                                if($value != $source[$rule_value]) {
                                    $this->addError("{$rule_value} must match {$item}");
                                }
                            break;
                            case 'unique':

                            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;
        }
    }

Input.php

<?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;

                default:
                    return false;
                break;
        }

    }

    public static function get($item) {
        if(isset($_POST[$item])) {
            return $_POST[$item];
        } elseif(isset($_GET)) {
            return $_GET[$item];
        }
        return '';

    }
}
</div>
  • 写回答

2条回答 默认 最新

  • dongpo2340 2015-03-23 10:47
    关注

    You get function in input class fails to check if the value is set correctly:

    public static function get($item)
    {
         if(isset($_POST[$item])) {
             return $_POST[$item];
         }elseif(isset($_GET[$item])) { //<-- here, was isset($_GET)
             return $_GET[$item];
         }
         return '';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题