Preparing for a first PHP interview? Practise these questions by building a small login project with PHP and MySQL. You can use a local WampServer setup and a dummy account. By the end, you should be able to explain what happens between clicking Sign in and opening a protected page.
PHP interview questions for freshers
1. How does PHP receive data from a form?
With a normal HTML form submitted using method="post", PHP reads fields by name from $_POST. Use POST for credentials and send them over HTTPS on a hosted site. In your project, inspect the form’s name attributes and check that each field maps to the expected PHP key.
2. What is server-side validation?
Check that the request contains the expected values and types before using them. In the login form, require a valid email and a non-empty password. Test with a blank field and a malformed email. When displaying user text back in HTML, use context-appropriate escaping such as htmlspecialchars() for HTML text.
3. How do prepared statements help?
A prepared statement keeps the SQL query separate from its data values. Use :email as the placeholder, then pass the email to execute(). When explaining this in an interview, point to the query and the separate value you bind. PHP PDO prepared statements
4. How do you check a password?
Hash the password with password_hash($password, PASSWORD_DEFAULT) when creating the account. At login, pass the entered password and the stored hash to password_verify(). Store the hash in a column that can hold at least 255 characters so the default algorithm has room to change. PHP password hashing · PHP password verification
5. How does PHP remember a signed-in user?
Call session_start() before output and store the authenticated user’s ID in $_SESSION. The browser normally sends a session cookie that lets PHP find the session on later requests. Regenerate the session ID after verifying credentials and before setting the authenticated state. PHP session management
Mini project. Login, protected dashboard and logout
- Create a practice database and a
userstable with a primary keyid, a uniqueemailand apassword_hashcolumn. Use a local setup script to insert one dummy account with a hashed password. - Make a login form with email, password and a hidden CSRF token generated with
random_bytes()and kept in the session. In the handler, accept POST and check the submitted token withhash_equals()before reading credentials. - Connect to the database with PDO and run the authentication block below. Use the same public error for unknown emails and incorrect passwords.
- In
dashboard.php, start the session and redirect to login if$_SESSION['user_id']is missing. Add a logout POST form with a CSRF token. On logout, clear session data, expire the session cookie and redirect to the login page.
Authentication block after the request and token checks
<?php
// Configure $pdo and call session_start() before rendering any HTML.
// Check the POST method and the form's CSRF token before this block.
$email = $_POST['email'] ?? null;
$password = $_POST['password'] ?? null;
if (
! is_string($email)
|| ! is_string($password)
|| $password === ''
|| ! filter_var(trim($email), FILTER_VALIDATE_EMAIL)
) {
exit('Email or password is incorrect.');
}
$query = $pdo->prepare(
'SELECT id, password_hash FROM users WHERE email = :email'
);
$query->execute(['email' => trim($email)]);
$user = $query->fetch(PDO::FETCH_ASSOC);
if (! $user || ! password_verify($password, $user['password_hash'])) {
exit('Email or password is incorrect.');
}
session_regenerate_id(true);
$_SESSION['user_id'] = (int) $user['id'];
header('Location: dashboard.php', true, 303);
exit;
Test the flow and explain it aloud
Try the correct password, an incorrect password and an email that has no account. Open the dashboard in a fresh browser session and confirm it redirects to login. Then sign in, log out and request the dashboard again. For interview practice, explain the sequence in 60 seconds, from the form’s POST request to the new session ID. Use the project to identify where you would add rate limiting before deploying a public login.
For guided practice with PHP forms, databases and sessions, explore PHP training in Ambala.