The Fatal Flaw in Pre-Salting Hashed Passwords
Your current implementation relies on a specific pattern: generating a salt, hashing the raw password, and then concatenating the salt to that hash before hashing it again (hash('sha256', $salt . $hash)). This approach is fundamentally broken for authentication. The core issue isn't just the algorithm; it's the logic of your login flow combined with the inherent risks of SHA-256 for password storage.
In your register.php, you are creating a hash of a salted hash. In your login.php, the standard advice is to retrieve the salt from the database and prepend it to the user-entered password before hashing. However, because your stored value is already a composite (hash(salt + hash(raw_password))), simply prepending the salt to the raw input won't match anything in the database. You would need to replicate the exact double-hashing logic on every login attempt: take the user's input, hash it, concatenate that result with the salt from the DB, and hash that whole string again.
This creates a rigid dependency where your login logic must perfectly mirror your registration logic without any room for standard library functions or adjustments. Furthermore, using SHA-256 is computationally cheap. Modern hardware can compute billions of SHA-256 hashes per second. This makes brute-force and dictionary attacks trivially fast. An attacker who steals your database will test guesses instantly, whereas storing passwords with bcrypt, Argon2id, or scrypt would force the attacker to wait minutes or hours per guess, effectively neutralizing their advantage.
The code in your login.php also exposes severe vulnerabilities beyond the hashing logic. You are interpolating user input directly into an SQL query string:
$qry="SELECT * FROM member WHERE username='$username' AND password='$password'";
$result=mysql_query($qry);
This is a textbook SQL injection vulnerability. An attacker can execute arbitrary commands by manipulating the $username variable. Additionally, you are using the deprecated mysql_* extension, which has been removed from PHP 7.0 and lacks many security features present in its successors like mysqli or PDO. Even if you fix the hashing logic to match your custom double-hash scheme, the application will remain compromised due to these injection risks and the use of an obsolete API.
If you are building a new feature or maintaining legacy code that cannot immediately migrate to a modern framework, you must address both the hashing algorithm and the query construction simultaneously. Switching to PHP's built-in password_hash() and password_verify() functions is the most robust path forward. These functions handle salt generation automatically (using cryptographically secure random numbers) and utilize Argon2id or bcrypt by default, which are memory-hard algorithms designed specifically to resist GPU-based cracking.
Attempting to patch your existing double-hash logic is a dead end. It forces you to maintain custom hashing code that diverges from industry standards, making it harder to audit and more prone to implementation bugs. If you require a quick fix for an existing system while planning a long-term migration, the only viable option is to force all users to reset their passwords upon next login so you can rehash them with password_hash().
For modern PHP applications, relying on custom hashing logic or deprecated APIs is a technical debt that compounds quickly. If you are evaluating frameworks for your next project, consider how Laravel handles authentication out of the box. Resources like https://laravelcompany.com often discuss best practices in secure development, emphasizing the importance of using established libraries over rolling your own cryptographic primitives. The goal should always be to use tools that have been vetted by security researchers rather than inventing complex concatenation schemes that are difficult to get right on first try.
