• You MUST read the Babiato Rules before making your first post otherwise you may get permanent warning points or a permanent Ban.

    Our resources on Babiato Forum are CLEAN and SAFE. So you can use them for development and testing purposes. If your are on Windows and have an antivirus that alerts you about a possible infection: Know it's a false positive because all scripts are double checked by our experts. We advise you to add Babiato to trusted sites/sources or disable your antivirus momentarily while downloading a resource. "Enjoy your presence on Babiato"

Could someone help me with the php code?

zorakila

Member
Banned User
May 6, 2019
44
6
8
I need a simple php code that generates random numbers
with 16 digits.
Example:
0000-0000-0000-000
in this format with the separations and a button to generate random.
Could anyone help?
 
You aren't getting responses because you haven't tried it by yourself and you want someone to write it for you. We can help. Show us the code you wrote until now and ask whatever you don't understand about it. It's like 3-4 lines of code. What are the errors that you are facing and so on. Alternatively, ask Google.
 
  • Like
Reactions: PrinceK
I may regret this. Don't PM me to write your code. Create threads and tag me. If I can help, I'll jump in.

PHP:
// Generate a random 16-digit number
$random_number = str_pad(mt_rand(1, 9999999999999999), 16, '0', STR_PAD_LEFT);

// Format the number with dashes every 4 digits
$random_number = chunk_split($random_number, 4, '-');

// Remove the last dash
$random_number = rtrim($random_number, '-');

// Output the formatted number
echo $random_number;
 
Please provide the required code
And if you need any modification I will help you


Code:
<?php
function generateRandomNumber() {
    $part1 = str_pad(mt_rand(0, 9999), 4, '0', STR_PAD_LEFT);
    $part2 = str_pad(mt_rand(0, 9999), 4, '0', STR_PAD_LEFT);
    $part3 = str_pad(mt_rand(0, 9999), 4, '0', STR_PAD_LEFT);
    $part4 = str_pad(mt_rand(0, 999), 3, '0', STR_PAD_LEFT);
    return "{$part1}-{$part2}-{$part3}-{$part4}";
}

if (isset($_POST['generate'])) {
    $randomNumber = generateRandomNumber();
} else {
    $randomNumber = "";
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Generate Random Number</title>
</head>
<body>
    <form method="post">
        <label for="number">Random Number:</label>
        <input type="text" name="number" id="number" value="<?php echo $randomNumber; ?>" readonly><br><br>
        <input type="submit" name="generate" value="Generate">
    </form>
</body>
</html>
 
here you go


PHP:
<?php
function generateRandomNumber() {
  $random_bytes = random_bytes(8);
  $num = hexdec(bin2hex($random_bytes)) % (10 ** 16); // Convert random bytes to a 16-digit number
  $formatted_num = sprintf('%04d-%04d-%04d-%04d', $num / 10 ** 12, ($num / 10 ** 8) % 10 ** 4, ($num / 10 ** 4) % 10 ** 4, $num % 10 ** 4); // Format number with hyphens
  return $formatted_num;
}
?>

<html>
<head>
  <title>Random Number Generator</title>
</head>
<body>
  <form>
    <label>Random Number:</label>
    <input type="text" name="random_number" value="<?php echo generateRandomNumber(); ?>" readonly>
    <br>
    <button type="submit">Generate Random Number</button>
  </form>
</body>
</html>
 
Please provide the required code
And if you need any modification I will help you


Code:
<?php
function generateRandomNumber() {
    $part1 = str_pad(mt_rand(0, 9999), 4, '0', STR_PAD_LEFT);
    $part2 = str_pad(mt_rand(0, 9999), 4, '0', STR_PAD_LEFT);
    $part3 = str_pad(mt_rand(0, 9999), 4, '0', STR_PAD_LEFT);
    $part4 = str_pad(mt_rand(0, 999), 3, '0', STR_PAD_LEFT);
    return "{$part1}-{$part2}-{$part3}-{$part4}";
}

if (isset($_POST['generate'])) {
    $randomNumber = generateRandomNumber();
} else {
    $randomNumber = "";
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Generate Random Number</title>
</head>
<body>
    <form method="post">
        <label for="number">Random Number:</label>
        <input type="text" name="number" id="number" value="<?php echo $randomNumber; ?>" readonly><br><br>
        <input type="submit" name="generate" value="Generate">
    </form>
</body>
</html>
What is your problem with this code exactly?

I would instead change the name of the input field to "randomNumber" to match the variable name or change the variable name to "number" to match the input field name.

Because now, you assign the generated random number to a variable named "$randomNumber". Other than that, this is a working code.
Also, you can check if the generate button was clicked and generated a random number, or if it's not, set the $randomNumber to an empty string.

PHP:
$randomNumber = isset($_POST['generate']) ? generateRandomNumber() : "";
 
AdBlock Detected

We get it, advertisements are annoying!

However in order to keep our huge array of resources free of charge we need to generate income from ads so to use the site you will need to turn off your adblocker.

If you'd like to have an ad free experience you can become a Babiato Lover by donating as little as $5 per month. Click on the Donate menu tab for more info.

I've Disabled AdBlock