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

<!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

<!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

<?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

<?php
session_start();
unset($_SESSION['sess_user']);
session_destroy();
header("Location: login.php");
?>

Was this article helpful?

Yes No
×

How can we improve it?

×

We appreciate your helpul feedback!

Your answer will be used to improve our content. The more feedback you give us, the better our pages can be.

Follow us on social media:

Facebook Pinterest
Share via:

6 Comments

  • iid ismaciil muxumed

    (April 26, 2017 - 5:06 am)

    Thanks very much your help thanks alot realy is the best codes in you-tube ,you are generosity .

  • you help me so much! thanks to you. ive subscribe your channel in youtube. we hope you post more tuts about displaying data in php..

  • The register works but when I try to login it just refreshes the page and does not redirect…

  • Thank you for sharing.

    It worked perfectly 🙂

  • I am getting a database error. Please help.

    <?php
    if(isset($_POST["submit"])){
    if(!empty($_POST['user']) && !empty($_POST['pass'])){
    $user = $_POST['user'];
    $pass = $_POST['pass'];
    //DB Connection

    $mysqli = new mysqli('localhost', 'thanos', '\$piderman2') or die(mysqli_error());

    //Select DB From database
    $db = mysqli_select_db($mysqli, 'SBio') or die("database error");
    //Selecting database
    $query = mysqli_query($mysqli, "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:index.php");

Leave a Reply

Your email address will not be published. Required fields are marked *