Jump to: navigation, search

PHP Login Form

From w3cyberlearnings

Contents

PHP Login Form

User requires to enter password and user name. A correct user name and password, user will direct to the home page,and an incorrect user name and password, user will direct to the login page.

Step 1(login.php)

  • This example, we user array to store the user name and password.
<?php
$user_login = array(
	 'userbob' => 'passwordbob',
	 'userjohn' => 'passwordjohn'
);

$msg = "";
if (!empty($_POST['login_name']) && !empty($_POST['login_password'])) {
	foreach ($user_login as $user => $pass) {
		if (($_POST['login_name'] == $user) &&
				  ($_POST['login_password'] == $pass)) {
			header('location: home.php?name=' . $_POST['login_name']);
		} else {
			header('location:login.php');
		}
	}
} else {
	$msg = 'please enter your name and password correctly!<br/>';
}
?>

<html>
<head>
<title>Login Form</title>
</head>
<body>
<b><?php echo $msg; ?></b>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<table border="0">
	<tr>
		<td>
			Login Name:
		</td>
		<td>
			<input type="text" name="login_name" size="20" maxlength="20"/>
		</td>
	</tr>
	<tr>
		<td>
			Password:
		</td>
		<td>
		     <input type="password" name="login_password" size="20" maxlength="20" />
		</td>
	</tr>
	<tr>
		<td colspan="2"><input type="submit" value="submit" /></td>
	</tr>
</table>
</form>
</body>
</html>



Step 2(home.php)


<?php

if ($_GET['name']) {
	echo 'Welcome !!! <br/>';
	echo 'your name is ' . $_GET['name'] . '<br/>';
}
?>

Output

Php form login 1.png

Related Links


HTML Form
  1. PHP Login Form
  2. PHP Feedback Form
  3. PHP Register Form
  4. PHP Search Form
  5. PHP Image Upload and MySQL
  6. PHP Multiple Forms
  7. PHP Generate Form by Ajax
Navigation
Web
SQL
MISC
References