在提交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>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();
}
?>当我测试的时候,它说:
分析错误:语法错误,意外
T_VARIABLE,在线期待T_FUNCTION: $entered =新外阴;
发布于 2012-10-30 18:31:41
在这样的类定义中不能有代码
class Vuln {
//> Your class definition
}
//> Outside of the class
$entered = new Vuln;
echo $entered->ShowErrors();我强烈建议您阅读PHP Doc的所有基础知识。
发布于 2012-10-30 18:32:16
代码的一部分直接放在类中:
$entered = new Vuln;
echo $entered->ShowErrors();这些应该放在类定义之外。如下文所述,改变:
public $username = $_POST['username'];
public $password = $_POST['password'];至
public $username;
public $password;并在构造函数中或类之外启动变量。
发布于 2012-10-30 18:33:35
就像这样
$entered = new Vuln;
$entered->username = $_POST['username'];
$entered->password = $_POST['password'];
$entered->ShowErrors();您目前正在从类中实例化类。
编辑:添加以获得更多澄清--这将实例化类,并且应该在类之外。删除类中的实例化。
另一个编辑更改了变量名以匹配示例。
https://stackoverflow.com/questions/13145457
复制相似问题