1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Validating email, username, password, and password confirm in PHP

Discussion in 'PHP' started by Tony Brar, May 1, 2012.

  1. #1
    Hi guys i need help. I have been working on this for sooo long with nothing good happening.
    I am trying to make it so users can submit info. so far all i need help with is the validation.
    here is the form
    
    <div id="registrationbox">
    <form action="processreg.php" method="post">
    Username: <input type="text" name="username" />
    Email: <input type="text" name="email" />
    Password: <input type="text" name="password" />
    Confirm Password: <input type="text" name="confirmedpassword" /><input type="submit"/>
    </form>
    </div>
    
    HTML:
    here is processreg.php (validation only)
    <?php
    //start validations
    //validate email
    if (strpos($_POST["email"], "@")<1 or strpos($_POST["email"], ".")<strpos($_POST["email"], "@")+2 or strpos($_POST["email"], ".")+2>=strlen($_POST["email"]) or strlen($_POST["email"])<8)
    $validemail=0;
    else
    {
    $validemail=1;
    //validate username
    $usernamelength=strlen($_POST["email"]);
    if ($usernamelength<3 or $usernamelength>11)
    $validusername=0;else{$validusername=1;
    //validate password
    $passwordlength=strlen($_POST["password"]);
    if ($passwordlength<6 or $passwordlength>12)
    $validpassword=0;
    else
    {$validpassword=1;
    //validate password confirmation
    if ($_POST["password"]===$_POST["confirmedpassword"])
    $validpassconfirm=1;
    else$validpassconfirm=0;
    }
    }
    }
    //end validations
    //if valid or not valid
    if ($validemail=0 or $validusername=0 or $validpassword=0 or $validpassconfirm=0)exit("Invalid registration.");
    elseif ($validemail=1 and $validusername=1 and $validpasswandd=1 and $validpassconfirm=1)
    print "Valid registration"
    //from here on, valid data is saved...
    ?>
    
    PHP:
    the problem is, whether I give it invalid or valid info, it says registration valid. ALWAYS!!!

    please somebody help me, tell me what to do to the code to make it work. if at all possible, make it simpler, but just please make it work!

    - Tony
     
    Solved! View solution.
    Tony Brar, May 1, 2012 IP
  2. downloadphpscripts

    downloadphpscripts Peon

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    0
    #2
    Hi. The first thing i noticed was that you don't have conditional statements at the end of the script:

    
    //end validations
    //if valid or not valid
    if ($validemail=0 or $validusername=0 or $validpassword=0 or $validpassconfirm=0)exit("Invalid registration.");
    elseif ($validemail=1 and $validusername=1 and $validpasswandd=1 and $validpassconfirm=1)
    
    
    PHP:

    These should look like this: (notice the double ==)
    $validemail==1 and $validusername==1 and $validpasswandd==1 and $validpassconfirm==1

    I hope that helps.
     
    downloadphpscripts, May 1, 2012 IP
  3. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #3
    okay, I'll try that

    also, I said exit if not valid

    
    if ($validemail=0 or $validusername=0 or $validpassword=0 or $validpassconfirm=0)
    exit("Invalid registration.");
    
    PHP:
    and i'll make it print valid if it is (just for testing)

    - Tony
     
    Tony Brar, May 2, 2012 IP
  4. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #4
    here is the new code: (please look through carefully, more than one change)
    
    if ($validemail==0 or $validusername==0 or $validpassword==0 or $validpassconfirm==0)
    {
    print "Invalid registration.";
    exit();
    }
    if ($validemail==1 and $validusername==1 and $validpassword==1 and $validpassconfirm==1)
    {
    print "Valid registration";
    //from here on, valid data is saved...
    }
    ?>
    
    PHP:
    now, if i give the script an invalid registration, it says "Invalid registration"
    but, if i give it a valid registration, it does nothing and displays a blank page. It should say "Valid registration" but does not. Why?
    Please help.
    If somebody can completely solve this problem, I will give best answer.
     
    Tony Brar, May 2, 2012 IP
  5. downloadphpscripts

    downloadphpscripts Peon

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    0
    #5
    Hi Tony,

    At this point i'd try to echo out the results of the the validation variables. For example:

    
    if ($validemail==0 or $validusername==0 or $validpassword==0 or $validpassconfirm==0)
    {
    print "Invalid registration.";
    exit();
    }
    if ($validemail==1 and $validusername==1 and $validpassword==1 and $validpassconfirm==1)
    {
    print "Valid registration";
    //from here on, valid data is saved...
    }
    
    echo "Valid Email: " . $validemail . "<br />";
    echo "Valid Username: " . $validusername . "<br />";
    echo "Valid Password: " . $validpassword . "<br />";
    echo "Valid Passwordconfirm: " . $validpassconfirm . "<br />";
    
    PHP:
    One of those values is not going to be 1 (since it's not validating) and it will help find exactly where the problem lies.
     
    downloadphpscripts, May 2, 2012 IP
  6. downloadphpscripts

    downloadphpscripts Peon

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    0
    #6
    That syntax error might be causing problems. I went ahead an spread out the validation code and added the curly brackets to make it a little easier to read. Hopefully this will work for you.

    
    //start validations
    //validate email
    if (strpos($_POST["email"], "@")<1 or strpos($_POST["email"], ".")<strpos($_POST["email"], "@")+2 or strpos($_POST["email"], ".")+2>=strlen($_POST["email"]) or strlen($_POST["email"])<8)
    {
    $validemail=0;
    }
    else
    {
      $validemail=1;
      //validate username
      $usernamelength=strlen($_POST["email"]);
      if ($usernamelength<3 or $usernamelength>11)
      {
        $validusername=0;
      }
      else
      {
        $validusername=1;
        //validate password
        $passwordlength=strlen($_POST["password"]);
        if ($passwordlength<6 or $passwordlength>12)
        {
          $validpassword=0;
        }
        else
        {
          $validpassword=1;
          //validate password confirmation
          if ($_POST["password"]===$_POST["confirmedpassword"])
          {
          $validpassconfirm=1;
          }
          else
          {
          $validpassconfirm=0;
          }
        }
      }
    }
    //end validations
    
    PHP:
     
    downloadphpscripts, May 2, 2012 IP
  7. webshore88

    webshore88 Well-Known Member

    Messages:
    130
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #7
    I will recommend you to use function for every check, as function valid_email(), valid_pass(). if valid_email is true then check for valid password else show error at that point. this way your system will be clean and easy to debug and user small var names, use
    $usernamelength as $uname_len.
     
    webshore88, May 2, 2012 IP
  8. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #8
    okay, did what you said. that was a good idea, and exposed an error: when I told it to echo the values of $validemail, $validusername, etc it DOESN'T display the 1s and 0s like it should. that sorta isolates the problem a little more.

    I'll think about it and look at the code again. We'll solve this soon.

    BTW when I put in the code to echo $_POST values, it worked fine.:confused:
     
    Tony Brar, May 2, 2012 IP
  9. #9
    Excellent! It looks like it may be in the email validation. You could change this:

    
    //validate email
    if (strpos($_POST["email"], "@")<1 or strpos($_POST["email"], ".")<strpos($_POST["email"], "@")+2 or strpos($_POST["email"], ".")+2>=strlen($_POST["email"]) or strlen($_POST["email"])<8)
    {
    $validemail=0;
    }
    
    PHP:
    To This:

    
    //validate email
    if (filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false)
    {
    $validemail=0;
    }
    
    PHP:
     
    downloadphpscripts, May 2, 2012 IP
  10. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #10
    i did what you said. it didn't help.
    however, now, for some reason, my problem is solved.
    thanks
    although you didn't give me a single miracle answer, downloadphpscripts, you kept helping until the problem was solved. i will give your above post best answer
     
    Tony Brar, May 2, 2012 IP