-2

i am new in php and i am working on a login and registration form . I created a function to check if the email address is valid or not even though i did't get any errors but it don't seem to work. It would be a great help if anyone here could take a look at my code.

     <?php
      $firstname="";
      $lastname="";
      $email="";
      $password="";
      $confirm_password="";
      $error= array("firstname"=>"",
                    "lastname" =>"",
                     "email"    =>"",
                     "password" =>"",
                      "confirm_password"=>""    );

     function correct_email(){  
         if(!empty($email)){
           if(filter_var($email,FILTER_VALIDATE_EMAIL)){
              return true;
               }else{
                 echo"your email address is not valid";
                      }
                          }
                   return false;
                   }

           function correct_password(){
                  global $error;
                 if(empty($error['password'])){
      if(strcmp($_POST['password'],$_POST['confirm_password'])==0){
        return true;
        }else{
           $error['password']="Password didnot match";
           $error['confirm_password']="Password didnot match";
           }
          }
       return false;
        }

         function validate(){
         global $error;
         $valid= true;
          foreach($_POST as $key=>$value){
            if(empty($value)){
            $valid=false;
            $error[$key]="This field is blank";
            }
           }
        return correct_password() && correct_email() && $valid;
          }


         if($_SERVER['REQUEST_METHOD']=="POST"){
         $firstname=$_POST['firstname'];
         $lastname=$_POST['lastname'];
          $email=$_POST['email'];
         $password=$_POST['password'];
          $confirm_password=$_POST['confirm_password'];
      if(validate()){
             echo "registration succesful";
            die;
            }
          else{
           echo "couldnot register";

             }
              }
       ?>
<html>
<head>
<title>Registration</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
   <h1>New Memebers Registration</h1>
   <form method="post" action="register.php">
    <table>
 <tr>
   <td>Firstname:</td>
   <td><input type="text" name="firstname" value="<?= $firstname ?>">
    <?=      $error['firstname'] ?> </td>
   </tr>
   <tr>
  <td>Lastname:</td>
  <td><input type="text" name="lastname" value="<?= $lastname ?>">
           <?= $   error['lastname'] ?> </td>
   </tr>
   <tr>
<td>Email:</td>
<td><input type="text" name="email" value="<?= $email ?>"> 
        <?= $error['email'] ?> </td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" value="<?= $password ?>"> 
       <?=   $error['password'] ?> </td>
</tr>
<tr>
<td>Confirm Password:</td>
<td><input type="password" name="confirm_password" 
  value="<?=    $confirm_password ?>"> 
       <?= $error['confirm_password'] ?>  </td>
</tr>
<tr>
  <td colspan="2" class="pullright">
         <input type="submit" value="Register">              </td>
 </tr>
  </table>
  </form>
  </body>
   </html>
John Conde
  • 217,595
  • 99
  • 455
  • 496

2 Answers2

4
  1. You don't pass $email to your function so it is not available within your function due to scope. See variable scope to learn more about this.

  2. filter_var() will return false if the email address is blank so you don't need to check that.

  3. You should not echo out anything from your function. Just return true or false and let the code do the error handling.

  4. But if you insist on setting error message in your code you are being inconsistent in your code as you echo an error message here instead of capturing it in your $error array like elsewhere in your code.

  5. You can return true by default and only return false on error which shortens up your code a bit.

Your function:

correct_email($email) {  
     if (!filter_var($email,FILTER_VALIDATE_EMAIL)) {
          $error['email'] = "your email address is not valid";
          return false;
     }
     return true;
}

Calling it:

return correct_password() && correct_email($email) && $valid;

I recommend removing any reference to global and pass all needed variables as parameters as using global is considered a bad programming practice.

John Conde
  • 217,595
  • 99
  • 455
  • 496
2

the variable is outside the function scope.

you need to define the function like:

function correct_email($email)...

when you call it parse email first to validate() then to correct_email:

 correct_email($email)

or use the global

correct_email($_POST['email'])