TutorialsPHPPHP Web Features
Share:

PHP Web Features

intermediate

What You'll Learn

    Theory

    PHP excels at web-specific tasks. It natively handles form submissions, maintains user state with sessions and cookies, and processes HTTP requests.

    Form Handling

    PHP automatically populates $_GET and $_POST superglobals with form data:

    <!-- form.html -->
    <form method="POST" action="process.php">
        <input type="text" name="username">
        <input type="submit">
    </form>
     
    <!-- process.php -->
    <?php
    $username = $_POST['username'];
    echo "Hello, $username!";
    ?>
    • $_GET — URL parameters (?key=value)
    • $_POST — form body data (not visible in URL)
    • $_REQUEST — combines GET and POST

    Sessions

    Sessions store user data across multiple page requests. The server assigns a unique session ID stored in a cookie:

    session_start();
    $_SESSION['user_id'] = 42;
    $_SESSION['cart'] = ['item1', 'item2'];

    Sessions are server-side — only the session ID is stored on the client.

    Cookies

    Cookies are small pieces of data stored on the client's browser:

    setcookie("theme", "dark", time() + 86400 * 30);  // 30 days
    echo $_COOKIE['theme'];  // "dark"

    State Management Comparison

    | Method | Storage | Security | Lifetime | |--------|---------|----------|----------| | GET params | URL | Low | Single request | | POST data | Request body | Medium | Single request | | Cookies | Browser | Low | Configurable | | Sessions | Server | High | Configurable |

    PHP Session and Form Example
    php

    Why this matters

    Form handling, sessions, and cookies are at the core of every interactive website. From login systems to shopping carts, these PHP features let you manage user state and process input securely.

    What's next

    In the next lessons, you'll dive deeper into each topic with hands-on examples and exercises.

    Exercises

    Login Counter with Sessions

    easy

    Create a PHP page that uses sessions to count how many times a user has visited the page. Display the count and a personalized greeting.

    Expected Output:

    First visit: 'Welcome! This is your first visit.' Subsequent visits: 'Welcome back! You've visited X times.'

    Mini Quiz

    Mini Quiz