To secure your API endpoints in PHP, you need to implement several strategies. These include authentication, authorization, input validation, rate limiting, and logging. Below, we will explore each of these strategies with practical examples.

1. Authentication

Authentication verifies the identity of a user or application trying to access your API. Common methods include API keys, OAuth tokens, and JWT (JSON Web Tokens).

Example: Using JWT for Authentication

use Firebase\JWT\JWT;

function generateJWT($userId) {
    $key = "your_secret_key";
    $payload = [
        'iat' => time(),
        'exp' => time() + 3600, // Token valid for 1 hour
        'sub' => $userId
    ];
    return JWT::encode($payload, $key);
}

function authenticateRequest($token) {
    $key = "your_secret_key";
    try {
        $decoded = JWT::decode($token, $key, ['HS256']);
        return $decoded->sub; // Return user ID
    } catch (Exception $e) {
        return null; // Token is invalid
    }
}

2. Authorization

Once authenticated, you need to ensure that users have permission to access specific resources. Implement role-based access control (RBAC) to manage user permissions effectively.

Example: Role-Based Access Control

function hasAccess($userRole, $requiredRole) {
    $rolesHierarchy = [
        'admin' => 3,
        'editor' => 2,
        'viewer' => 1
    ];
    
    return $rolesHierarchy[$userRole] >= $rolesHierarchy[$requiredRole];
}

// Usage
if (!hasAccess($userRole, 'editor')) {
    http_response_code(403);
    echo json_encode(['error' => 'Forbidden']);
    exit;
}

3. Input Validation

Always validate and sanitize input data to prevent injection attacks and ensure data integrity. Use PHP's built-in functions to validate data types and formats.

Example: Validating Input Data

function validateInput($data) {
    if (!filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
        throw new Exception("Invalid email format");
    }
    if (!is_numeric($data['age']) || $data['age'] < 0) {
        throw new Exception("Age must be a positive number");
    }
    return true;
}

// Usage
try {
    validateInput($_POST);
} catch (Exception $e) {
    http_response_code(400);
    echo json_encode(['error' => $e->getMessage()]);
    exit;
}

4. Rate Limiting

To prevent abuse of your API endpoints, implement rate limiting. This restricts the number of requests a user can make in a given time frame.

Example: Simple Rate Limiting

session_start();

function checkRateLimit() {
    $limit = 100; // Max requests
    $timeFrame = 3600; // 1 hour

    if (!isset($_SESSION['request_count'])) {
        $_SESSION['request_count'] = 0;
        $_SESSION['first_request_time'] = time();
    }

    if (time() - $_SESSION['first_request_time'] > $timeFrame) {
        $_SESSION['request_count'] = 0; // Reset count after timeframe
        $_SESSION['first_request_time'] = time();
    }

    $_SESSION['request_count']++;

    if ($_SESSION['request_count'] > $limit) {
        http_response_code(429);
        echo json_encode(['error' => 'Rate limit exceeded']);
        exit;
    }
}

// Usage
checkRateLimit();

5. Logging

Implement logging to monitor API access and detect potential security incidents. Use a logging library to manage log entries effectively.

Example: Basic Logging

function logRequest($message) {
    $logFile = 'api_requests.log';
    $timestamp = date('Y-m-d H:i:s');
    file_put_contents($logFile, "[$timestamp] $message\n", FILE_APPEND);
}

// Usage
logRequest("User accessed endpoint: " . $_SERVER['REQUEST_URI']);

Summary of Best Practices

Best PracticeDescription
AuthenticationUse JWT or OAuth for secure user authentication.
AuthorizationImplement RBAC to control access to resources.
Input ValidationValidate and sanitize all incoming data.
Rate LimitingLimit requests per user to prevent abuse.
LoggingKeep logs of API access for monitoring and auditing.

By implementing these practices, you can significantly enhance the security of your PHP API endpoints, protecting your application from unauthorized access and various attacks.

Learn more with useful resources