haikelfazzani

Php Form Validation & Security

Jul 21st, 2017
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.03 KB | None | 0 0
  1. <?php
  2.     if($_SERVER["REQUEST_METHOD"] == "POST"){
  3.        
  4.         $fullName = test_input($_POST['fullname']);
  5.         $Email = test_input($_POST['email']);
  6.         $userName = test_input($_POST['username']);
  7.         $passWord = test_input(sha1($_POST['password']));
  8.        
  9.         echo $fullName . "<br />" . $Email . "<br />" . $userName . "<br />" . $passWord . "<br />";
  10.        
  11.         if(filter_var($Email,FILTER_VALIDATE_EMAIL)){
  12.            
  13.             echo 'your email is valid ' . '<br />';
  14.            
  15.         }else{
  16.            
  17.             echo 'your email is not valid : try again'  . '<br />';
  18.            
  19.         }
  20.        
  21.         if(preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/',$fullName)){
  22.         // or   if(!preg_match('/^[0-9a-zA-Z ]+$/',$fullName))
  23.  
  24.             echo 'bad full name input ';
  25.            
  26.         }
  27.         else{
  28.            
  29.             echo 'good full name input ';          
  30.         }
  31.     }
  32.    
  33.     function test_input($inputField){
  34.        
  35.         $inputField = htmlspecialchars(stripcslashes(strip_tags($inputField)));
  36.         return $inputField;
  37.        
  38.     }
  39.    
  40. ?>
  41.  
  42. <html>
  43.     <head>
  44.         <meta charset='UTF-8'>
  45.         <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  46.     </head>
  47. <body>
  48.    
  49.     <form action="<?php htmlspecialchars($_SERVER['PHP_SELF']) ?>" method="POST">
  50.     <h1>CodeJs : Input Validation </h1>
  51.        
  52.         <div class="form-group">
  53.             <label>Full Name:</label>
  54.             <input type="text" class="form-control" name='fullname'>
  55.         </div>
  56.        
  57.         <div class="form-group">
  58.             <label>Email address:</label>
  59.             <input type="text" class="form-control" name='email'>
  60.         </div>
  61.        
  62.         <div class="form-group">
  63.             <label>Username :</label>
  64.             <input type="text" class="form-control" name='username'>
  65.         </div>
  66.        
  67.         <div class="form-group">
  68.             <label>Password:</label>
  69.             <input type="text" class="form-control" name='password'>
  70.         </div>
  71.        
  72.         <button type="submit" class="btn btn-default">Submit</button>
  73.     </form>
  74.    
  75. <style>form{ width: 400px; margin-left: 350px; }</style>
  76. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  77. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  78. </body>
  79. </html>
Add Comment
Please, Sign In to add comment