Skip to content

Lesson 9 of 9 · 24m

Modernizing Legacy PHP Without the Big Rewrite

Replace dangerous habits incrementally with wrappers, regression tests, strangler routes, password migration, and security checks that run before every deploy.

The rewrite pitch always sounds clean: throw away the legacy application and build it properly. Meanwhile, the business runs on the ugly system, the rewrite misses hidden behavior, and the migration deadline becomes a security freeze lasting eighteen months. Senior work is usually less romantic. You make the existing system safer in thin slices until the dangerous architecture no longer matters.

Put the dangerous primitives behind doors

You cannot audit every concatenated query in a weekend, but you can stop new ones from appearing. Introduce small wrappers with safe behavior and use them at the edges you touch:

function db_query(PDO $pdo, string $sql, array $params = []): PDOStatement {
    $stmt = $pdo->prepare($sql);
    $stmt->execute($params);
    return $stmt;
}

Then enforce with code review and simple CI greps: no new mysql_query, mysqli_query concatenation, eval, dynamic includes, or raw echo of request data. The goal is not instant purity; it is ratcheting in one direction.

Characterize behavior before changing it

Legacy systems often encode business rules nobody remembers. Write characterization tests around current behavior before refactoring security-sensitive flows. If the invoice endpoint currently rounds in a strange way or accepts a legacy parameter, capture that behavior first. Security fixes that silently alter workflows get rolled back — sometimes by disabling the security control.

Strangle routes instead of rewriting modules

Move one route or controller at a time behind a modern front controller or framework kernel. The router sends new traffic to new code and falls through to legacy behavior for everything else. This gives you deployable progress, rollback, and a boundary where shared authentication and authorization can be centralized.

Upgrade authentication incrementally

Many legacy apps still contain MD5 or SHA1 password hashes. Do not mass-reset passwords if you can avoid the disruption. Verify legacy hashes once at login, then rehash transparently:

if (hash_equals($user['legacy_hash'], md5($password))) {
    $newHash = password_hash($password, PASSWORD_ARGON2ID);
    $users->upgradeHash($user['id'], $newHash);
} elseif (password_verify($password, $user['password_hash'])) {
    if (password_needs_rehash($user['password_hash'], PASSWORD_ARGON2ID)) {
        $users->upgradeHash($user['id'], password_hash($password, PASSWORD_ARGON2ID));
    }
}

Add regression tests for fixed vulnerabilities

Every verified vulnerability deserves a test that would have caught it. A CSRF fix gets a test that posting without a token returns 403. An IDOR fix gets a test that user A cannot fetch user B's invoice. An upload fix gets malicious fixture files. These tests are not bureaucracy; they are how you keep a legacy system from reopening the same wound during a rushed feature.

Make security part of deployment

A thin CI pipeline beats a heroic annual review:

composer audit --locked
php -l $(find src public -name '*.php')
vendor/bin/phpunit
rg -n "eval\(|extract\(\$_(GET|POST|REQUEST)|mysql_query\(" src public && exit 1

Field notes

  • Do not mix a security migration with a visual redesign unless forced; separate rollback reasons.
  • If behavior is ambiguous, ask operators and support staff before assuming; they often know the undocumented workflow.
  • Measure progress by attack-surface reduction — endpoints migrated, dangerous sinks removed, deprecated auth paths closed — not by how modern the code feels.

Was this lesson helpful?

Sign in to rate