Login Form

<!DOCTYPE html>
<html>
	<head>
		<title>Login Form</title>
	</head>
	<body>
		<form  method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
			<input type="text" name="username" placeholder="Username" />
			<input type="password" name="password" placeholder="Password" />
			<input type="submit" value="Login" />
		</form>


		<?php
			if(isset($_POST['username']) && isset($_POST['password'])) {

				/* Associative Array with md5 encrypted password */
				$loginfo = array("geek12" => md5("1234"), "abc4" => md5("abcd"), "jcb84" => md5("msk53"));
				
				/* Trim whitespace from the beginning and end */
				/* Compare with regular expression */
				if((trim($_POST['username']) == '') || (ereg ('[^a-zA-Z0-9]', $_POST['username'])))
					echo "Invalid username...Usernames only contain letters and digits.";
				else {
					if((trim ($_POST['password']) == '') || (ereg ('[^[:alnum:][:punct:][:space:]]', $_POST['password'])))
						echo "Invalid password...Passwords only contain letters, digits, punctuation and spaces.";
					else { 

						/* Checking username exist or not */
						if(isset($loginfo[$_POST['username']])) {

							/* Checking password associated with matched username */
							if($loginfo[$_POST['username']] == md5($_POST['password'])) {
								/* Set session */
								session_start();
								$_SESSION["user_id"] = $_POST['username'];
								echo "Authenticated!";
							}
							else
								echo "Password didn't match!";
						}
						else
							echo "Username didn't match!";
					}
				}
			}
		?>
	</body>
</html>



/* Output */
Input:
username = geek12
password = 1234

Authenticated!

Comments (0)

  • To add your comment please or

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.

Got It!