Hi..hello viewers!.. Welcome to "Parallel Code"

Login logout using session in php tamil

Login logout செய்வதற்கு தனி தனியான சில php file-கள் தேவைபடுகிறது. கீழே கொடுக்கப்பட்டுளவாறு file-களை உங்கள் project folder-க்கு உள்ள create செய்து கொள்ளுங்கள்.

File Creation

  1. dahsboard.php
  2. generate-report.php
  3. login.php
  4. logout.php
  5. manage-functions.php
  6. report.php

Folder Creation

  1. config
  2. css
  3. js

"css" folder, project-க்கு தேவையான அனைத்து style file-களையும் manage செய்ய உதவுகிறது. இதில் mystyle.css என்ற file-ஐ create செய்து கொள்ளவேண்டும். இதற்கான css style கீழே கொடுக்கப்பட்டு உள்ளது அதை copy paste செய்து கொள்ளுங்கள்.

"js" folder project-க்கு தேவையான அனைத்து javascript file-களையும் வைத்துகொள்ள உதவுகிறது. இதில் myscript.js என்ற file-ஐ create செய்து கொள்ளவேண்டும். இதற்கான js script கீழே கொடுக்கப்பட்டு உள்ளது அதை copy paste செய்து கொள்ளுங்கள். jquery-3.6.0.min இந்த file-ஐ இந்த link கிளிக் செய்து download செய்து கொள்ளலாம். Click and download jquery-3.0.6.1.min.js

Note: இந்த project-க்கு தேவையான முழு source code-ஐ இந்த Download Source Code button-ஐ கிளிக் செய்து download செய்து கொள்ளுங்கள். அல்லது கீழே தனி தனியாக கொடுக்கப்பட்டு உள்ள source code-ஐ copy paste செய்து கொள்ளுங்கள்.

Download Source Code

1 config.php

<?php
date_default_timezone_set('Asia/Kolkata');
try {
    $link = mysqli_connect('localhost', 'root', '', 'usrreg');    
} catch (Exception $ex) {
    echo 'DB Connection Error. ' . mysqli_connect_error();
}
?>

2. dahsboard.php

<?php
session_start();
require_once 'config/config.php';
if (isset($_SESSION['user_email']) && isset($_SESSION['user_name'])) {
    ?>
    <html>
        <head>
            <title>Dashboard</title>
            <link href="css/mystyle.css" rel="stylesheet" type="text/css"/>
        </head>
        <body>
            <div class="container">
                <div class="login">                
                    <div class="heading">Dashboard</div>
                    <div class="alink">
                        <?php
                        if ($_SESSION['user_email'] == 'admin@gmail.com') {
                            ?>
                            <a href="report.php">User Report</a> | 
                        <?php }
                            ?>
                            
                        <a href="logout.php">Logout</a>
                    </div>
                    <div class="profile">

                        <div>ID:<span id="userid"><?= $_SESSION['user_id'] ?></span></div>                    
                        <div>Name:<?= $_SESSION['user_name'] ?></div>                    
                        <div>Email: <?= $_SESSION['user_email'] ?></div>
                        <?php
                        $id = $_SESSION['user_id'];
                        $cdate = date('Y-m-d');
                        $query = "SELECT * from report where emp_id=$id and check_in_date='$cdate'";
                        $result = mysqli_query($link, $query);
                        if (mysqli_num_rows($result) == 0) {
                            ?>
                            <div><button class="check-in-btn">Check in </button> <span id="check-in-time" style="font-weight:bold;"></span></div>                             
                            <div><button class="check-out-btn" style="display:none">Check out </button> <span id="check-out-time" style="font-weight:bold;"></span></div>    
                            <?php
                        } else {
                            $row = mysqli_fetch_assoc($result);
                            if ($row['check_in_time'] == '00:00:00') {
                                ?>
                                <div><button class="check-in-btn">Check in </button> <span id="check-in-time" style="font-weight:bold;"></span></div>  
                                <?php
                            } else {
                                ?>
                                <div><span id="check-in-time" style="font-weight:bold;">Started at: <?= $row['check_in_date'] . ' ' . $row['check_in_time'] ?></span></div>
                                <?php
                            }
                            if ($row['check_out_time'] == '00:00:00') {
                                ?>
                                <div><button class="check-out-btn">Check out </button> <span id="check-out-time" style="font-weight:bold;"></span></div>    
                                <?php
                            } else {
                                ?>
                                <div><span id="check-out-time" style="font-weight:bold;">Ended at: <?= $row['check_in_date'] . ' ' . $row['check_out_time'] ?></span></div>
                                <?php
                            }
                        }
                        ?>


                    </div>
                </div>
            </div>
            <script src="js/jquery-3.6.0.min.js" type="text/javascript"></script>
            <script src="js/myscript.js" type="text/javascript"></script>
        </body>
    </html>
    <?php
} else {
    header('location:login.php');
}
?>

3.generate-report.php


<?php

session_start();
require_once 'config/config.php';
extract($_REQUEST);
$filename = $id . '.xls';
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");

function changeDateFormate($str) {
    $ext = explode('/', $str);
    $newstr = $ext[2] . '-' . $ext[1] . '-' . $ext[0];

    return $newstr;
}

$startdate = changeDateFormate($std);
$todate = changeDateFormate($end);
//$startdate = date('Y-m-d', strtotime($std));
//$todate = date("d-m-Y", strtotime($end));

$query = "SELECT * FROM report where emp_id=$id and (check_in_date between '$startdate' and '$todate')";

$result = mysqli_query($link, $query);

if (mysqli_num_rows($result) > 0) {
    $heading = false;
    while ($row = mysqli_fetch_assoc($result)) {
        if (!$heading) {
            echo implode("\t", array_keys($row)) . "\r\n";
            $heading = true;
        }

        $str = implode("\t", $row) . "\r\n";
        echo $str;
    }
    //header('location:dahsboard.php');
}else{
    echo "\t Data not found \r\n";
}
?>

4. login.php

<?php
include 'config.php';
?>
<!doctype html>
<html>
    <head>
        <title>Employee list</title>
        <link href="css/crudstyles.css" rel="stylesheet" type="text/css"/>
    </head>
    <body>
        <div class="container">
            <div class="left">
                <?php require 'menu.php'; ?>
            </div>
            <div class="right">
                <form class="empform" id="empform" action="" enctype="multipart/form-data">
                    <table cellspacing="20">
                        <tr>
                            <th colspan="2"><center>New Employee Form</center></th>
                        </tr>
                        <tr>
                            <td>Emp ID</td>
                            <td><input type="text" name="empid" id="empid" /></td>
                        </tr>
                        <tr>
                            <td>Name</td>
                            <td><input type="text" name="empname" id="empname" /></td>
                        </tr>
                        <tr>
                            <td>Email</td>
                            <td><input type="email" name="empemail" id="empemail" /></td>
                        </tr>
                        <tr>
                            <td>Photo</td>
                            <td><input type="file" name="profilepic" id="profilepic" /></td>
                        </tr>
                        <tr>
                            <td></td>
                            <td><button class="sbtn" name="submit" id="submit" value="create">Submit</button></td>
                        </tr>
                    </table>
                </form>
            </div>
        </div>
        <script src="js/jquery-3.6.0.min.js" type="text/javascript"></script>
        <script src="js/myscript.js" type="text/javascript"></script>

    </body>
</html>

5. logout.php

<?php
include 'config.php';
?>
<!doctype html>
<html>
    <head>
        <title>Employee list</title>
        <link href="css/crudstyles.css" rel="stylesheet" type="text/css"/>
    </head>
    <body>
        <div class="container">
            <div class="left">
                <?php require 'menu.php'; ?>
            </div>
            <div class="right">
                <?php
                $query = "SELECT * from emp";
                $result = mysqli_query($link, $query);
                if (mysqli_num_rows($result) > 0) {
                    ?>
                    <h3>Employee List</h3>
                    <table class="emplist">
                        <thead>
                            <tr>
                                <th>Emp id</th>
                                <th>Name</th>
                                <th>Email</th>
                                <th>Profile</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            while ($row = mysqli_fetch_assoc($result)) {
                                ?>
                                <tr>
                                    <td><?= $row['id'] ?></td>
                                    <td><?= $row['name'] ?></td>
                                    <td><?= $row['email'] ?></td>
                                    <td><img src="<?= $row['profilepic'] ?>" width="100" height="100"></td>
                                    <td>
                                        <a href="edit.php?empid=<?= $row['id'] ?>">Edit</a> | 
                                        <a href="javascript:void(0)" onclick="deleteFunction('<?= $row['id'] ?>',this)">Delete</a>
                                    </td>
                                </tr>

                                <?php
                            }
                            ?>
                        </tbody>
                    </table>

                    <?php
                } else {
                    echo 'Record Not found';
                }
                ?>
            </div>
        </div>

        <script src="js/jquery-3.6.0.min.js" type="text/javascript"></script>
        <script src="js/myscript.js" type="text/javascript"></script>

    </body>
</html>

6. manage-functions.php

<?php
include 'config.php';
extract($_REQUEST);
?>
<!doctype html>
<html>
    <head>
        <title>Employee list</title>
        <link href="css/crudstyles.css" rel="stylesheet" type="text/css"/>
    </head>
    <body>
        <div class="container">
            <div class="left">
                <?php require 'menu.php'; ?>
            </div>
            <div class="right">
                <?php
                $query = "SELECT * from emp where id=$empid";
                $result = mysqli_query($link, $query);
                if (mysqli_num_rows($result) > 0) {
                    ?>
                    <form class="empform" id="updateform" enctype="multipart/form-data">
                        <table cellspacing="20">
                            <tr>
                                <th colspan="2"><center>New Employee Form</center> <span id="update-status"></span></th>
                            </tr>
                            <?php
                            while ($row = mysqli_fetch_assoc($result)) {
                                ?>

                                <tr>
                                    <td>Emp ID</td>
                                    <td><input type="text" name="empid" id="empid"  value="<?= $row['id'] ?>" readonly="" /></td>
                                </tr>
                                <tr>
                                    <td>Name</td>
                                    <td><input type="text" name="empname" id="empname" value="<?= $row['name'] ?>"/></td>
                                </tr>
                                <tr>
                                    <td>Email</td>
                                    <td><input type="email" name="empemail" id="empemail"  value="<?= $row['email'] ?>"/></td>
                                </tr>
                                <tr>
                                    <td>Photo</td>
                                    <td><img src="<?= $row['profilepic'] ?>" alt="Profile Picture" width="200" height="200"/></td>
                                </tr>
                                <tr>
                                    <td>Change(optional)</td>
                                    <td><input type="file" name="profilepic"  id="profilepic" /></td>
                                </tr>
                                <tr>
                                    <td></td>
                                    <td><button class="sbtn" name="submit" id="submit" value="update">Update</button></td>
                                </tr>
                                <?php
                            }
                            ?>
                        </table>
                    </form>
                    <?php
                } else {
                    echo 'Record Not found';
                }
                ?>
            </div>
        </div> 
        <script src="js/jquery-3.6.0.min.js" type="text/javascript"></script>
        <script src="js/myscript.js" type="text/javascript"></script>
    </body>
</html>

7.report.php

<?php

include 'config.php';
extract($_REQUEST);
if (isset($submit) && $submit == 'create') {
    $fname = $_FILES['profilepic']['name'];
    $tr = explode('.', $fname);
    $ext = strtolower(end($tr));
    if ($ext == 'jpg' || $ext == 'png' || $ext == 'jpeg') {

        $dir = 'profilepic';
        $newfname = $empid . '.' . $ext;
        $profilepic_path = $dir . '/' . $newfname;
        move_uploaded_file($_FILES['profilepic']['tmp_name'], $profilepic_path);

        $query = "INSERT INTO emp(id,name,email,profilepic) values('$empid','$empname','$empemail','$profilepic_path')";
        $result = mysqli_query($link, $query);
        if ($result) {
            //header('location:create.php');
            echo 'success';
        } else {
            echo mysqli_error($link);
        }
    } else {
        echo 'You have choosen ' . $ext . ' file. Please choose png or jpg or jpeg file.';
    }
} else if (isset($submit) && $submit == 'update') {

    if ($_FILES['profilepic']['size'] == 0) {
        $query = "UPDATE emp set name='$empname',email='$empemail' where id='$empid'";
        $result = mysqli_query($link, $query);
        if ($result) {
            echo 'success';
            // header('location:retrive.php');
        } else {
            echo mysqli_error($link);
        }
    } else {
        $fname = $_FILES['profilepic']['name'];
        $dd = explode('.', $fname);
        $ext = strtolower(end($dd));

        if ($ext == 'jpg' || $ext == 'png' || $ext == 'jpeg') {
            $dir = 'profilepic';
            $newfname = $empid . '.' . $ext;
            $profilepic_path = $dir . '/' . $newfname;
            move_uploaded_file($_FILES['profilepic']['tmp_name'], $profilepic_path);

            $query = "UPDATE emp set name='$empname',email='$empemail',profilepic='$profilepic_path' where id='$empid'";
            $result = mysqli_query($link, $query);
            if ($result) {
                //header('location:retrive.php');
                echo 'success';
            } else {
                echo mysqli_error($link);
            }
        } else {
            echo 'You have choosen ' . $ext . ' file. Please choose png or jpg or jpeg file.';
        }
    }
} else if (isset($submit) && $submit == 'delete') {
    $query = "SELECT profilepic from emp where id=$empid";
    $result = mysqli_query($link, $query);
    $row = mysqli_fetch_assoc($result);
    $profilepic_path = $row['profilepic'];
    if (file_exists($profilepic_path)) {
        unlink($profilepic_path);
    }
    $dquery = "delete from emp where id=$empid";
    $result = mysqli_query($link, $dquery);
    if ($result) {
        ///header('location:retrive.php');
        echo 'success';
    }
}

8. mystyle.css


.container{
    width: 500px;
    background-color: orange;
    padding: 20px;
    margin: auto;
}
.heading{
    font-size: 30px;
    text-align: center;
}
.login{
    width: 400px;
    margin: auto;
}
.login input{
    padding: 10px;
    width: 300px;
}
.login button:hover{
    background-color: purple;
}
.login button{
    padding: 10px 20px;
    font-weight: bold;
    font-size: 20px;
    background-color: black;
    border: 1px solid black;
    color: #fff;
    cursor: pointer;
}
.login div{
    margin-bottom: 20px;
}
.alink{
    font-size: 20px;
    font-weight: bold;
    text-align: right;
}
.profile{
    font-size: 20px;
}

.check-in-btn,.check-out-btn{
    padding: 10px 20px;
    font-weight: bold;
    font-size: 20px;
    background-color: black;
    border: 1px solid black;
    color: #fff;
    cursor: pointer;
}
.check-in-btn:hover,.check-out-btn:hover{
    background-color:purple;
}

.report-table,.report-table td{
    border:1px solid black;
    border-collapse: collapse;
    padding: 10px;
}

9. myscript.js


function checkUserExist() {
    var useremail = $('#useremail').val();
    var userpassword = $('#userpassword').val();
    var error = 0;
    $.ajax({
        type: 'post',
        url: 'manage-functions.php',
        data: {
            email: useremail,
            password: userpassword,
            login_check: 'yes'
        },
        async: false,
        success: function (response) {
            var info = $.parseJSON(response);
            if (info.status == 'faild') {
                $("#status").text(info.message);
                error = 1;
            } else {
                $("#status").text(info.message);
            }
        }

    });
    if (error == 1) {
        return false;
    } else {
        return true;
    }
}

$(document).ready(function () {
    $(".check-in-btn").on('click', function () {
        var con = confirm('Are you sure to check in?');
        if (con) {
            var userid = $('#userid').text();
            $.ajax({
                type: 'post',
                url: 'manage-functions.php',
                data: {
                    userid: userid,
                    check_in: 'yes'
                },
                async: false,
                success: function (response) {
                    var info = $.parseJSON(response);
                    if (info.status == 'success') {
                        $(".check-in-btn").remove();
                        $(".check-out-btn").css({'display': 'block'});
                        $('#check-in-time').text("Started at: " + info.checkintime);
                    } else {
                        $('#check-in-time').text(info.message);
                    }
                }
            });
        }

    });
    $(".check-out-btn").on('click', function () {
        var con = confirm('Are you sure to check out?');
        if (con) {
            var userid = $('#userid').text();
            $.ajax({
                type: 'post',
                url: 'manage-functions.php',
                data: {
                    userid: userid,
                    check_out: 'yes'
                },
                async: false,
                success: function (response) {
                    var info = $.parseJSON(response);
                    if (info.status == 'success') {
                        $(".check-out-btn").remove();
                        $('#check-out-time').text("Ended at: " + info.checkouttime);
                    } else {
                        $('#check-out-time').text(info.message);
                    }
                }
            });
        }

    });
    
    
    $('.greport').on('click',function(){
       var fromdate=$("#fromdate").val();
       var todate=$("#todate").val();
       var empid=$(this).closest('tr').find('td:nth-child(1)').text();
       if(fromdate=='' || todate==''){
           alert('Please select all the dates');
           return false;
       }else{
         window.location='generate-report.php?std='+fromdate+'&end='+todate+'&id='+empid+'';  
       }
       
       
    });
    
});


Note: இந்த page-ல் உள்ள code உங்களுக்கு சரியாக புரியாத பட்சத்தில் இதற்க்கான முழு "Youtube" வீடியோ link இங்கு கொடுக்க பட்டுள்ளது. அதை கிளிக் செய்து நீங்கள் இன்னும் தெளிவாக புரிந்து கொள்ளலாம்.

Click and see the video

Comments

yyhnyh 5th December,2023 12:00 PM
trjhty
mohan 28th January,2023 11:59 AM
super bro