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 9 of 9
  1. #1
    Senior Coder
    Join Date
    Dec 2004
    Location
    Essex, UK
    Posts
    2,636
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Random String Generator

    Is there anyway to create a random string of characters/numbers/symbols in php and then add it to a text box on a page?

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,773
    Thanks
    19
    Thanked 155 Times in 146 Posts
    For the numbers you could use rand() .

    For the characters, symbols: you put all the characters of the english language into one array, and whatever symbols you want to use into another array....then you can use array_rand() to pick several random elements from each of these arrays...

    Then use concatenation to string them together into a larger string....the result would look like:

    3494ejdf&*#%
    There might be a better way to do this, but that's certainly one way...
    Last edited by chump2877; 06-27-2005 at 08:25 PM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #3
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    This should generate a string out of any normal character:
    PHP Code:
    <?php
    function rand_str($len)
    {
        
    $str '';
        for(
    $i 0$i $len$i++)
        {
            
    $str .= chr(mt_rand(32126));
        }
        return 
    $str;
    }
    ?>
    Last edited by marek_mar; 06-27-2005 at 08:32 PM.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #4
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,773
    Thanks
    19
    Thanked 155 Times in 146 Posts
    So that function returns a longish number that reads as a string, right marek?
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #5
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    It accepts the length as argument.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #6
    Senior Coder
    Join Date
    Dec 2004
    Location
    Essex, UK
    Posts
    2,636
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks alot.

    Um, I'm very new to php so how would I say have that function in page - when a button is clicked, the function is run and a text box shows the random string...?

  • #7
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,773
    Thanks
    19
    Thanked 155 Times in 146 Posts
    You can either reload the same page when submit button is pressed, by setting the action of the form equal to:

    Code:
    <form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
    or load another page:

    Code:
    <form method="post" action="another_page.php">
    Whichever one you use, that is where you will execute the function in your script....

    Then, after the script has run, set the value of your target textbox to whatever variable you assigned to your random string of characters:

    If your random string of characters is set to $random_string, then the text box code would be:

    Code:
    <input type="text" name="name" value="<? echo $random_string; ?>">
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #8
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    The page with the function is called. Anywhere you like you just print what the function returns. Example:
    PHP Code:
    <input type="text" value="<?php print rand_str($length);?>">
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #9
    Senior Coder
    Join Date
    Dec 2004
    Location
    Essex, UK
    Posts
    2,636
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, that's helped alot.


  •  

    Posting Permissions

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