1

usernamepasswordが空でないフォームを送信する際に、検証しようとしています。

フォーム:

<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 = 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 に答える