Sensation Energy

Tutorials


How to create Login with JSON nad PHP

Json Login

Introduction

In this tutorial I will show you how to make an application using php and json. In this tutorial, we will not use the Miskl database, but we will extract the data from json.

Json Login is MIT Licence

The json login installation consists of the following steps:

  • Unzip the zip file and insert "JSON LOGIN" it on the file server (Wamp. Xamp, Lamp)
  • Run via localhost - Example: http://localhost/json_login/index.php
  • Login

    Login function serves as everywhere for logging. This means that the function checks that there is a user in the database and that his entered username and password match the username and password from the database.

    Example for login fuction

    
    < ?php
    //The session starts here
    session_start();
    //This means if $_POST['prijava'] which is also taken name="prijava" in the button
    if (isset($_POST['prijava'])) {
    //Checks if it is POST user = user and password = password
    //Ecample admin = admin
    $user = $_POST['user'];
    $password = $_POST['password'];
    //Function get User from user.json
    function getUser(){
    $uzmi = file_get_contents('user.json');
    $u = json_decode($uzmi,true);
    return $u;
    }
    $u  = getUser();
    foreach($u  as $us){
    //When you have taken the user from the previous functions here it is checked whether the user who filled in the login formula exists basically,
    //If the name then returns to the login page index.php, if there is its username and password then his role is verified.
    //You can see this in the code below.
    if($user!=$_SESSION['user']){
    header('location:index.php');
    }
    //Exampole role[admin] = role[admin]
    //if the user has a role admin then go to the admin.php page, if the user has a role user then go to the user.php page
    //A user who has a role user cannot go to admin.php, and if he has a role admin he can go to admin.php and user.php
    //Because user.php belongs to the user who logged in
    $_SESSION['user']=$us['user'];
    $role=$us['role'];
    if (($user == $us['user']) AND ($password == $us['password']) AND ($role == $us['role'])){
    if($role=='admin'){
    header('location:admin.php');
    }else if($role=='user'){
    header('Location:user.php?user='.base64_encode($_SESSION['user']));
    }}}} ?>
                      
                        

    Form

    Form is used to allow the user who wants to log in to enter their username and password

    
    <!-- This form has an action that takes place in login.php -->
    <form action="login.php" method="post">
    <div class="form-group">
    <!-- The username is insert here -->
    <!-- name="user" is important because the user of the entered user is checked through the name -->
    
    <label for="username">Username:</label>
    <input type="text" name="user"  class="form-input" />
    </div>
    <div class="form-group">
    <!-- The password is insert here  -->
    <!-- name="password" is important because the password of the entered user is checked through the name -->
    <label for="password">Password:</label>
    <input type="password" name="password" class="form-input" />
    </div>
    <div class="btn-group">
    <!-- As we said before name = "prijava" is important because in if the user checks through the post -->
    <button type="submit" name="prijava" class="btn-3" >POST</button>
    </div>
    </form>
    </div>
    
      

    Admin page

    Admin page is made here as an example. In previous tutorials, we explained what admin page or admin dashbaord (BackEnd)

    
    < ?php
    //Just like in login.php it takes the user with the admin role and allows access to admin.php
    session_start();
    function uzmiUser(){
    $get = file_get_contents('user.json');
    $u = json_decode($get,true);
    return $u ;
    }
    $u = uzmiUser();
    foreach($u  as $us){
    if (isset($_SESSION['user']) && $us['role'] == 'admin'){
    if($_SESSION['user'] == $us['user']){
    //Example $_SESSION['admin'] and  us['role'] == 'admin'
    ?>
    <h2>ADMIN</h2>
    <br>
    <!-- it is shown here that the user username -->
    <a href="user.php?user=< ?php echo base64_encode($_SESSION['user']) ?>"  class="link">User: </a>
    <br>
    <!-- it is shown here which role the logged-in user has -->
    Role: < ?php echo  $us['role']; ?>
    <br>
    <a href="logout.php"><button class="btn-3">Logout</button></a>
    < ?php
    //It is assumed that the user is logged in, if not then he returns it to the login page indes.php
    } else {
    echo '<meta http-equiv="refresh" content="0;URL=index.php" />';
    }
    } }
    ?>
                  
                  

    User page

    An example of a page user is given here. which is valid only for logged in users and it does not matter whether they have the role of admin or user.

    
    < ?php
    //As in the previous examples, the accuracy of the login data is checked first, and if everything is OK,
    //Then the data related to the logged in user is displayed
    session_start();
    $_SESSION['user'] = base64_decode($_GET['user']);
    function uzmiUser(){
    $get = file_get_contents('user.json');
    $u = json_decode($get,true);
    return $u ;
    }
    $u = uzmiUser();
    $user_id='0';
    foreach($u as $us){
    if($_SESSION['user'] == $us['user']){
    ?>
    <!-- The following logged-in user data is displayed here: -->
    First name: < ?php echo $us['first_name']; ?>
    <br>
    Last name: < ?php echo $us['last_name']; ?>
    <br>
    Email: < ?php echo $us['email']; ?>
    <br>
    User: < ?php echo $us['user']; ?>
    <br>
    Role:
    < ?php echo  $us['role'];
    ?>
    <br>
    <!-- This button indicates if the logged in user wants to log out -->
    <a href="logout.php"><button class="btn-3">Logout</button></a>
    <br>
    < ?php
    } }
    ?>
    
    

    Logout

    Logout is used to log off the logged-in user and to terminate his session. If I had to log in again after logging out, I would have to go to the login page indes.php, and log in again.

    
    < ?php
    session_start();
    unset($_SESSION["user"]);
    //Functionality serves for unset $_SESSION["user"] and session destroy
    if (!isset($_SESSION)) { session_start(); }
    $_SESSION = array();
    session_destroy();
    header("Location: index.php");
    //The login user logs out automatically returns to the login page indes.php
    ?>
    
    

    Download on github



    Sum download: 1

    Content


    Documentation



    Answer the following question:

    You must be logged in


    Advertising