KodeInfo | Learning resources for web and mobile development

PHP Password Validation

September 17th, 2014 18:38:09 by Irfan Iqbal Comments(0) - Views(6989)

In this post let us study how to fetch email and password from database and validate it.

At first we need to make a html file for requesting user to enter email and password .

<html>
<head>
<title>password validation</title>
</head>
<body>
<?php
                echo '<div class="alert alert-warning" role="alert">' . $error . '</div>';
                ?>

<form  method="post" action="admin.php" style="margin-top:100px;margin-left:500px;font-size:40dp;">
					<div class="form-group">

						<label for="exampleInputEmail1">Admin Email</label>
						<input type="email" class="form-control" name="email" placeholder="Enter Email" >
					</div>
					<div class="form-group">
						<label for="exampleInputPassword1">Password</label>
						<input type="password" class="form-control" name="password" placeholder="Password">
					</div>

					<button type="submit" class="btn btn-success"  name="submit"  style="margin-left:100px;margin-top:20px;height:30px;width:100px">
						Submit
					</button>
				</form>

			</body>
		</html>

Now the user will enter a email and password.we need to validate that the email and password provided is correct or not for that we have to make PHP file .let us now validate email and password.

<?php
// Create connection
session_start();
$error = "";

function login($email, $password) {
	$con = mysqli_connect("localhost", "root", "", "shopefy");

	// Check connection
	if (mysqli_connect_errno()) {
		echo "Failed to connect to MySQL: " . mysqli_connect_error();
	}

	$email = mysqli_real_escape_string($con, $_POST["email"]);

	$password = mysqli_real_escape_string($con, $password);
	$query = "SELECT * FROM admin WHERE email='$email' AND password='$password'";

	$result = mysqli_query($con, $query) or die(mysqli_error($con));
	$num_row = mysqli_num_rows($result);

	if ($num_row == 1) {
		while ($row = mysqli_fetch_array($result)) {
			$_SESSION['user_id'] = $row['user_id'];
		}
	} else {
		return false;
	}

	return true;
}

if (isset($_POST['submit'])) {

	$validLogin = login($_POST['email'], $_POST['password']);

	if ($validLogin) {
		header("Location:adminpanel.php");
		exit(0);
	} else {
		$error = "Your Login Name or Password is invalid";

	}

}
?>

If the email and password is incorrect it will display an error .

mysqli_fetch_array:Fetch a row as an associative,a numeric array ,or both.

mysqli_num_rows:Gets the number of rows in the result.

 

Author

  • Irfan Iqbal
    Irfan Iqbal

    Irfan is a web developer and consultant from India. He is the co-founder of KodeInfo, the PHP and Laravel Community . In the meantime he follows other projects, manages everything related to marketing and seo for kodeinfo , works as a freelance backend consultant for PHP applications and studies IT Engineering . He loves to learn new things, not only about PHP or development but everything.

Related

WHY USE A FRAMEWORK OVER PLAIN PHP

WHY USE A FRAMEWORK OVER PLAIN PHP
read more

GETTING STARTED WITH LARAVEL

GETTING STARTED WITH LARAVEL
read more

UNDERSTANDING LARAVEL STRUCTURE

UNDERSTANDING LARAVEL STRUCTURE
read more

UNDERSTANDING LARAVEL ROUTES

UNDERSTANDING LARAVEL ROUTES
read more

comments powered by Disqus