PHP with HTML, Request-Response Flow, Cookies, and Sessions
See how PHP works with HTML output and learn the browser-server concepts behind user state and login flows.
Inside this chapter
- Embedding PHP in HTML
- Understanding the Response
- Cookies
- Sessions
- Real Example
Series navigation
Study the chapters in order for the clearest path from PHP basics to backend architecture, security, deployment, and production engineering habits. Use the navigation at the bottom to move smoothly through the full tutorial series.
Embedding PHP in HTML
<h1>Welcome, <?php echo htmlspecialchars($userName); ?></h1>
PHP can generate HTML dynamically based on data, permissions, records, or application state. This ability made PHP especially popular for dynamic websites.
Understanding the Response
PHP generates output on the server and sends the result back to the browser. This output can be HTML, JSON, CSV, a redirect header, or even a file download. Knowing what the server returns is key to understanding web programming.
Cookies
setcookie("theme", "dark", time() + 3600);
Cookies store small pieces of data in the browser. They are often used for preferences, session identifiers, and lightweight cross-request memory.
Sessions
session_start();
$_SESSION['user_id'] = 101;
Sessions allow the server to remember data across requests for a user, usually through a session ID stored in a cookie. Login systems, carts, and multi-step workflows commonly rely on sessions.
Real Example
After a successful login, a PHP application may store the authenticated user ID in the session, then render a personalized dashboard. Cookies might remember the preferred language or theme.