User Registration Login & Logout with PHP and MySQLi using Session

Hi, friends in this tutorial I show you how to create user registration, login & logout with PHP and MySQLi using session. User registration/login system is one of the important parts of all websites and the knowledge to build them is must have a skill for any web developer. In this post, let us see how to create a complete login and registration system using PHP and MySQL database. This tutorial is fairly easy to understand, it covers as much as needed to build advanced registration/login system. For this, I used data validation to keep PHP sessions for validated access.
User Registration Login & Logout with PHP and MySQLi using Session – (Part- I)
User Registration Login & Logout with PHP and MySQLi using Session – (Part- II)
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<!doctype html> <html> <head> <title>User Registration</title> </head> <body> <h1>User Registration</h1> <form action="" 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" value="Register" name="submit"><br/><br/> <!-- Login Link --> <a href="login.php">Login</a> </form> <?php if(isset($_POST["submit"])){ if(!empty($_POST['user']) && !empty($_POST['pass'])){ $user = $_POST['user']; $pass = $_POST['pass']; $conn = new mysqli('localhost', 'root', '') or die (mysqli_error()); // DB Connection $db = mysqli_select_db($conn, 'test') or die("DB Error"); // Select DB from database //Selecting Database $query = mysqli_query($conn, "SELECT * FROM userpass WHERE user='".$user."'"); $numrows = mysqli_num_rows($query); if($numrows == 0) { //Insert to Mysqli Query $sql = "INSERT INTO userpass(user,pass) VALUES('$user','$pass')"; $result = mysqli_query($conn, $sql); //Result Message if($result){ echo "Your Account Created Successfully"; } else { echo "Failure!"; } } else { echo "That Username already exists! Please try again."; } } else { ?> <!--Javascript Alert --> <script>alert('Required all felds');</script> <?php } } ?> </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 43 44 45 46 47 48 49 50 51 52 53 54 |
<!doctype html> <html> <head> <title>Login</title> </head> <body> <h1>Login</h1> <form action="" method="post"> <label>Username:</label><input type="text" name="user"><br/> <label>Password:</label><input type="password" name="pass"><br/> <input type="submit" value="Login" name="submit"><br/> <!--New user Register Link --> <p><a href="register.php">New User Registeration!</a></p> </form> <?php if(isset($_POST["submit"])){ if(!empty($_POST['user']) && !empty($_POST['pass'])){ $user = $_POST['user']; $pass = $_POST['pass']; //DB Connection $conn = new mysqli('localhost', 'root', '') or die(mysqli_error()); //Select DB From database $db = mysqli_select_db($conn, 'test') or die("databse error"); //Selecting database $query = mysqli_query($conn, "SELECT * FROM userpass WHERE user='".$user."' AND pass='".$pass."'"); $numrows = mysqli_num_rows($query); if($numrows !=0) { while($row = mysqli_fetch_assoc($query)) { $dbusername=$row['user']; $dbpassword=$row['pass']; } if($user == $dbusername && $pass == $dbpassword) { session_start(); $_SESSION['sess_user']=$user; //Redirect Browser header("Location:welcome.php"); } } else { echo "Invalid Username or Password!"; } } else { echo "Required All fields!"; } } ?> </body> </html> |
welcome.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php session_start(); if(!isset($_SESSION["sess_user"])){ header("Location: login.php"); } else { ?> <!doctype html> <html> <head> <meta charset="UTF-8"> <title>Welcome to SoftAOX</title> </head> <h1>Welcome to SoftAOX</h1> <p>This is Login Page</p> <?=$_SESSION['sess_user'];?>!<a href="logout.php">Logout</a> <body> </body> </html> <?php } ?> |
logout.php
1 2 3 4 5 6 |
<?php session_start(); unset($_SESSION['sess_user']); 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]