Website Development

2 min read

The PHP Security Checklist: 15 Fixes That Stop Most Attacks

You do not need a huge budget to dramatically reduce your risk. Work through this checklist and you will close the gaps most attackers rely on. Keep it…

PHP security best practices

You do not need a huge budget to dramatically reduce your risk. Work through this checklist and you will close the gaps most attackers rely on.

Keep it current

  1. Run a supported PHP version. Upgrade away from end-of-life releases and plan upgrades each year.
  2. Update CMS, plugins, themes and libraries. Enable automatic security updates where safe. Delete anything unused.
  3. Scan dependencies. Use composer audit and automated tools such as Dependabot to flag vulnerable packages.

Write safer code

  1. Use prepared statements for every query.

$stmt = $pdo->prepare(‘SELECT * FROM users WHERE id = :id’);
$stmt->execute([‘id’ => $_GET[‘id’]]);
$user = $stmt->fetch();

  1. Validate input and escape output. Validate type, length and format on the server. Escape on output to stop XSS:

echo htmlspecialchars($comment, ENT_QUOTES, ‘UTF-8’);

  1. Hash passwords properly.

$hash = password_hash($password, PASSWORD_DEFAULT);
if (password_verify($input, $hash)) { /* login ok */ }

  1. Protect forms with CSRF tokens and set SameSite on cookies.
  2. Never use eval() or unserialize untrusted data. Avoid passing user input to include, system or exec.

Lock down uploads and files

  1. Restrict file uploads. Allow-list file types, check real MIME type, rename files, store them outside the web root, and block PHP execution in upload directories.
  2. Set correct file permissions. Least privilege; configuration files not writable by the web server and never publicly accessible.

Secure access

  1. Enforce strong authentication. Multi-factor authentication for admins, login rate limiting, and session regeneration after login.
  2. Force HTTPS and add security headers (HSTS, Content-Security-Policy, X-Content-Type-Options).

Configure and monitor

  1. Turn off error display in production. Log errors privately instead: display_errors = Off.
  2. Add a web application firewall (WAF) and file integrity monitoring with alerts for suspicious changes.
  3. Back up automatically and test restores. Keep backups offsite and separate from the server.

Make it a habit Schedule a monthly patch review, a quarterly access review and an annual penetration test. Security is a process, not a project.