Random Password Generator – closingtags </>
Categories
PHP

Random Password Generator

I’ve had a little bit of downtime at work, and didn’t want to be idle. Luckily for me, I found this awesome subreddit, called /r/dailyprogrammer. They’ve got tons of programming challenges for a wide variety of skills. So I’ve been picking through some simple ones in my down time, and doing some of the easier ones. This one is the most recent challenge I did.

Basically, it just asks you to create a random password generator (like this one) with the ability to specify how many passwords to generate, and how long the string should be. And since the comments are closed, I can’t really submit my solution there, so here is what I came up with, in PHP.

<!DOCTYPE html>
<html>
<head>
</head>
<body>
	<div style="text-align: center; margin: 50px;">
		<form action="passwords.php" method="post" style="margin-bottom: 25px;" enctype="multipart/form-data">
			<label for="inputs"># of passwords: </label>
			<input type="text" name="text1" id="text1"><br><br>
			<label for="inputs">string length: </label>
			<input type="text" name="text2" id="text2"><br><br>
			<input type="submit" name="submit" value="Submit">
		</form>
		<?php
			if($_POST["text1"] && $_POST["text2"]) {
				
				$max = intval($_POST["text1"]);
				$length = intval($_POST["text2"]);
				$count = 0;

				$seed = str_split("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_", 1);
				
				$tehstring = "";

				while($count < $max) {
					for ($i=0; $i < $length; $i++) {
						$tehstring .= $seed[mt_rand(0, count($seed)-1)];
					}
					
					echo $tehstring . "<br>";
					$tehstring ="";
					$count++;
				}
			}
		?>
	</div>
</body>
<footer>
</footer>
</html>

 

By Dylan Hildenbrand

Dylan Hildenbrand smiling at the camera. I have tossled, brown hair, rounded glasses, a well-trimmed and short beard. I have light complexion and am wearing a dark sweater with a white t-shirt underneath.

Author and full stack web developer experienced with #PHP, #SvelteKit, #JS, #NodeJS, #Linux, #WordPress, and #Ansible. Check out my book at sveltekitbook.dev!

Do you like these posts? Consider sponsoring me on GitHub!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.