php-login with cookie and Authentication Using Session

in #utopian-io3 years ago (edited)

What Will I Learn?

In This Tutorial, We are learning the Login With Cookie and Authentication Using Session.

  • We Will learn from this tutorial How to Login with Cookie
  • Also Learn from this Tutorial, Login Authentication Using Session.

Requirements

There are Some requirements for learning this tutorial in the given below :

  • Basic Knowledge of Cookie
  • Basic Knowledge of Session
  • Establish Database Connection

Difficulty

There is no difficulty in this tutorial. It is Primary Level and easy to learn.

  • Basic

Tutorial Contents

In This Tutorial, We are mainly focused the php-login with Cookie and Login-Authentication Using Session.
p.PNG

php-Login With Cookie : First Of all We discuss What is Cookie .

Cookie : Cookie is Used to Identify a User. Cookie Saves in Browser. In php , User can Create and Retrieve the Cookies Value.

Let's we discuss php-Login using cookie - First of all , we create a Login platform using php , Login platform have Id and password for Logged In the Home page .

Let's see an example of the Login page Code -

<?php
    if(isset($_COOKIE['id'])){
        session_start();
        $_SESSION['id']=$_COOKIE['id'];
        header("location: home.php");
    }
    if($_SERVER['REQUEST_METHOD']=="POST"){
        $id = $_POST['id'];
        $password = $_POST['password'];
        $remember = $_POST['remember'];
        
        $conn = mysqli_connect("127.0.0.1", "root", "", "user_db", 3306);
        $result = mysqli_query($conn, "SELECT password FROM person WHERE id=$id");
        $row = mysqli_fetch_assoc($result);
        if($row['password']==$password){
            if(isset($remember)){
                setcookie("id", $id, time()+60);
            }
            session_start();
            $_SESSION['id']=$id;
            header("location: home.php");
        }
        else{
            echo "Invalid User ID or Password";
        }
    }
?>
<form method="post">
    Login<hr/>
    ID:<br/><input name="id"/>
    <br/>Password:<br/><input type="password" name="password"/>
    <br/><input type="checkbox" name="remember"/>Remember ME
    <hr/><input type="submit"/>
</form>

After running the program, we see the following Output-

10.PNG

Database Structure- Here is a database of person that's include person's id and password.
11.PNG

After Successfully Login , We go to the Home page .
Let's see an example of home.php code-

<?php
    session_start();
    if(isset($_SESSION['id'])==false){
        header("location: login.php");
    }
?>
<a href="logout.php">logout</a>
<?php   
    var_dump($GLOBALS);
?>

After running the home.php code we will see the following Output-
12.PNG

After Visiting the home page , We can easily Click Logout button and returning to the Login page -
Let's see an example of Logout .php code-

<?php
    session_start();
    //unset($_SESSION['id']);
    session_destroy();
    header("location: login.php");
?>

After clicking the Logout button , we see the following output-

13.PNG

Here, is the rest of the part of this tutorial discuss in the given below :

Login Authentication Using Session : Let's we discuss about the Session.

Session : In Theoritically, Between Session Starting time and Ending Time , It's performed some action. It's Client Specific.

Here is an example of Session Login Code -we create a Login platform using php , Login platform have Id and password for Logged In the Home page .

<?php
    if($_SERVER['REQUEST_METHOD']=="POST"){
        $id = $_POST['id'];
        $password = $_POST['password'];
        
        $conn = mysqli_connect("127.0.0.1", "root", "", "user_db", 3306);
        $result = mysqli_query($conn, "SELECT password FROM person WHERE id=$id");
        $row = mysqli_fetch_assoc($result);
        if($row['password']==$password){
            session_start();
            $_SESSION['id']=$id;
            header("location: home.php");
        }
        else{
            echo "Invalid User ID or Password";
        }
    }
?>
<form method="post">
    Login<hr/>
    ID:<br/><input name="id"/>
    <br/>Password:<br/><input type="password" name="password"/>
    <hr/><input type="submit"/>
</form>

After running the following program , We will see the output -
4.PNG

After Successfully Login , We go to the Home page .
Let's see an example of home.php code-

<?php
    session_start();
    if(isset($_SESSION['id'])==false){
        header("location: login.php");
    }
?>
<a href="logout.php">logout</a>
<?php   
    var_dump($GLOBALS);
?>

After running the program, We see the following output-
5.PNG

Here , We see Logout.php code- After checking the home page we logout the home page and returning to the Login page-

<?php
    session_start();
    //unset($_SESSION['id']);
    session_destroy();
    header("location: login.php");
?>

After running the program , We will see the following Output-
6.PNG

Curriculum

Here is an previous tutorial link-



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Coin Marketplace

STEEM 1.23
TRX 0.14
JST 0.146
BTC 63464.78
ETH 2270.73
BNB 561.25
SBD 8.98