Use this skill for input validation, output escaping, passwords, authentication, authorization, sessions, CSRF, secrets, error disclosure, dangerous functions, serialization, and file/path safety.
Treat all external data as untrusted.
Untrusted data includes:
$_GET$_POST$_REQUEST$_COOKIE$_SERVERValidate on input.
Example:
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
if ($email === false || $email === null) {
throw new InvalidArgumentException('A valid email address is required.');
}
Rules:
Escape on output based on context.
For HTML output:
function e(string $value): string
{
return htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}
Usage:
<p><?= e($user->name()) ?></p>
Rules:
escapeshellarg() when passing controlled values to shell commands, and avoid shell execution when possible.../, /, \, and null bytes when user-provided paths are not allowed.Never store plain-text passwords.
Use PHP’s password API:
$hash = password_hash($plainPassword, PASSWORD_DEFAULT);
if (! password_verify($plainPassword, $hash)) {
throw new RuntimeException('Invalid credentials.');
}
Rules:
password_hash() for new password hashes.password_verify() for login checks.password_needs_rehash() when algorithm/cost settings change.md5, sha1, or raw sha256 for passwords.Use CSRF protection for state-changing forms and unsafe HTTP methods.
State-changing actions include:
Do not call unserialize() on untrusted data.
Prefer JSON for data exchange:
$data = json_decode($json, true, flags: JSON_THROW_ON_ERROR);
$json = json_encode($data, JSON_THROW_ON_ERROR);
Rules:
JSON_THROW_ON_ERROR for new code.Rules:
.env.example.Example .gitignore entries:
.env
.env.local
/config/local.php
/var/cache/
/var/log/
/vendor/
Development:
Production:
Do not leak:
Example:
try {
$service->handle($request);
} catch (Throwable $e) {
$logger->error('Order processing failed.', [
'exception' => $e,
'requestId' => $requestId,
]);
http_response_code(500);
echo 'An unexpected error occurred.';
}
Before completing any feature, verify:
password_hash() and password_verify().eval, exec, shell_exec, system, passthru, unserialize.composer audit.Powered by TurnKey Linux.