May 8, 2016
How to make a registration and login page with md5 (password encryption) in PHP

Hello friends in this tutorial, we are going to see how to make a registration and login page with md5 (password encryption) in PHP. This is very easy PHP code but most useful because in this we have not saved the password in string format. So now we going encrypt the password and then after we going store the password into our database. So we can secure the password from hackers because it was encrypted by using md5() PHP function.
conn.php
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php //Database Connection $servername = "localhost"; $username = "root"; $password = ""; $db="test"; //Check Connection $conn = new mysqli($servername, $username, $password, $db); if(!$conn){ die ("Error on the Connection" . $conn->connect_error); } ?> |
register.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php include('conn.php');//DB Connection if($_SERVER['REQUEST_METHOD'] == "POST") { // Username and Password sent from Form $username = mysqli_real_escape_string($conn, $_POST['user']); $password = mysqli_real_escape_string($conn, $_POST['pass']); $password = md5($password); //Password Encrypted $sql = "INSERT INTO userpass(user,pass) values('$username', '$password')"; $result = mysqli_query($conn, $sql); echo "Created Successfully!"; } ?> <!doctype html> <html> <head> <title>SoftAOX | New User Login</title> </head> <body> <h1>New User Login</h1> <form action="<?php $_SERVER['PHP_SELF'];?>" method="post"> <label>Username</label> <input type="text" name="user"><br/><br/> <label>Password</label> <input type="password" name="pass"><br/><br/> <input type="submit" name="submit" value="Create"><br/> <p><a href="login.php">Login</a></p> </form> </body> </html> |
login.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php include('conn.php'); session_start(); if($_SERVER['REQUEST_METHOD'] == "POST") { //Username and Password sent from Form $username = mysqli_real_escape_string($conn, $_POST['user']); $password = mysqli_real_escape_string($conn, $_POST['pass']); $password = md5($password); $sql = "SELECT * FROM userpass WHERE user='$username' AND '$password'"; $query = mysqli_query($conn, $sql); $res=mysqli_num_rows($query); //If result match $username and $password Table row must be 1 row if($res == 1) { header("Location: welcome.php"); } else { echo "Invalid Username or Password"; } } ?> <!doctype html> <html> <head> <meta charset="UTF-8"> <title>SoftAOX | Login</title> </head> <body> <h1>Login</h1> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <label>Username</label> <input type="text" name="user"><br/><br/> <label>Password</label> <input type="password" name="pass"><br/><br/> <input type="submit" name="submit" value="Login"><br/> <p><a href="register.php">New User Register!</a></p> </form> </body> </html> |
welcome.php
1 2 3 4 5 6 7 8 9 10 11 12 |
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>Welcome to SoftAOX</title> </head> <body> <h1 align="center">Welcome to SoftAOX</h1> <p align="center">This is Password Encryption Tutorial</p> <p align="center"><a href="logout.php">Logout</a></p> </body> </html> |
logout.php
1 2 3 4 5 |
<?php session_start(); session_destroy(); header("Location:login.php"); ?> |
Download
Mraj
Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts. If you have any questions let me drop an email with the article name to the following email id: [email protected]