IMEI Tracker code in php

Below is a simple example code to build an IMEI Tracker in PHP. This code assumes you’re collecting IMEI numbers and their corresponding device locations in a database.

Database Schema

Create a table device_locations:

CREATE TABLE device_locations (
    id INT AUTO_INCREMENT PRIMARY KEY,
    imei VARCHAR(20) NOT NULL,
    latitude DECIMAL(9,6) NOT NULL,
    longitude DECIMAL(9,6) NOT NULL,
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

PHP Script for Inserting Location

This endpoint saves location data based on the IMEI number:

<?php
// Database connection
$host = "localhost";
$dbname = "tracker_db";
$username = "root";
$password = "";

try {
    $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Connection failed: " . $e->getMessage());
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $imei = $_POST['imei'];
    $latitude = $_POST['latitude'];
    $longitude = $_POST['longitude'];

    if (!empty($imei) && is_numeric($latitude) && is_numeric($longitude)) {
        $stmt = $conn->prepare("INSERT INTO device_locations (imei, latitude, longitude) VALUES (:imei, :latitude, :longitude)");
        $stmt->bindParam(':imei', $imei);
        $stmt->bindParam(':latitude', $latitude);
        $stmt->bindParam(':longitude', $longitude);

        if ($stmt->execute()) {
            echo json_encode(["status" => "success", "message" => "Location saved."]);
        } else {
            echo json_encode(["status" => "error", "message" => "Failed to save location."]);
        }
    } else {
        echo json_encode(["status" => "error", "message" => "Invalid input."]);
    }
}
?>

PHP Script for Fetching Location

This endpoint retrieves the latest location for a given IMEI:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    $imei = $_GET['imei'];

    if (!empty($imei)) {
        $stmt = $conn->prepare("SELECT * FROM device_locations WHERE imei = :imei ORDER BY timestamp DESC LIMIT 1");
        $stmt->bindParam(':imei', $imei);
        $stmt->execute();

        $location = $stmt->fetch(PDO::FETCH_ASSOC);
        if ($location) {
            echo json_encode(["status" => "success", "location" => $location]);
        } else {
            echo json_encode(["status" => "error", "message" => "No location found."]);
        }
    } else {
        echo json_encode(["status" => "error", "message" => "IMEI is required."]);
    }
}
?>

Sample Usage

Save Location (POST Request)

curl -X POST -d "imei=123456789012345&latitude=40.7128&longitude=-74.0060" http://yourserver/insert_location.php

Get Location (GET Request)

curl http://yourserver/get_location.php?imei=123456789012345

Security Note:

  • Validate and sanitize inputs to prevent SQL injection.
  • Secure the endpoints using API keys or authentication methods.
  • Use HTTPS to protect data during transmission.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x