SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict seanmayhew's Avatar
    Join Date
    Aug 2002
    Posts
    209
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Comparing two password fields for insertion into database

    I have two form fields a 'password' and 'confirmpassword' field I just want to make sure the two text strings match. So on my validation page before the password is submitted I want to compare the two.

    Code From my form
    Code:
    <td width="150" align="left" valign="top" class="FormText"> 
    <div align="right">Password</div></td>
    <td width="250"> <input name="password" type="password" id="password" class="FormText"></td>
    </tr>
    <tr> 
    <td width="150" align="left" valign="top" class="FormText"> 
    <div align="right">Confirm Password</div></td>
    <td width="250"> 
    <input name="confirmpassword" type="password" id="confirmpassword" class="FormText"></td>
    Code From the Validation Page
    PHP Code:
    // Define post fields into simple variables
    $first_name $_POST['first_name'];
    $last_name $_POST['last_name'];
    $email_address $_POST['email_address'];
    $username $_POST['username'];
    $password $_POST['password'];
    $confirmpassword $_POST['confirmpassword'];
    $info $_POST['info'];
    /* Let's strip some slashes in case the user entered
    any escaped characters. */
    $first_name stripslashes($first_name);
    $last_name stripslashes($last_name);
    $email_address stripslashes($email_address);
    $username stripslashes($username);
    $password stripslashes($password);
    $confirmpassword stripslashes($confirmpassword);
    $info stripslashes($info); 
    Where Im at with the function for comparing the two string:
    PHP Code:
     function confirmthepasswords($password,$confirmpassword){
    if(
    $password == $confirmpassword){
    $passwordOK $password;
    }
    else{
    $passwordOK

    return $passwordOK;

    And from there I plan to use the $passwordOK variable to insert the password into the database. My logic I dont think makes sense here Any ideas?

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Montgomery, TX
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would do something like this...

    PHP Code:
    function confirmthepasswords($password,$confirmpassword)
    {

      
    $passwordOK "";

      if(
    $password == $confirmpassword)
        {
        
    $passwordOK $password;
        }

      return 
    $passwordOK;

    And when you call confirmthepasswords, check the return value, if it's blank, then the passwords don't match, else, everything's cool.

    HTH,

    David

  3. #3
    Free your mind Toly's Avatar
    Join Date
    Sep 2001
    Location
    Panama
    Posts
    2,182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems ok to me. I would use something easier to compare the passwords though. Something like...

    PHP Code:
    <?php
     
    if ($password != $confirmpassword) {
    echo(
    "Error... Passwords do not match");
    exit;
    }
     
    ?>
    Then just insert the $password variable into the dbase.
    Community Guidelines | Community FAQ

    "He that is kind is free, though he is a slave;
    he that is evil is a slave, though he be a king." - St. Augustine


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •