0% complete

Authentication · Lesson 1 of 7

User Registration

Validate a new account and store a password hash, never the password.

Lesson

Registration should validate required fields, normalize identifiers consistently, and enforce uniqueness in the database as well as application logic. Hash passwords with password_hash and verify them with password_verify. Avoid revealing whether a particular account already exists when that would enable enumeration. The example stores only a password hash; real code should also handle validation and duplicate-key errors. Further guidance Use PHP password hashing APIs, regenerate a session identifier after authentication, and clear session state during logout. Authentication identifies the user; authorization checks whether that user may perform this action on this record. Rate-limit sensitive endpoints and keep failure messages generic. Practice task: Create a fresh example for User Registration. Predict the result before running it, then change one input and explain why the result changes. Test a normal case and one boundary or invalid case where it applies. Compare your output with the example and note one rule you would use in a real project.

Code examples

User Registration
PHP
$passwordHash = password_hash($password, PASSWORD_DEFAULT);
$statement = $pdo->prepare('INSERT INTO users (email, password) VALUES (:email, :password)');
$statement->execute(['email' => $email, 'password' => $passwordHash]);
Expected output
A user row containing a one-way password hash

Key points

  • Validate a new account and store a password hash, never the password.
  • Store password hashes, not plaintext passwords.
  • Check authorization on every protected operation.

Your learning progress

Finish this lesson

Optional practice

PHP playground

This site does not execute learner-provided PHP code. Connect a secured PHP sandbox service before enabling code execution.

Execution is disabled. No code is sent to a server.