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 string "like" string

Discussion in 'PHP' started by Greenmethod, Aug 25, 2009.

  1. #1
    I am trying to make my password not contain the all OR part of the username. I'm not real sure how to do this...

    If the username is "User123" I would not like the password to contain the following (in any caps/not caps scheme):

    • use
    • ser
    • er1
    • r12
    • 123

    The only thing I can think of is to use a strtoupper/lower and then do a foreach loop using substr and going one space at a time. I'm hoping there's something easier than that though. Thanks in advance!
     
    Greenmethod, Aug 25, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use this, if the password is inside the username you can show an error.

    if (stripos($username, $password) !== false) { 
    //error
    }
    PHP:
     
    premiumscripts, Aug 25, 2009 IP
  3. Greenmethod

    Greenmethod Peon

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    is stripos only available in php 5 or something? Because it's giving me undefined function.
     
    Greenmethod, Aug 25, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Indeed it is, you should really update.. In the mean time:

    
    if (strpos(strtolower($username), strtolower($password)) !== false) {
    
    }
    PHP:
     
    premiumscripts, Aug 25, 2009 IP
  5. Greenmethod

    Greenmethod Peon

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    upgraded, thanks for your help!
     
    Greenmethod, Aug 25, 2009 IP
  6. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #6
    If you want "User123" not to match even "xxserxx" you should use something like this

    
    function compareStrings ($firstString, $secondString, $maxInCommon)
    {
    	for ($i = 0; $i <= strlen($firstString) - $maxInCommon; $i ++) {
    		if (strpos(strtolower($secondString), strtolower(substr($firstString, $i, $maxInCommon))) !== false) {
    			return false;
    		}
    	}
    	return true;
    }
    
    PHP:
    so you can call it like this:
    
    $username = "User123";
    $password = "xxusexx";
    
    echo compareStrings($username, $password, 3) ? "Password valid" : "Password invalid, too similar to username";
    
    PHP:
    EDIT: I see you got your problem solved, but I'll let this small function stay here, someone could make an use of it =)
     
    Gray Fox, Aug 25, 2009 IP
    premiumscripts likes this.
  7. pneulameiro

    pneulameiro Peon

    Messages:
    440
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Excellent function my friend!! I was thinking about this and your solution is the best I found.
     
    pneulameiro, Aug 25, 2009 IP
  8. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Excellent function gray fox :) Though to the original poster, I don't think you should be so restrictive in your passwords. It will only aggrevate users signing up. Ofcourse it depends on which app you're building.
     
    premiumscripts, Aug 25, 2009 IP
  9. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #9
    strpos is also available for PHP4, the difference however is that in PHP4 , strpos will only search for a single character instance instead of an entire string.
     
    kblessinggr, Aug 25, 2009 IP