php admin and user login with session - Web Development and Web Design Codes

Latest

Friday, May 26, 2017

php admin and user login with session

PHP MySQL Admin Panel and User Panel Login With SESSION


admin and user login in php mysql
admin and user login in php mysql

Hi Guys Today we will Learn how To create a Admin and User Panel Login System with SESSION in Single page.
You do not need to create Separate admin and user page.
When a user Hit login Button After Fill the Username and Password They Will Automatically Redirect Their Area Accroding To Their Role.
Ex: Admin will Redirect admin page and user will Redirect user page. I provide this codes Only for Beginners .it will help User To Understand Role Based Login System with SESSION.
learn more about PHP SESSION

Database and Files Creation:

Database Structure:
Database Name: codenair
Table Name: user
Use the below SQL Commands to Create Table Named 'user'
CREATE TABLE IF NOT EXISTS `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  `role` varchar(100) NOT NULL,
   PRIMARY KEY(id)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;


Dumping data for table `user`
INSERT INTO `user` (`id`, `username`, `password`, `role`) VALUES
(NULL, 'XYZ', '1234', 'user'),
(NULL, 'ABCD', '1122', 'admin');

Files Structure:
Create 7 files in same folder named:
1. connect.php
2. login.php
3. session.php
4. admin.php
5. user.php
6. logout.php
7. style.css
Now Copy the Below codes for For Certain File
admin and user login in php mysql
admin and user login in php mysql

connect.php:
<?php
// Creating connection test is our database name
$conn = new mysqli('localhost', 'root', '','codenair');
// Checking connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

login.php:

<?php
session_start();
if(!empty($_SESSION['user'])){
 include('session.php');
 if($role_session=='admin'){
    header('location:admin.php'); 
     }
  if($role_session=='user'){
    header('location:user.php'); 
     }
}
?>
<link rel="stylesheet" href="style.css" type="text/css"/>
<h3>PHP MySQL SESSION Based User and Admin Pannel Login System</h3>
<form method="POST" action="">
   <table>
      <tr>
      <td><span>UserName:</span></td>
   <td><input type="text" name="username"/></td>
   </tr>
   <tr>
      <td><span>Password:</span></td>
   <td><input type="password" name="password"/></td>
   </tr>
   <tr>
      <td></td>
   <td><input type="submit" name="submit" value="LogIn"/></td>
   </tr>
   </table>
</form>

<?php
include("connect.php");
if(isset($_POST['submit'])){
 $name=$_POST['username'];
 $pass=$_POST['password'];
  //RUN Sql query to Get result
 $result=mysqli_query($conn,"SELECT*FROM user WHERE username='$name' AND password='$pass'");
    $count=$result->num_rows;
 //for valid user login count must be 1
 if($count==1){
    $row=$result->fetch_assoc();
    $role=$row['role'];
 //Setting SESSION
  $_SESSION['user']=array(
   'username'=>$row['username'],
   'password'=>$row['password'],
   'role'=>$row['role']
   );
   switch($role){
    case'user':
    header('location:user.php');
       break;
       case'admin':
       header('location:admin.php');
       break;    
 }
}else{
 echo "No User Found";
}
}
 ?>

session.php
Now we will access logged user session using session.php page.
<?php
include('connect.php');
session_start();
//Get username from SESSION
$user_check=$_SESSION['user']['username'];
//Fetch Data by User
$result=mysqli_query($conn,"SELECT*FROM user WHERE username='$user_check'");
$row=$result->fetch_assoc();
$login_session=$row['username'];
$role_session=$row['role'];
?>

admin.php
<?php
include('session.php');
if($role_session=='user'){
 header('location:user.php');
}
?>
<p>Your Name: <?php echo $login_session;?>|| Your Role: <?php echo $role_session;?></p>
<a href="logout.php">Logout Now</a>

user.php
<?php
include('session.php');
//Redirecting Admin to admin page
  if($role_session=='admin'){
 header('location:admin.php'); 
}
?>
<p>Your Name: <?php echo $login_session;?>|| Your Role: <?php echo $role_session;?></p>
<a href="logout.php">Logout Now</a>

logout.php
<?php
session_start();
session_destroy();
 
 header('location:login.php');
?>

style.css
input[type=text],[type=password]{
    width: 250px;
    padding: 8px;
    margin: 4px 0;
    box-sizing: border-box;
    border: 1px solid #808080;
    -webkit-transition: 0.5s;
    transition: 0.5s;
   font-size:20px;
}
span{
 font-size:22px;
 color:green;
        font-weight:bold;
}
input[type=text]:focus {
    border: 1px solid green;
}
 
input[type=submit]{
 background:black;
 color:white;
 padding:6px 8px;
 font-weight:bold;
 cursor:pointer;
 border-radius:5px;
}

ok You have Successfully Created PHP MySQL SESSION Based User and Admin Panel Login System.If you Have face any Problems Don't Forget to Comments.Thank you.

2 comments:

  1. At step one (creating the table), I am getting this error:

    "Incorrect table definition; there can be only one auto column and it must be defined as a key"

    ReplyDelete
    Replies
    1. Sorry For That...codes updated ..try again.. Thank you..

      Delete

Thank You for Your Comment

Note: Only a member of this blog may post a comment.