1

我只是想验证,在提交usernameandpassword不为空的表单时。

表格

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

用户检查.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();
    
}


?>

当我测试时,它说:

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

    $entered = new Vuln;
4

6 回答 6

5

您不能在这样的类定义中包含代码

    class Vuln {
       //> Your class definition
    }

    //> Outside of the class

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

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

于 2012-10-30T18:31:41.590 回答
1

您的部分代码直接放在类中:

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

那些应该放在类定义之外。如下所述,更改:

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

public $username;
public $password;

并在构造函数或类外部启动变量。

于 2012-10-30T18:32:16.140 回答
0

就像是

$entered = new Vuln;
$entered->username = $_POST['username'];
$entered->password = $_POST['password'];
$entered->ShowErrors();

您目前正在从您的班级中实例化您的班级。

编辑:添加以进行更多说明 - 这会实例化类并且应该在类之外。删除类中的实例化。

另一个编辑更改了变量名称以匹配示例

于 2012-10-30T18:33:35.083 回答
0

不应在类定义中找到执行对象的代码。

你可能是这个意思:

<?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();
于 2012-10-30T18:33:43.313 回答
0

是什么阻止你使用

<form action="usercheck()" id="form" method="post">

并使用以下JS

var theForm = document.forms["form"];
var user = theForm.elements["user"];
var pass = theForm.elements["pass"];

if(user.value==null || user.value=="")
{
alert("First name(s) must be filled out");
return false;
}

else if(pass.value==null || pass.value=="")
{
    alert("Last name must be filled out");
return false;
}

else
{
return true;
}
于 2012-10-30T18:37:21.400 回答
0

您不能从类本身中创建类的对象。您必须在提交表单时调用该类。还将类文件名更改为类似 vuln.php 的名称,并将 usercheck.php 更新为以下代码。

     if($_POST){
        include("Vuln.php");
        $entered = new Vuln;
        echo $entered->ShowErrors();
     }

希望它可以帮助你。

于 2012-10-30T18:37:58.057 回答