1

我收到错误说我的电子邮件变量是空的,除了我的正则表达式有问题,这似乎是字符类中的范围乱序,我想这是一个愚蠢的错误,因为我有很多东西要学

<?php

    include_once('../resources/config.php');
    class register
    {
        private $username;
        private $password;
        private $passmd5;
        private $email;

        private $errors;
        private $token;

        private $db;

        public function __construct()
        {
            $this->db = new config();
            $this->db = $this->db->dbConnect();

            $this->errors = array();

            $this->username = $this->regex($_POST['user']);
            $this->password = $this->regex($_POST['pass']);
            $this->email = $this->regex($_POST['email']);
            $this->token = $this->regex($_POST['token']);

            $this->passmd5 = md5($this->password);  
        }

        public function process()
        {
            if($this->valid_token() && $this->valid_data())
                $this->register();
        }

        public function regex($var)
        {
            $reggie = preg_replace('/[^a-zA-ZO-90.]/','',$var);
            return $reggie;
        }

        public function register()
        {
            $this->db->prepare("insert into users(name, pass) values ('($this->username)','($this->passmd5)')");
            if(rowCount() < 1)
                $this->errors() == "form has failed you";   
        }

        public function show_errors()
        {
            foreach ($this->errors as $key => $value) {
                echo $value."</br>";
            }
        }

        public function valid_data()
        {
            if(empty($this->username))
                $this->errors() == 'invalid username';
            if(empty($this->password))
                $this->errors() == 'invalid password';
            if(empty($this->email) || !ereg('/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/', $this->email))
                $this->errors() == 'invalid email';

            return count ($this->errors)? 0:1;

        }

        public function valid_token()
        {

        }



        }

    ?>


    $obj_reg = new register();

    if($obj_reg->process())
    {
        echo 'succes';
    }else
    {
        $obj_reg->show_errors();
    }
}


    $token = $_SESSION['token'] = md5(uniqid(mt_rand(), true));
?>

<form method="post" action="index.php?page=register.php" >
    <table>
    <tr><td>username:</td><td> <input type="text" name="user"/></td></tr>
    <tr><td>password:</td><td> <input type="password" name="pass"/></td></tr>
    <tr><td>email:</td><td> <input type="text" name="email"/></td></tr>
</table>
    <input type="hidden" name="token" value="<?=$token;?>"/>
    <input type="submit" name="register" value="register"/>
</form> 
4

3 回答 3

1

对于电子邮件正则表达式,您应该替换:

if(empty($this->email) || !ereg('/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/', $this->email))
                $this->errors() == 'invalid email';

和:

if (!filter_var($this->email, FILTER_VALIDATE_EMAIL))
    $this->errors() == 'invalid email';
于 2012-12-09T16:52:08.630 回答
0
<?php
require_once('../resources/config.php');
class register
{
    private $username;
    private $password;
    private $passmd5;

    private $errors;



    public function process()
    {
        if($this->valid_data())
        {
            $this->send();

        }
        else
        {
            echo "mama";
        }


    }

    public function regex($var)
    {
        $reggie = preg_replace('/[^A-Za-z0-9:.\/_-]/','',$var);
        return $reggie;
    }



    public function send()
    {
        mysql_query("INSERT INTO 'users' WHERE name='.$this->username.' pass='.$this->password.'");
    }

    public function checking($name, $pass)
    {
        $this->errors = array();

        $this->username = $this->regex($name);
        $this->password = $this->regex($pass);
        $this->passmd5 = md5($this->password);  
    }

    public function show_errors()
    {
        foreach ($this->errors as $key => $value) {
            echo $value."</br>";
        }
    }

    public function valid_data()
    {
        if(empty($this->username))
            $this->errors[] = 'invalid username';
        if(empty($this->password))
            $this->errors[] = 'invalid password';

        return count ($this->errors);

    }







}



?>
于 2012-12-13T08:33:25.490 回答
0

利用

$this->errors[] = "invalid foo";

代替

$this->errors() == "invalid foo";
于 2012-12-09T17:01:25.490 回答