2016-04-13 25 views
1

ゆっくりとPHPを学びます。私が思う本の中の一点で自分自身を見つけ出すことは、実践的なコーディングの例として有用です。私は長さと複雑さのためにユーザーのパスワードを確認したい。はい、私は長いと複雑なパスワードの余地が多いことを理解します。この問題は、私がゆっくりと学ぶのに十分です。要件は、正確に9文字と1記号です。その他の文字はすべて使用できます。パスワードが要件を満たしていないときにエラーメッセージが返されない理由を確かめてください。どんな援助も高く評価されており、これは恐らく死んだ馬であることを知っています。他の答えのほとんどは、私が望むより複雑ですが、最終的にそこに着くでしょう。建設的にコメントしてください...ありがとう!ユーザにエラーメッセージが表示されない

<?php 
     $pwd = filter_input(INPUT_GET, 'password'); 
     $errmsg = ""; 

     //function with 2 parameters/one passed by reference 
     function passVal($pwd) { 
      $errmsg = null; 
      if (!preg_match('/^(?=.*[@]){9}$/', $pwd)) { 
        $errmsg = "Password must contain exactly 9 characters and one @ sign."; 
      } 
      if (strlen($pwd == 9) && preg_match('/(?=.*[@])/', $pwd)) { 
       $errmsg = "Contains exactly 9 characters and there is at least one @ sign. Password is good"; 
      } 
     return $errmsg; 
     } 
?> 

HTML

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Untitled Document</title> 
</head> 

<body> 
<header><h1>Password Check</h1></header> 
<form action="" method="get"> 
<h3>Enter a password in the box.</h3><br> 
<p>The password must be exactly 9 characters and include at least 
one &#64 sign. All other characters are allowed.</p> 
<p>Enter a password<input type="text" name="password"></p> 
<p><button type="submit" formmethod="get" name="button">Check Password</button></p> 
<p><?php echo $errmsg; ?></p> 
</body> 
</html> 
+1

正確にあなたが "passVal" 関数を呼び出していますか? – dquinonez

+0

実際には、passVal()関数を実際に呼び出すようには見えません。 – WillardSolutions

+0

あなたはどこでも$ _GETから投稿されたパスワードを読んでいません。 – WillardSolutions

答えて

3

他にも回答があります。あなたは決してpassVal()機能と呼んだと言っています。

strlen()の条件文が失敗しています。

if (strlen($pwd == 9) && preg_match('/(?=.*[@])/', $pwd)) 

とのようになり、そして包装$pwd内部括弧:strlen()機能の手動http://php.net/manual/en/function.strlen.php当たりとして

if ((strlen($pwd) == 9) && preg_match('/(?=.*[@])/', $pwd)) 

  • strlen($str);

だからあなた(strlen($pwd == 9)はここに失敗します。だろう/空でないセットの場合、それはチェックするために条件文を使用して、しかし

echo passVal($pwd); 

return $errmsg; 
} 

後:

そのため、追加し$pwdパラメータを持つ関数をエコーすることができますより良い。

追記編集:あなたがフォームを閉鎖していないことが表示されますので、それはあなたの実際のコードであれば、あなたはそれ</form>を追加する必要があります。アイデア編集をいじる


条件付きempty()の両方をチェックし、同じ行の中で関数をエコーする場合にも、3項演算子を使用することができます。

I:<p><?php echo !empty($pwd) ? passVal($pwd) : ''; ?></p>であり、に割り当てられた2つの異なるメッセージを$msg_bad$msg_goodの両方で定義します。

ここで完全な書き直しです:

<?php 

$pwd = filter_input(INPUT_GET, 'password'); 

$msg_bad = "<b>Password must contain exactly 9 characters and one @ sign.</b>"; 
$msg_good = "Contains exactly 9 characters and there is at least one @ sign. Password is good"; 


//function with 2 parameters/one passed by reference 
function passVal($pwd) { 

global $msg_good, $msg_bad; 

    $errmsg = null; 


    if ((strlen($pwd) == 9) && preg_match('/(?=.*[@])/', $pwd)) { 

     $errmsg = $msg_good; 

    } 

    else { 

     $errmsg = $msg_bad; 

    } 

    return $errmsg; 

} 


?> 
<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Untitled Document</title> 
</head> 

<body> 
<header><h1>Password Check</h1></header> 

<form action="" method="get"> 
<h3>Enter a password in the box.</h3> 
<p>The password must be exactly 9 characters and include at least 
one &#64 sign. All other characters are allowed.</p> 
<p>Enter a password<input type="text" name="password"></p> 
<p><button type="submit" formmethod="get" name="button">Check Password</button></p> 
</form> 

<p><?php echo !empty($pwd) ? passVal($pwd) : ''; ?></p> 

</body> 
</html> 
+0

これは良い説明です。私は常にシンタックスの重要な部分を欠いているようです。残念ながら、自宅で私のデバッグ手段は限られています。私はこのコードで正規表現を適用し、回避することではなかったので、この答えを受け入れました。私のエラーを修正し、現在は動作しています。ありがとう! – allendks45

+0

@ allendks45お元気です、助けになってうれしかったです。私はあなたの作業コードに閉じた ''タグが含まれているかどうかわからない、私の答えを少し編集しました。私は底に近い私の答えにそれを加えました*歓声* –

0

以下のコードをチェックし、私はダウンの$ ERRMSG変数を移動し、それに返された値を代入する関数を呼び出します。

<?php 
$pwd = $_GET['password']; 

//function with 2 parameters/one passed by reference 
function passVal($pwd) { 
    $errmsg = null; 

    if (!preg_match('/^(?=.*[@]){9}$/', $pwd)) { 
     $errmsg = "Password must contain exactly 9 characters and one @ sign."; 
     } 

    if (strlen($pwd == 9) && preg_match('/(?=.*[@])/', $pwd)) { 
     $errmsg = "Contains exactly 9 characters and there is at least one @ sign. Password is good"; 
} 
    return $errmsg; 
} 

**$errmsg = passVal($pwd);** 

?> 
<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Untitled Document</title> 
</head> 

<body> 
<header><h1>Password Check</h1></header> 
<form action="" method="get"> 
<h3>Enter a password in the box.</h3><br> 
<p>The password must be exactly 9 characters and include at least 
one &#64 sign. All other characters are allowed.</p> 
<p>Enter a password<input type="text" name="password"></p> 
<p><button type="submit" formmethod="get" name="button">Check Password</button></p> 
<p><?php echo $errmsg; ?></p> 
</body> 
</html> 
0

は、私の知る限りでは、あなたがpassVal関数を呼び出すことはありません見ることができるように。

if(! empty($_GET)) { 
    $errmsg = passVal($_GET['password']); 
} 

フォームを送信するだけで、関数が自動的に呼び出されるわけではありません。実際には、フォームが提出されたかどうかを常に確認してから、必要な処理を行う必要があります。

理想的には、PHPロジックをHTMLから分離する必要がありますが、それはまったく別のトピックです。

0

あなたは関数を呼び出すのではなく、関数の必要はありません。これを行う:

<?php 
$pwd = filter_input(INPUT_GET, 'password'); 
$errmsg = ""; 
//function with 2 parameters/one passed by reference 

if ((count($pwd)<9) && (strpos($pwd, "@") == false)) { 
    $errmsg = "Password must contain exactly 9 characters and one @ sign."; 
    } 
else { 
    $errmsg = "Contains exactly 9 characters and there is at least one @sign. Password is good"; 
} 



?> 
<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Untitled Document</title> 
</head> 
<body> 
<header><h1>Password Check</h1></header> 
<form action="" method="get"> 
<h3>Enter a password in the box.</h3><br> 
<p>The password must be exactly 9 characters and include at least 
one &#64 sign. All other characters are allowed.</p> 
<p>Enter a password<input type="text" name="password"></p> 
<p><button type="submit" formmethod="get" name="button">Check Password</button></p> 
<p><?php echo $errmsg; ?></p> 
</body> 
</html> 
関連する問題