Solved

php random number generation

Posted on 2010-11-08
5
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
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 …

739 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