All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Login Form In PHP With Session And Validation

Last Updated : Mar 11, 2024

Login Form In PHP With Session And Validation

In this tutorial we will show you the solution of login form in PHP with session and validation, 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 because when we storing some information on session we can access those from any different page in website.

Step By Step Guide On Login Form In PHP With Session And Validation :-

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 and without login when user tries to open member’s page we redirecting them to login page.

<!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'];?>!</h2>
</body>
</html>
<?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 throws error on webpage. In member.php file we checks 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.

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 form in PHP with session and validation helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪