🛡️ Building Secure PHP Applications in 2026
PHP has come a long way over the years, and when used correctly it can power fast, secure and scalable applications. Unfortunately, many websites are still vulnerable because developers overlook the basics.
In this guide we'll walk through the essential security practices every PHP developer should be using.
🔐 1. Always Use Prepared Statements
Never insert user input directly into SQL queries.
❌ Bad
$username = $_POST['username'];
$sql = "SELECT * FROM users WHERE username = '$username'";
$result = $pdo->query($sql);
A malicious user could manipulate the query and access or destroy your database.
✅ Good
$stmt = $pdo->prepare("
SELECT *
FROM users
WHERE username = :username
");
$stmt->execute([
'username' => $_POST['username']
]);
$user = $stmt->fetch();
Prepared statements automatically separate data from SQL code, preventing SQL Injection attacks.
🔑 2. Hash Passwords Correctly
Never store passwords in plain text.
Never use:
- MD5
- SHA1
- Base64
Instead use PHP's built-in password hashing.
$hash = password_hash(
$password,
PASSWORD_DEFAULT
);
Verify passwords using:
if(password_verify($password, $hash))
{
echo "Login Successful";
}
🚦 3. Rate Limit Sensitive Pages
Login pages are a favourite target for attackers.
A simple rate limiter can stop brute-force attacks before they become a problem.
if($attempts > 5)
{
http_response_code(429);
exit("Too many login attempts.");
}
Recommended limits:
| Action | Suggested Limit |
|---|---|
| Login | 5 per minute |
| Register | 3 per hour |
| Contact Form | 5 per hour |
| Password Reset | 3 per hour |
🛡️ 4. Protect Every Form with CSRF Tokens
Without CSRF protection an attacker can trick logged-in users into submitting requests they never intended.
Generate a token:
$_SESSION['csrf'] = bin2hex(random_bytes(32));
Validate it before processing the form.
if(
!hash_equals(
$_SESSION['csrf'],
$_POST['csrf']
)
)
{
exit("Invalid CSRF Token");
}
📤 5. Secure File Uploads
Never trust uploaded files.
Always:
- ✅ Check MIME type
- ✅ Restrict extensions
- ✅ Generate random filenames
- ✅ Store uploads outside your public directory
- ✅ Limit maximum upload size
Example:
$filename = bin2hex(random_bytes(16));
move_uploaded_file(
$_FILES['image']['tmp_name'],
"/uploads/$filename.png"
);
🍪 6. Secure Sessions
After a successful login always regenerate the session ID.
session_regenerate_id(true);
Also enable secure cookies.
session_set_cookie_params([
'secure' => true,
'httponly' => true,
'samesite' => 'Strict'
]);
🌐 7. Always Use HTTPS
Modern websites should never transmit passwords over HTTP.
HTTPS protects:
- Login credentials
- Cookies
- Personal information
- Payment details
- API requests
If your site still supports HTTP, it's time to change that.
📋 Security Checklist
| Feature | Status |
|---|---|
| Prepared Statements | ✅ |
| Password Hashing | ✅ |
| CSRF Protection | ✅ |
| HTTPS | ✅ |
| Rate Limiting | ✅ |
| Secure Sessions | ✅ |
| File Validation | ✅ |
| Input Validation | ✅ |
| Output Escaping | ✅ |
💡 Final Thoughts
Security isn't achieved by adding one feature—it's the result of many small protections working together.
Whether you're building a portfolio, a community platform, or a large web application, taking the time to implement these practices will protect both your users and your data.
What security practices do you always include in your PHP projects? Share your thoughts below—I'd love to hear what your checklist looks like.
Replies
0 comments
Please log in to comment.