Solved

php random number generation

Posted on 2010-11-08
5
230 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 500 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now