• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

php random number generation

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
Neil_Bradley
Asked:
Neil_Bradley
  • 3
  • 2
1 Solution
 
rogerardCommented:
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
 
Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
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
 
Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
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
 
rogerardCommented:
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
 
Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
Thanks for that. A slightly modified version worked perfectly.
N
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now