2016-08-14 4 views
1

phpの新機能で、さまざまなスキルや問題を解決し、修正する方法を見つけるためにWebアプリケーション開発を行っています。PHPフォームのバリデーションとXSSセキュリティ

登録フォームを作成し、フォームを検証してSQLインジェクションとXSSから保護しています。 NOTE私は準備されたステートメントを使用することができますが、私のスキルのレベルでは、Mysqli手続きから始めることが十分に自信を持って完了するまで私の開発のための最良の結果であると思う。

だから私は、あなたが削除したり、追加したり、代わりに(stmtとは別に)使用する必要があるかどうかを専門家に見てほしい。

ここに私の登録ページです。

<?php 
    // define mqsqli real escape string function 
    function _olaskee($escape) { 
     $escape = htmlspecialchars ($escape, ENT_QUOTES, 'UTF-8'); 
     $escape = trim ($escape, ENT_QUOTES, 'UTF-8'); 
     $escape = stripcslashes ($escape, ENT_QUOTES, 'UTF-8'); 
     return $escape; 

    } 
    // start session 
    session_start(); 

    // include database connection 
    //require_once('include/connection.php'); 

    // if user type already detected, redirect to index.php 
    if(isset($_SESSION['user_type'])){ 
     header('Location: index.php'); 
    } 

    // check if we have submited/if the for as being submitted 
    if(!empty($_POST['submit'])){ 

     //instantiate 
     $firstname = _olaskee($con, $_POST['firstname']); 
     $lastname = _olaskee($con, $_POST['lastname']); 
     $user_name = _olaskee($con, $_POST['user_name']); 
     $user_type = _olaskee($con, $_POST['user_type']); 
     $password = _olaskee($con, $_POST['password']); 
     $confirm_password = _olaskee($con, $_POST['confirm_password']); 

      // hash password 
     $hashed_password = password_hash($password, PASSWORD_BCRYPT, ['cost' => 12]); 

     // include database connection 
     require_once('include/errMsg.php'); 

    } 
    // include page title 
    $title = 'Registration Page'; 


    // include header layout 
    require_once('include/header.php'); 
    ?> 

    <div> 

     <form name="register" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8'); ?>" method="post"> 
     <table> 
     <tr> 
      <td>First Name</td> 
      <td><input type="text" name="firstname" value='<?php// echo htmlspecialchars ($firstname) ?>'><br><span style='color: red'><?php echo $fnErr ?></span></td> 
      <?php echo $firstname ; ?> 
     </tr> 
     <tr> 
      <td>Last Name</td> 
      <td><input type="text" name="lastname" value='<?php echo htmlspecialchars ($lastname) ?>'><br><span style='color: red'><?php echo $lnErr ?></span></td> 
     </tr> 
     <tr> 
      <td>User Name</td> 
      <td><input type="text" name="user_name" value='<?php echo htmlspecialchars ($user_name) ?>'><br><span style='color: red'><?php echo $unameErr ?></span></td> 
     </tr> 
     <tr> 

      <td>User Type</td> 
      <td> 
    <!-- <label for="flavor">Select User Type:</label > --> 

    <select id="user_type" name='user_type' > 
     <option value="">Select User Type</option> 
     <option <?php echo $user_type=='rsw'?'selected':''; ?> >rsw</option> 
     <option <?php echo $user_type=='sp'?'selected':''; ?> >sp</option> 
    </select> 
     <span style='color: red'><?php echo $u_typeErr?></span> 
      </td> 
     </tr> 

     <tr> 
      <td>Email</td> 
      <td><input type="email" name="email" value='<?php echo htmlspecialchars ($email) ?>'><br /><span style='color: red'><?php echo $emailErr ?></span></td> 
     </tr> 
     <tr> 
      <td>Password:</td> 
      <td><input type="password" name="password" id="password"><br /><span style='color: red'><?php echo $passErr ?></span></td></td> 
     </tr> 
     <tr> 
      <td>Confirm Password:</td> 
      <td><input type="password" name="confirm_password" id="confirm_password"><br /><span style='color: red'><?php echo $cpassErr ?></span></td></td> 
     </tr> 
     <tr> 
      <td></td> 
      <td><input type="submit" name="submit" value="Register"><a href='index.php'> Login</a></td> 
     </tr> 
     </table> 
     </form> 
    </div> 

    <?php 
    if(is_file('include/footer.php')) 
    include_once('include/footer.php'); 
    ?> 

そしてここでは、両方のページでいくつかの行をコメントアウトしている私のエラーメッセージページ

<?php 

    // error handler variable 
    $fnErr = $lnErr = $unameErr = $u_typeErr = $emailErr = $passErr = $cpassErr = ''; 
    $firstname = $lastname = $user_name = $user_type = $email = $password = $confirm_password = ''; 

      // if submit, then validate 
     $firstname = ($_POST['firstname']); 
      // set field validation for first name 
      if (empty($firstname)){  
      $fnErr = 'Field empty, please enter your first name';   
      }else{ 
        if (strlen($firstname) < 3){ 
          $fnErr = 'First Name is too short'; 
        } 
      } 
       // check if name only contains letters and whitespace 
         if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) { 
         $fnErr = "Only letters and white space allowed"; 
      } 


      // set field validation for last name 
     $lastname = ($_POST['lastname']); 
      if (empty($lastname)){  
      $lnErr = 'Field empty, please enter your last name';   
      }else{ 
        if (strlen($lastname) < 3){ 
          $lnErr = 'Last Name is too short'; 
        } 
      } 
       // check if name only contains letters and whitespace 
         if (!preg_match("/^[a-zA-Z ]*$/",$lastname)) { 
         $lnErr = "Only letters and white space allowed"; 
      } 

      // set field validation for user name 
      $user_name = ($_POST['user_name']); 
      if (empty($user_name)){  
      $unameErr = 'Field empty, please enter user name';   
      }else{ 
         if (strlen($user_name) < 6){ 
          $unameErr = 'Password is too short'; 
        }else{ 

         if (strlen($user_name) > 15){ 
          $unameErr = 'Password is too long';     
          } 
        } 
       } 
      // check if name only contains letters and whitespace 
        if (!preg_match("#.*^(?=.*[a-z])(?=.*[A-Z]).*$#",$user_name)) { 
        $unameErr = "At least one CAPS, letters and white space allow"; 
      } 


      // check if user select user type from list 
      $user_type = ($_POST['user_type']); 
        if (empty($user_type)){  
        $u_typeErr = 'Please select user type from list';   
        } 



     // set email filter validation 
      $email = ($_POST['email']); 
      if (empty($email)){  
       $emailErr = 'Field empty, please enter your last name';   
      }else{ 
        // check if e-mail address is well-formed 
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
         $emailErr = "Invalid email format"; 
        } 
      } 


      // set field validation for password 
      $password = ($_POST['password']); 
      if (empty($password)){  
      $passErr = 'Field empty, please create a password';   
      }else{ 
         if (strlen($password) < 6){ 
          $passErr = 'Password is too short'; 
        }else{ 

         if (strlen($password) > 15){ 
          $passErr = 'Password is too long';     
          }       
        }          
       } 
         if(!preg_match("#[A-Z]+#", $password)) { 
          $passErr = "Password must include at least one CAPS! "; 
        }else{ 

        if(!preg_match("#[0-9]+#", $password)) { 
          $passErr = "Password must include at least one NUMBER! "; 
         } 
        } 
    // //    // check if name only contains letters and whitespace 
    //    if (preg_match("#.*^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$#", $password)) { 
    //     $passErr = "Try again... Password must contain NUMBER, LETTER and CAPS"; 
    //    } 


       // set field validation for confirm password 
     $confirm_password = ($_POST['confirm_password']); 
      if (empty($confirm_password)){  
      $cpassErr = 'Field empty, please confirm your password';   
      }else{ 
        if ($password != $confirm_password) { 
         $cpassErr = 'Error... Passwords do not match'; 
        } 
      }  


    // // define mqsqli real escape string function 
    // function _olaskee($escape) { 
    // $escape = htmlspecialchars ($escape, ENT_QUOTES, 'UTF-8'); 
    // $escape = trim ($escape, ENT_QUOTES, 'UTF-8'); 
    // $escape = stripcslashes ($escape, ENT_QUOTES, 'UTF-8'); 
    // return $escape; 

    // } 

    ?> 

NOTEです。

また、登録ページには、セッションの先頭にセキュリティ機能が含まれているかどうかは不明です。

はまた、パスワードハッシュを使用していたが、私はまだデータベース上でテストしていませんが、(私は右のそれを使用している?)

だけ見ていると私にあなたの専門家の意見を教えてください

よろしくお願いいたします

答えて

0

私は専門家ではありませんが、私はあなたにいくつかのメモを与えることができます。あなたの消毒機能_olaskeeでは、私はあなたがこれらの機能は、それが

  • あなたがここにstripcslashesを必要としない使用しない方法を理解する必要があると思います。この関数はスラッシュを削除してなぜここに入れていますか?

  • パスワードをサニタイズする必要はありません。あなたはそれを使用する前にそれをハッシュし、ハッシングは注入されたコードを置き換えます。

  • SQLインジェクションに対するサニタイズの場合は、mysqli_real_escape_stringを使用する必要があります。

  • filter_varの機能をご覧ください。入力をサニタイズして検証するのに非常に役立ちます。この関数は、指定した長さに対して検証することができます、あなたは攻撃が行われた最初の方法を知っておく必要がある攻撃から身を守る方法を理解するには

に(テキストエリアのような)特定の入力にし、その一部のHTMLタグを許可します。 SQLインジェクションについて読んで、脆弱なコードでデータベースをハッキングできるかどうかを確認してください。

ZAPツールをお試しください。サイトのURLを渡すだけで自動スキャンを使用することができます。自動的にウェブアプリケーションをスキャンし、見つかった脆弱性を報告します。

ログインシステムの作成方法を学ぶのは良いことです。しかし、実際のアプリケーションでは、独自のログインシステムを作ることはお勧めしません。テストされ、承認されたソフトウェアに常に頼ってください。そうしないと、脆弱性のあるシステムを作成することになります。がんばろう!

+0

あなたの入力をお寄せいただきありがとうございます...私はmysqliの実際のエスケープ文字列を使用してフォームをサニタイズすることを考えました... – olaskee

+0

既に作成したソフトウェアを使用することの大きなファンではありません。私がソフトウェアを使用していて、自分の好みに合わせてカスタマイズしたいのであれば、デザインコンセプトを理解していないかもしれません。 – olaskee

+0

私はZAPツールだけでなく、フィルタvarを見てみましょう。あなたの助けをありがとう...私は他の人がこれについて他の意見を持っている場合は表示されます。 – olaskee

関連する問題