All TalkersCode Topics

Follow TalkersCode On Social Media

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

PHP Login Different User Levels

Last Updated : Mar 11, 2024

PHP Login Different User Levels

In this tutorial we will show you the solution of PHP login different user levels, here we created login form with two input fields for collecting user name, password and when user clicks on login it loads action attribute of login_action external script file.

In this file we executed database connection and we collecting user entered details then passed on select query for checks valid user.

If they valid user then we checks userlevel from table ‘userlevels’ and redirect to user page.

Step By Step Guide On PHP Login Different User Levels :-

Here we defined login form with input fields for collecting user name, password and another one is login button then when clicks on login button form action attribute loads external ‘login_action.php’ php file.

In php file we executed database ‘dbase’ connection and we collected ‘username, password’ from user using post method then stored on respective variables ‘$username,$password’.

Using select query we retrieving table row from database and if condition checks row count whether greater than ‘0’ or not.

Then we checking current users userlevel from database whether its ‘1,2,3 or 4’ for redirect them to user html page otherwise access denied. In user html page we displayed welcome message on webpage.

<!DOCTYPE html>
<html>
<head>
<title>User Levels</title>
</head>
<body>
<h5>Sign In</h5>
<div>
<form action="login_action.php" method="post">
<input type="text" name="username" placeholder="username">
<input type="password" name="password" placeholder="password">
<button type="submit">Login</button>
</div>
</form>
</body>
</html>
Login_action.php
<?php
$dbCon = mysqli_connect('localhost','root','','dbase');
if(!$dbCon){
    die('Could not Connect MySql Server:' .mysql_error());
}
$username=$_POST['username'];
$password=$_POST['password'];
$sql="SELECT * FROM userlevels WHERE uname='$username' and pass='$password'";
$result=mysqli_query($dbCon,$sql);
if(mysqli_num_rows($result) > 0){
$rows = mysqli_fetch_assoc($result);
}
if ($rows['userlevel'] == '1' || $rows['userlevel'] == '2' || $rows['userlevel'] == '3' || $rows['userlevel'] == '4') {
 header('location: user.html');
}
else
{
echo "<script>alert('Access Denied!');
window.location='userlvls.php';
</script>";
}
?>
User.html
<!DOCTYPE html>
<html>
    <head>
        <title>user page</title>
    </head>
    <body>
        <h3>Welcome to user webpage!!</h3>
    </body>
</html>
  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. Here we need to create database table ‘userlevels’ with column ‘userlevel’. This column type is varchar and we need to insert data on table users with ‘userlevel’ numeric values like ‘1,2,3,4’.
  4. In html block we defined signin form with input fields of username, password and login button when user filling details then clicks login button. Action attribute loads external login_action php file.
  5. Here we defined and executed database ‘dbase’ connection then its referred by variable ‘$dbCon’. Using method ‘post’ we collected users form details of ‘username, password’ and stored to respective variables ‘$username,$password’.
  6. We defined select query with condition to checks in table ‘userlevels’ which row match with user given details then query stored to variable ‘$sql’.Using if condition we checks whether row count greater than ‘0’ or not.
  7. If its greater then we checking that users userlevel from database and redirected to ‘user.html’ page otherwise it throws error message and redirected to login form. In user html page we just displayed welcome message for valid users.

Conclusion :-

In conclusion we are able to know how to create login form with user levels 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 validates whether user entered details present in database or not.

If they match then we are checking userlevel and navigate to user page there we displaying some welcome message.

I hope this tutorial on PHP login different user levels helps you and the steps and method mentioned above are easy to follow and implement.