Generate a list of secure random numbers with PHP and display them in an ordered HTML list. User input is validated with a max limit of 100 numbers.
<?php
// Set the default number of elements
$numeroElementi = 10;
// Maximum limit to avoid excessive load
$maxElementi = 100;
// Check if a value was sent via GET
if (isset($_GET['count'])) {
// Sanitize and convert to integer
$input = filter_input(INPUT_GET, 'count', FILTER_VALIDATE_INT, [
'options' => [
'default' => $numeroElementi,
'min_range' => 1,
'max_range' => $maxElementi
]
]);
if ($input !== false) {
$numeroElementi = $input;
}
}
// Generate the array of random numbers
$numeriCasuali = [];
for ($i = 0; $i < $numeroElementi; $i++) {
// Generate a random number between 1 and 1000
$numeriCasuali[] = random_int(1, 1000);
}
?>
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<title>Random Number Generator</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
input[type=number] { width: 60px; }
ul { padding-left: 20px; }
</style>
</head>
<body>
<h1>Random Number Generator</h1>
<form method="get" action="">
<label for="count">How many numbers to generate? (1-100): </label>
<input type="number" id="count" name="count" min="1" max="100" value="<?php echo htmlspecialchars($numeroElementi); ?>" required>
<button type="submit">Generate</button>
</form>
<h2>Generated numbers:</h2>
<ul>
<?php foreach ($numeriCasuali as $numero): ?>
<li><?php echo htmlspecialchars($numero); ?></li>
<?php endforeach; ?>
</ul>
</body>
</html>PHP 7.0+ installed and configured on the server
Web server (e.g. Apache, Nginx) with PHP support
Web browser to view the page
DEPENDENCIES: no external libraries, only standard PHP
CONFIG: no external configuration required