How to Encrypt and Decrypt Password in php Using md5() Login System || Password Encryption in PHP

 How to Encrypt and Decrypt Password in php Using md5() Login System || Password Encryption in PHP





Login.php

<?php
session_start();
$con = mysqli_connect("localhost", "root", "", "loginyt") or die("Con Error");



if (isset($_POST['btn'])) {
$un = $_POST['uname'];
$ps = $_POST['pass'];

$sql = "SELECT * FROM user WHERE uname = '$un'";
$q = mysqli_query($con, $sql);
$num = mysqli_num_rows($q);

if ($num == 1) {

$data = mysqli_fetch_assoc($q);

$upass = $data['pass'];

if (md5($ps) == "$upass") {
$_SESSION['user'] = $un;
header("Location: index.php");
} else {
echo "Invalid Username Or Password";
}
} else {
echo "Invalid Username Or Password";
}
}


?>




<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
</head>

<body>

<form action="" method="post">
<input type="text" name="uname" placeholder="username">
<br><br>
<input type="password" name="pass" placeholder="password">
<br><br>
<input type="submit" value="Login" name="btn">

</form>


</body>

</html>



index.php

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>

<body>

<?php
session_start();
echo $ses = $_SESSION['user'];
if ($ses == false) {
header("Location: login.php");
}
?>
<br>
<a href="logout.php">Logout</a>

</body>

</html>


Logout.php

<?php
session_start();
session_unset();
header("Location: login.php");

?>









Post a Comment

Previous Post Next Post