Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Create string with random characters

  1. #1
    Join Date
    Mar 2009
    Location
    Chennai, India
    Posts
    77
    Thanks
    16
    Thanked 7 Times in 6 Posts

    Default Create string with random characters

    Is there any function to create a string with random characters and numbers? This string will be used to create passwords..

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,625
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    I don't think PHP support a function that can be used to generate a password or a security code. You have to develop it using a custom algorithm. You can also find lots of random password generators in PHP through google.

  3. #3
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    PHP Code:
    function randomAlphanumericPassword($length 5){
        
    $characters array_merge(range('a''z'), range(09));
        
    shuffle($characters);
        
    $password array_slice($characters0abs($length));
        return 
    implode($password);
    }
    //EXAMPLE
    echo randomAlphanumericPassword(6); 

  4. The Following User Says Thank You to techietim For This Useful Post:

    borris83 (03-31-2009)

  5. #4
    Join Date
    Mar 2009
    Location
    Chennai, India
    Posts
    77
    Thanks
    16
    Thanked 7 Times in 6 Posts

    Default

    Hi Techietim!

    The function works great.. Thanks

  6. #5
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    That is pretty slick Tim. I've never seen one done that way.

    Only thing I would do differently is get rid of 0 (Zero), 1 (one) and l (lowercase L).

    Nice job.

    PHP Code:
    $characters array_merge(range('a''k'), range('m',  'z'), range(29)); 
    Last edited by JasonDFR; 03-31-2009 at 02:25 PM. Reason: Forgot to get rid of l

  7. #6
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    I can understand the lowercase L being removed, but why take out 1?

  8. #7
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Same reason. Depending on the font, 1 and l can look similar. Get rid of O and I as well if allowing uppercase letters. When something as simple as this can avoid potential problems for some users, why not?

  9. #8
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Fair enough but if it's going to be part of a hyperlink that's randomly generated then there's no point in removing specific letters since the user will click it and not need to worry about there being 1s that look like ls.

    But of course, if this is going to be for a different purpose then what you say is true and removing those characters will help to avoid confusion.

  10. #9
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    I think it is for generating passwords.
    Code:
    function randomAlphanumericPassword

  11. #10
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    I seem to have lost the ability to read today =/

Tags for this Thread

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
  •