All TalkersCode Topics

Follow TalkersCode On Social Media

Login And Logout Using Session In PHP And MySQLi

Last Updated : Jul 1, 2023

Login And Logout Using Session In PHP And MySQLi

In this tutorial we will show you the solution of login and logout using session in PHP and MySQLi, here we created login form with two input fields for collecting user name, password and when user clicks submit button we validating and verifying whether user is information validate or not.

If those are valid then only we storing user information on session variable and redirected another page there we displaying user name by using session variable and logout option.

When we storing some information’s on session we can access those from any different page in website.

Step By Step Guide On Login And Logout Using Session In PHP And MySQLi :-

Here we defined login form with input fields for collecting user name, password and another one is submit button then when clicks submit button we validating each input fields using empty() method if both fields are empty it throws error.

Otherwise we executes database connection using select query we collecting which row in table data matched with user details and stored to variable $query.

We checking whether row count is not equal to zero or not if it is true we storing collected row name,password on respective variables ‘$dbusername,$password’ then we checking both user and database details are same.

If both are same then only we starting session and storing user name on session variable.

Lastly we navigate to member page otherwise it throws error message.

In member page we displaying user name by using session variable with logout option and without login when user tries to open member’s page we redirecting them to login page.

When user clicks on logout link its loads logout external file there we destroying session and we unsets session variable to none.

<!doctype html>
<html>
<head>
<title>Login</title>
</head>
<body>
<center><h1>LOGIN FORM</h1></center>
<form action="" method="POST">
Username: <input type="text" name="user"><br />
Password: <input type="password" name="pass"><br />
<input type="submit" value="Login" name="submit" />
</form>
<?php
if(isset($_POST["submit"])){
if(!empty($_POST['user']) && !empty($_POST['pass'])) {
    $user=$_POST['user'];
    $pass=$_POST['pass'];
    $con=mysqli_connect('localhost','root','','dbase') or die(mysql_error());
    $query=mysqli_query($con,"SELECT * FROM info WHERE Name='".$user."' AND Password='".$pass."'");
    $numrows=mysqli_num_rows($query);
    if($numrows!=0)
    {
    while($row=mysqli_fetch_assoc($query))
    {
    $dbusername=$row['Name'];
    $dbpassword=$row['Password'];
    }
    if($user == $dbusername && $pass == $dbpassword)
    {
    session_start();
    $_SESSION['sess_user']=$user;
    header("Location: member.php");
    }
    } else {
    echo "Invalid username or password!";
    }
} else {
    echo "All fields are required!";
}
}
?>
</body>
</html>

member.php

<?php
session_start();
if(!isset($_SESSION["sess_user"])){
    header("location:login.php");
} else {
?>
<!doctype html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h2>Welcome, <?=$_SESSION['sess_user'];?>! <a href="logout.php">Logout</a></h2>
</body>
</html>
<?php
}
?>

logout.php

<?php
session_start();
unset($_SESSION['sess_user']);
session_destroy();
header("location:login.php");
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. In html block we defined login form with input fields username, password and submit button and when user filling details and clicks submit using if condition we checks whether submit is set or not by isset() method.
  4. If returns true we checks another condition for validating both input fields are filled or not by user if both are not empty then we executed database connection by mysqli_connect() method.
  5. The mysqli_connect() method will executes database connection for that we need to pass four parameters ‘host name, user name, password, database name’ properly then only we can successfully make connection otherwise it throws error.
  6. Using select query we collecting data from table ‘info’ by condition which row had username, password match with user entered username, password then it is stored to variable $query.
  7. Then we checking whether $query count is more than value ‘0’ if is true then using while loop we fetching table data and stored to respective variables ‘$dbusername,$dbpassword’.
  8. Using another if condition we checks both database and user entered details are same then we starts session and stored current username on session variable so we can access that session value any page in website and redirected to member.php page by header() method.
  9. Otherwise it will throw’s error on webpage. In member.php file we are checking whether any user without login they navigate to this link and access anything we redirecting them to login page. Otherwise welcome message displayed with current user name and logout option because we cant leave our account page as it can anyone access so we provided logout link.
  10. When user clicks logout it loads logout script file, here we destroyed session and also we unsets session variables so it had no more detail about of users.

Conclusion :-

In conclusion we are able to know how to create login form with session and validation using php.

First we need to start our xampp server then we load this program on browser we can see result of login page when user filled input fields and clicks login then we validating whether all input fields filled by user and if condition checks entered user information with database if they correct then we are navigate to member page there using session variable we displayed current user name with welcome message.

I hope this tutorial on login and logout using session in PHP and MySQLi helps you and the steps and method mentioned above are easy to follow and implement.