Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

php random number generation

Posted on 2010-11-08
5
Medium Priority
?
244 Views
Last Modified: 2012-05-10
I am designing a booking contact form with PHP and part of the response the client gets on form submission will be a unique 10 character letter string (to identify their booking in the system). Is there code to allow me to generate this unique letter string?
Thanks,
N
0
Comment
Question by:Neil_Bradley
  • 3
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
rogerard earned 1500 total points
ID: 34087685
Here you go, from this website: http://www.lost-in-code.com/programming/php-code/php-random-string-with-numbers-and-letters/

function genRandomString() {
    $length = 10;
    $characters = ’0123456789abcdefghijklmnopqrstuvwxyz’;
    $string = ”;    

    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }

    return $string;
}

Open in new window

0
 
LVL 5

Author Comment

by:Neil_Bradley
ID: 34087767
Looks like what I am after however I am getting a syntax error warning on this line
$string .= $characters[mt_rand(0, strlen($characters))];
0
 
LVL 5

Author Comment

by:Neil_Bradley
ID: 34087849
I modified it a little and it worked great.

function genRandomString() {
    $length = 10;
    $characters = '0123456789abcdefghijklmnopqrstuvwxyz'; 

    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }

    return $string;
}
echo genRandomString();

Open in new window

0
 
LVL 7

Expert Comment

by:rogerard
ID: 34087851
If you copied my code exactly, then you'll need to replace all of the ' with ".  Apparently, what I copied and pasted, php doesn't like.
0
 
LVL 5

Author Closing Comment

by:Neil_Bradley
ID: 34087854
Thanks for that. A slightly modified version worked perfectly.
N
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question