PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
65: Password Hashing with password_hash
A few years ago, I was reviewing a codebase for a startup that had been built by a very talented, but slightly outdated, developer. He had a "security" strategy for passwords that looked like this: md5($password . "s3cret_s@lt_123"). He felt great about it because he'd added a "salt"—a random string to prevent rainbow table attacks. The problem? That salt was hardcoded in the source code. When the database was eventually leaked in a minor breach, the attacker found the salt in a backup of the PHP files, and the entire user table was cracked in under an hour. MD5 is just too fast; a modern GPU can chew through billions of MD5 hashes per second.
This is why we stop trying to be clever with our own hashing logic. In modern PHP, you should never use md5(), sha1(), or even hash() for passwords. Instead, we use a dedicated API designed specifically to be slow and computationally expensive, which is exactly what you want when defending against brute-force attacks.
The Magic of password_hash()
The password_hash() function is the industry standard for a reason. It doesn't just scramble the string; it handles the salting for you automatically. You don't need to create a salt column in your database or store a secret string in your config file. The salt is generated randomly for every single password and is bundled directly into the resulting hash string.
$password = "my_super_secret_123";
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
echo $hashedPassword;
// Output looks something like: $2y$10$nOUIs5kJWOnp7v9sS6S.u.GfS2S8KkFfLd/R7P8V.h6R...
Notice that PASSWORD_DEFAULT. I always recommend using this because it tells PHP to use the strongest algorithm currently supported by your version of PHP (usually Bcrypt or Argon2). If PHP is updated in the future to a stronger algorithm, your code stays current without you having to manually change the algorithm constant everywhere.
Verification and the Cost Factor
Since the salt is baked into the hash, you can't just hash the user's input during login and compare two strings with ==. That's a common mistake. Instead, you use password_verify(). This function knows how to extract the salt from the stored hash, apply it to the plain-text password provided by the user, and tell you if they match.
$userInput = "my_super_secret_123";
$storedHash = '$2y$10$nOUIs5kJWOnp7v9sS6S.u.GfS2S8KkFfLd/R7P8V.h6R...';
if (password_verify($userInput, $storedHash)) {
echo "Access granted!";
} else {
echo "Invalid password.";
}
One thing to keep in mind: these functions are intentionally slow. That's a feature, not a bug. If you're running this on a very powerful server or targeting extremely high security, you can pass an options array as the third argument to password_hash() to increase the "cost." The cost determines how many iterations the hashing algorithm runs. Increasing the cost by 1 doubles the time it takes to calculate the hash. Just be careful—set it too high, and you'll accidentally create a Denial of Service (DoS) vulnerability where a few login attempts peg your CPU at 100%.
📋 Practical Task
Exercise: Building a Secure User Credential Validator
Your task is to create a small script that simulates a user registration and login flow. You don't need a real database for this—you can use an associative array to simulate one.
- Create an array called
$user_dbwhere the keys are usernames and the values are the hashed passwords. - Write a function called
registerUser($username, $password)that hashes the password usingPASSWORD_DEFAULTand adds the user to your array. - Write a function called
loginUser($username, $password)that checks if the username exists in the array and usespassword_verify()to check if the password is correct. - Test your system by registering a user, then attempting to log in with both the correct password and a wrong one to verify your logic works.
There are no comments for now.