Source code activity
Exercise: Implement User Authentication System
Description: Develop a user authentication system for a web application using PHP and
MySQL. Implement both vulnerable and secure versions to demonstrate the risks of
SQL injection and how to prevent them using prepared statements and proper input
validation.
Instructions:
1. Design a database schema to store user credentials. Create a table named
"users" with columns for "id" (INT), "username" (VARCHAR), and "password"
(VARCHAR).
2. Implement user registration functionality in PHP. Create forms to allow users to
register by providing a username and password. Store the user credentials
securely in the "users" table.
3. Develop login functionality with two versions: a. Vulnerable version: Concatenate
user input directly into the SQL query to check for user credentials. b. Secure
version: Use prepared statements and parameterized queries to validate user
credentials securely.
4. Test both versions of the login functionality by attempting to log in with valid and
invalid credentials. Additionally, try injecting SQL commands into the vulnerable
version to demonstrate the risk of SQL injection.
5. Compare the behaviors of the vulnerable and secure versions. Show how the
secure version prevents SQL injection attacks while the vulnerable version is
susceptible.
6. Provide explanations and comments in your code to clarify the differences and
highlight the importance of using prepared statements and proper input validation
for SQL injection prevention.