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 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了