Part 1: Core Concepts (2 Marks)
- PHP (Hypertext Preprocessor): An open-source, server-side, interpreted scripting language designed primarily for web development, executed on web servers (Apache, Nginx) before HTML output is sent to the client.
- PHP Superglobals: Predefined global arrays accessible from any scope without
global $var:$_GET,$_POST,$_SERVER,$_COOKIE,$_SESSION,$_FILES,$_REQUEST,$_ENV. includevsrequire:include: If the specified file is missing, generates a Warning (E_WARNING) and continues execution.require: If missing, generates a fatal error (E_COMPILE_ERROR) and halts program execution immediately.
Part 2: Structural Frameworks (10 Marks)
Client-Server PHP Execution Lifecycle
- [Client Web Browser] -- 1. HTTP GET request (index.php)
- [Web Server (Apache/Nginx)] 2. Dispatches to PHP Engine v [PHP Interpreter Engine] 3. Executes DB Queries (MySQL) 4. Renders pure HTML v [Client Web Browser] <-- 5. Returns pure HTML stream ------- [Web Server]
Part 3: Distinction Tables
| Basis of Comparison | Client-Side Scripting (JS) | Server-Side Scripting (PHP) |
|---|---|---|
| Execution Environment | Client browser engine (V8, SpiderMonkey). | Web server PHP runtime engine. |
| Source Code Visibility | Fully visible to user via “View Page Source”. | Source code stays confidential; user receives only output HTML. |
| Database Access | Cannot directly access internal DB ports. | Full native direct database access (MySQL/PDO). |
Part 4: Worked Problems & Exam Traps
Form Processing with Superglobal $_POST
- <?php
- if ($_SERVER[“REQUEST_METHOD”] == “POST”) {
_POST[‘username’])); _POST[‘email’]), FILTER_SANITIZE_EMAIL); - if (empty(
email)) {
- echo “<p style=‘color:red;’>All fields are required!</p>”;
- } else {
- echo "<p>Welcome, " .
email . “</p>”; - }
- }
- ?>
- <form method=“POST” action=“<?php echo htmlspecialchars($_SERVER[“PHP_SELF”]);?>”>
- Name: <input type=“text” name=“username”><br>
- Email: <input type=“email” name=“email”><br>
- <input type=“submit” value=“Submit”>
- </form>
Printing
$_SERVER["PHP_SELF"]withouthtmlspecialchars()exposes the webpage to Cross-Site Scripting (XSS) via URL tampering.