close
Itxperts

How to Integrate a Biometric Attendance Device with Your Own Server – PHP Demo

In today’s workplace, attendance tracking is a fundamental task, and biometric attendance devices have made it simpler and more secure. If you’re looking to integrate a biometric device with your own server, you’ve come to the right place. In this post, we’ll explore how you can set up a biometric attendance system and store attendance records directly in your server using PHP.


Prerequisites

To get started, you’ll need:

  1. A biometric attendance device with network connectivity (such as ZKTeco).
  2. A web server with PHP and MySQL.
  3. Basic knowledge of PHP programming.
  4. An understanding of REST APIs (if the device supports RESTful communication).

Step 1: Set Up the Biometric Device

Most biometric attendance devices are equipped with software to manage attendance records. However, if you want to handle this in your own server, you need to ensure:

  1. Network Setup: Connect your device to the same network as your server.
  2. Device Configuration: Go to the device’s settings, set up the IP address, and configure the communication protocol (usually HTTP or TCP/IP).
  3. Device APIs: If the device provides an API (like some ZKTeco devices), you can directly pull attendance data through HTTP requests. If not, look into SDKs provided by the device manufacturer, which may support custom integrations.

Step 2: Create the MySQL Database for Storing Attendance Records

Create a database and a table to store attendance logs. Let’s assume we’re using MySQL.

CREATE DATABASE attendance_db;
USE attendance_db;

CREATE TABLE attendance_log (
    id INT AUTO_INCREMENT PRIMARY KEY,
    employee_id VARCHAR(10),
    punch_time DATETIME,
    status ENUM('IN', 'OUT'),
    device_id VARCHAR(10)
);

This table includes:

  • employee_id: A unique identifier for each employee.
  • punch_time: The date and time of the biometric scan.
  • status: Tracks if the log is an “IN” or “OUT” record.
  • device_id: Identifies the device that recorded the log (useful if you have multiple devices).

Step 3: Write PHP Code to Capture Data from the Device

Depending on your device’s integration capability, you can either:

  1. Receive data through HTTP requests if the device supports webhooks (device pushes data to your server).
  2. Poll the device using the device’s API to retrieve records.

Here’s a simple example of PHP code that captures attendance data from a device with webhook support.

attendance_process.php

This script will accept attendance data from the device.

<?php
// attendance_process.php

// Database connection
$host = 'localhost';
$db = 'attendance_db';
$user = 'root';
$pass = '';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Database connection failed: " . $e->getMessage());
}

// Check if POST data is received
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $employee_id = $_POST['employee_id'] ?? '';
    $punch_time = $_POST['punch_time'] ?? '';
    $status = $_POST['status'] ?? '';
    $device_id = $_POST['device_id'] ?? '';

    if ($employee_id && $punch_time && $status && $device_id) {
        // Prepare SQL statement
        $stmt = $pdo->prepare("INSERT INTO attendance_log (employee_id, punch_time, status, device_id) VALUES (:employee_id, :punch_time, :status, :device_id)");
        $stmt->execute([
            ':employee_id' => $employee_id,
            ':punch_time' => $punch_time,
            ':status' => $status,
            ':device_id' => $device_id
        ]);

        echo json_encode(['status' => 'success', 'message' => 'Attendance recorded.']);
    } else {
        echo json_encode(['status' => 'error', 'message' => 'Incomplete data.']);
    }
} else {
    echo json_encode(['status' => 'error', 'message' => 'Invalid request method.']);
}
?>

This PHP script:

  • Connects to the attendance_db database.
  • Receives employee_id, punch_time, status, and device_id via POST.
  • Inserts the data into the attendance_log table.

Note: Make sure to configure the device to send POST data to this endpoint (attendance_process.php).


Step 4: Configure the Device to Send Data to Your Server

Depending on your device, set the webhook or push URL to point to http://<your_server_ip>/attendance_process.php. This tells the device to push data to your server every time an attendance punch is recorded.


Step 5: Test the Integration

To ensure your setup is working correctly:

  1. Make an attendance punch on the biometric device.
  2. Check the attendance_log table in your database to confirm the record was inserted.

You can also send test data using a tool like Postman:

  • URL: http://<your_server_ip>/attendance_process.php
  • Method: POST
  • Body:
    • employee_id: E123
    • punch_time: 2024-10-31 08:00:00
    • status: IN
    • device_id: D001

If everything is set up correctly, the data should appear in the attendance_log table.


Step 6: Display Attendance Data (Optional)

You may want to create a simple dashboard to view attendance records. Here’s a quick example using PHP.

view_attendance.php

<?php
// view_attendance.php

try {
    $pdo = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Database connection failed: " . $e->getMessage());
}

$stmt = $pdo->query("SELECT * FROM attendance_log ORDER BY punch_time DESC");
$logs = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html>
<head>
    <title>Attendance Records</title>
</head>
<body>
    <h2>Attendance Records</h2>
    <table border="1">
        <tr>
            <th>Employee ID</th>
            <th>Punch Time</th>
            <th>Status</th>
            <th>Device ID</th>
        </tr>
        <?php foreach ($logs as $log): ?>
        <tr>
            <td><?= htmlspecialchars($log['employee_id']) ?></td>
            <td><?= htmlspecialchars($log['punch_time']) ?></td>
            <td><?= htmlspecialchars($log['status']) ?></td>
            <td><?= htmlspecialchars($log['device_id']) ?></td>
        </tr>
        <?php endforeach; ?>
    </table>
</body>
</html>

Conclusion

Integrating a biometric attendance device with your own server can provide you with more control over employee data and customization options. Using PHP and MySQL, you can manage attendance logs effectively, either directly receiving data from the device or by polling through APIs. Follow these steps, and you’ll have a functional biometric attendance tracking system up and running on your server.

Have fun coding, and enjoy the power of biometric integration!