Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    477
    Thanks
    7
    Thanked 3 Times in 3 Posts

    compare two strings are the same but not as a value

    I am trying to get my script to check if both password fields have the same content but using the standard

    if ($pw1 = $pw2) { //do something } else { // show error message }

    does not work when a password like

    00000 and 0000000000000 is used as these are both the same in value but not in content.

    how do i correct and safely check these are the same for my sign up page.


    EDIT

    i have just tried...

    if (strcmp($pw1,$pw2) == 0) { //do something } else { // show error message }

    but this too does the same, it uses the strings as values.
    Last edited by needsomehelp; 02-27-2010 at 04:49 PM.

  • #2
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    I haven't done this before, but it should be this.

    PHP Code:
    $pw1 $_POST['password1'];
    $pw2 $_POST['password2'];

    if (
    $pw1 != $pw2) {
    //Show error
    }
    else {
    //Process

    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #3
    Regular Coder
    Join Date
    Oct 2009
    Posts
    477
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by masterofollies View Post
    I haven't done this before, but it should be this.

    PHP Code:
    $pw1 $_POST['password1'];
    $pw2 $_POST['password2'];

    if (
    $pw1 != $pw2) {
    //Show error
    }
    else {
    //Process

    i have tried this already in my original code!!

    if the user was to enter say

    00000 zeros in the first
    and

    0000000000 loads of zeros in the second

    if ($pw1 != $pw2) {

    would allow this as valid even though the two 'strings' are not the same

    it is checking them as values not strings.

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    if ($pw1 !== $pw2) {

  • #5
    New Coder
    Join Date
    Mar 2009
    Posts
    90
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Hi,Try comparison of value and type === , !==

    Edit: MattF was faster
    Last edited by koko5; 02-27-2010 at 05:48 PM.


  •  

    Posting Permissions

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