• home
  • forum
  • my
  • kt
  • download
  • PHP Random String Generator

    Author: 2007-08-20 10:19:19 From:

    This is one of my smallest, most powerful, most widely used function. It takes the input bewteen 1-32 and will return the string of that particular length. The string that is generated is completely random, as is the way it is selected within the function.
    The beauty of this is that if a length less than 32 is wanted, it will randomly select the position as well.


    < ?
    function randomString($length)
    {
        // Generate random 32 charecter string
        $string = md5(time());

        // Position Limiting
        $highest_startpoint = 32-$length;

        // Take a random starting point in the randomly
        // Generated String, not going any higher then $highest_startpoint
        $randomString = substr($string,rand(0,$highest_startpoint),$length);

        return $randomString;

    }

    ?>


    To use the function:
    < ?php

    // Gets a random string of length 10
    $randomString = randomString(10);

    ?>



    This function literally has a statistical chance of repeating the same value next to never. Unless, of course, you only want 1 charecter returned. At that point, their are only 34 different possibilities that it could be (a-z and 0-9). Other then that, it will be pretty unique!

    Thats about it! It is pretty self explanitory. Enjoy!

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (8)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (5)
      Postcards (0)
      Randomizing (8)
      Redirection (8)
      Searching (6)
      Security (6)
      Site Navigation (7)
      User Authentication (10)
      WAP and WML (7)
      Web Fetching (0)
      Web Traffic Analysis (11)
      XML and PHP (0)

    New

    Hot