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.

PHP Username Check Against Password

Discussion in 'PHP' started by KidGuko, Sep 22, 2006.

  1. #1
    I need help creating some code to check and make sure that the password does not contain the username within it.

    For instance:

    Username: blah
    Password: blah123

    or

    Username: blah
    Password: 12blah34

    I need to make sure that the username is no where in the password.


    Thanks :)
     
    KidGuko, Sep 22, 2006 IP
  2. kjewat

    kjewat Active Member

    Messages:
    149
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Just use the strpos() function...

    
    $password = 'abcdefghijk';
    $username  = 'cdef';
    
    if(strpos($password , $username  ))
    {
        echo "The username can not be contained in the password."
        //print the form again.
    
    }
    
    ?>
    
    PHP:
     
    kjewat, Sep 22, 2006 IP
  3. KidGuko

    KidGuko Peon

    Messages:
    149
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Awesome works...thanks a bunch.
     
    KidGuko, Sep 22, 2006 IP
  4. Cropsy

    Cropsy Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    There is a small problem with kjewat's code, strpos will return 0 if the username is at the start of the password. This means the example code will allow exactly the same username and password. I'd suggest replacing the strpos line with

    if(stripos($password, $username)!==false)
    Code (markup):
    which checks against boolean false (which is returned only if the string is not found) and not 0 or ''. It's also case insensitive so using an uppercase version of the username in the password isn't allowed.

    The php man page for stripos probably explains it better than I just have.
     
    Cropsy, Sep 22, 2006 IP
  5. KidGuko

    KidGuko Peon

    Messages:
    149
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    yelp...I figured that part out...he just helped to give me the direction to go in :)

    But thanks for clarifying that...
     
    KidGuko, Sep 23, 2006 IP
  6. intoex

    intoex Peon

    Messages:
    414
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    you also can lowercase both of variables to check, if password not containt username without depending on char case
     
    intoex, Sep 24, 2006 IP
  7. KidGuko

    KidGuko Peon

    Messages:
    149
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    good ideal...all these ideas are helping me gain knowledge and experience...thanks everyone
     
    KidGuko, Sep 24, 2006 IP