generating a random code in php?

php generate random string fixed length
php generate random alphanumeric string
php random 5 digit number
how to generate unique random number in php
php generate random number
generate 6 digit unique random number in php
generate random number in php without repetition
generate 8 digit unique number in php

i know this might seem silly, but i want to generate a random code of 8 characetrs, only numbers or letters using php. i needs this to generate a password for each user that signs up, thanks

I would rather use md5 to generate passwords

But you can use something like this if you want a custom:

function createRandomPassword() { 

    $chars = "abcdefghijkmnopqrstuvwxyz023456789"; 
    $i = 0; 
    $pass = '' ; 

    while ($i <= 7) { 
        $num = rand() % 33; 
        $tmp = substr($chars, $num, 1); 
        $pass = $pass . $tmp; 

    return $pass; 


generating a random code in php?, I would rather use md5 to generate passwords. But you can use something like this if you want a custom: function createRandomPassword()� A random integer between min (or 0) and max (or getrandmax() inclusive) Return Type: Integer: PHP Version: 4+ PHP Changelog: PHP 7.1: The rand() function is an alias of mt_rand(). PHP 4.2.0: The random number generator is seeded automatically.

What about something like this, for ease:

$pass = substr(md5(uniqid(mt_rand(), true)) , 0, 8);

rand - Manual, If you want a random number between 5 and 15 (inclusive), for example, use rand(5, 15). Caution. This function does not generate cryptographically secure� Random Floats. Instead of generating random integers, you might also want to generate floats. This can be done effortlessly by simply dividing a random number with a value returned by mt_getrandmax(). The following example will illustrate how to generate a random float between 0 and 1 or between any other minimum and maximum limits.


$uniqid = uniqid();

$rand_start = rand(1,5);

$rand_8_char = substr($uniqid,$rand_start,8);


Generate Random Alphanumeric Strings in PHP, Generating Random Numbers in PHP. There are three different functions for generating random numbers in PHP. All of them will accept a minimum and maximum possible value for the random numbers and output a random number for you. These are rand($min, $max) , mt_rand($min, $max) , and random_int($min, $max) . Note: As of PHP 7.1.0, rand() uses the same random number generator as mt_rand(). To preserve backwards compatibility rand() allows max to be smaller than min as opposed to returning FALSE as mt_rand() .

A good or efficient method for doing this is:

public function generateRandomString($length = 8) {
    $characters = '0123456789abcdefghijklmnopqrs092u3tuvwxyzaskdhfhf9882323ABCDEFGHIJKLMNksadf9044OPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    return $randomString;

By changing $length variable you can generate alphanumeric code according to your need.

PHP rand() Function, The rand() function generates a random integer. Tip: If you want a random integer between 10 and 100 (inclusive), use rand (10,100). Tip: The mt_rand() function produces a better random value, and is 4 times faster than rand(). Generate unique random number in PHP. In this tutorial, we would love to share with you how to generate 2,4,6,10,12 digit unique random number in PHP. Generate unique random number in PHP. You can use the php rand() and mt_rand() function to generate 2,4,6,10,12, etc digit unique random number in PHP. PHP rand() function

Use base64_encode(), feed it some rand() numbers, and cut off the first 8 characters, which are definitely letters or numbers. That's not a totally random combination due to the input being just integers. But it's good enough for default user-passwords. (Else use rand() via chr() before encoding.)

Generating Random String Using PHP, Generate a random number using rand() function. Hash it using one of the above functions. Program 1:. The rand() PHP function can also be used to generate a random number within a specific range, such as a number between 10 and 30. If no max limit is specified when using the rand() PHP function, the largest integer that can be returned is determined by the getrandmax() function, which varies by operating system.

Using the PHP Rand() Functions to Generate Random Numbers, Learn how to generate a random number in PHP with the rand() function, plus other features like how to select a random number within a� You can use the coupon code generator PHP class file to generate N number of coupons and its customizable, with various options of adding own mask with own prefix and suffix. Simple PHP coupon code generator. Example: coupon::generate(8); // J5BST6NQ

PHP random string generator Code Example, TreeBuilder::getRootNode()" before creating the root node is not supported, migrate to the new constructor signature instead. trigger stock� Generating a random hex color code with PHP. Ask Question Asked 9 years, 4 months ago. Active 1 year, 7 months ago. Viewed 113k times 74. 21. I'm working on a project

Generating Unbiased Random Passwords in PHP, We start by discussing a few common, but incorrect, ways of generating passwords, then provide a secure password generator class for PHP. Common Mistakes. Using PHP, it's pretty easy to generate a random password. Using the function below you can specify what kind of symbols your password(s) should contain, what the password length should be, and how many passwords you want to generate.

  • Have you googled "php random password generator" - there's a whole lot of hits...
  • using the md5() idea, you could take the $pass your are returning now and instead do return substr(md5($pass), 0, 8);
  • i presume the L and 1 are left on purpose! because they look alike?
  • The advantage of this over md5 or some other hash function is that you can leave out characters that look similar, such as the above example leaves out L and 1.
  • I'd now use something like this: ``` $chars = "abcdefghijkmnopqrstuvwxyz023456789"; $i = 0; $token = ''; while ($i <= 7) { $num = \random_int(0, 33); $token .= $chars{$num}; $i++; } ``` (uses CSPRNG/pure randomness PHP functions)
  • you might want to salt the time otherwise a hacker might be able to take advantage. it's a long shot, but still possible.
  • I think 'long shot' is an understatement. ;)
  • still, all some hackers has, is time :-)
  • fine! did not do what you told me to do on principal, BUT I made the starting position for the substring random. better? ;)
  • i just did a little research and it appears this whole back-and-forth was for naught, you can just add true to the second parameter of uniqid() and it will add entropy for unique values :-)
  • another option would be to use rand() only once by setting the min & max number allowed ie- $pwd = rand(10000000, 99999999);
  • @Patrick -- good thought! If speed is a problem then you can replace the interior random calls with string concatenation alone or a call to char.