使用OO-PHP进行简单登录

Cod*_*alk 1 php syntax-error

我正在尝试验证,提交表格username并且password不是空的.

表格:

<form action="usercheck.php" method="post">
    User: <input type="text" name="username" maxlength="10" />
    Pass: <input type="password" name="password" maxlength="10" />      
    <input type="submit" value="Submit" />
</form>
Run Code Online (Sandbox Code Playgroud)

usercheck.php

<?php

class Vuln{

    public $username = $_POST['username'];
    public $password = $_POST['password'];

    public function ShowErrors(){
        if($this->username == '' || $this->password == ''){
            return 'username or password field blank';  
        }
        else{
            echo stripslashes('we\'re good');
        }   
    }

    $entered = new Vuln;
    echo $entered->ShowErrors();

}


?>
Run Code Online (Sandbox Code Playgroud)

当我测试时,它说:

解析错误:语法错误,意外T_VARIABLE,期待T_FUNCTION在线:

    $entered = new Vuln;
Run Code Online (Sandbox Code Playgroud)

dyn*_*mic 5

你不能在类定义中包含代码

    class Vuln {
       //> Your class definition
    }

    //> Outside of the class

    $entered = new Vuln;
    echo $entered->ShowErrors();
Run Code Online (Sandbox Code Playgroud)

我强烈建议您阅读PHP Doc的所有基础知识