Courses/PHP/Sessions & Cookies

    Lesson 10 โ€ข Intermediate

    Sessions & Cookies ๐Ÿช

    Persist data between page loads โ€” use sessions for secure server-side storage and cookies for client-side preferences.

    What You'll Learn in This Lesson

    • โ€ข How sessions maintain state across HTTP requests
    • โ€ข Setting, reading, and destroying session variables
    • โ€ข Creating and deleting cookies with secure settings
    • โ€ข When to use sessions vs cookies
    • โ€ข Building a secure login system with "remember me"

    Try It: PHP Sessions

    Start sessions, store login data, check authentication status

    Try it Yourself ยป
    JavaScript
    // PHP Sessions (simulated in JavaScript)
    console.log("=== How Sessions Work ===");
    console.log();
    console.log("HTTP is STATELESS โ€” each request is independent.");
    console.log("Sessions let you remember users between page loads.");
    console.log();
    
    console.log("1. User visits site โ†’ PHP generates session ID");
    console.log("2. Session ID stored in browser cookie (PHPSESSID)");
    console.log("3. Server stores data linked to that ID");
    console.log("4. Next request โ†’ browser sends cookie โ†’ server finds
    ...

    Try It: Cookies & Security

    Set secure cookies, compare sessions vs cookies, and build a login system

    Try it Yourself ยป
    JavaScript
    // PHP Cookies (simulated in JavaScript)
    console.log("=== Sessions vs Cookies ===");
    console.log();
    
    console.log("Feature        | Sessions           | Cookies");
    console.log("โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€");
    console.log("Stored on      | Server             | Client (browser)");
    console.log("Security       | More secure        | Less secure");
    console.log("Max size       | No practical limit | 4KB per cookie");
    console.log("Lifetime       | Browser close*     | Custom
    ...

    โš ๏ธ Common Mistakes

    โš ๏ธ
    Not calling session_start() first โ€” must be called before ANY output (even whitespace!). Put it at the very top of your PHP file.
    โš ๏ธ
    Storing sensitive data in cookies โ€” cookies are stored on the client and can be tampered with. Never put passwords or secret tokens in cookies.
    โš ๏ธ
    Not regenerating session ID on login โ€” always call session_regenerate_id(true) after successful login to prevent session fixation attacks.
    ๐Ÿ’ก
    Pro Tip: Always set httponly: true and secure: true on cookies to prevent JavaScript access and ensure HTTPS-only transmission.

    ๐Ÿ“‹ Quick Reference โ€” Sessions & Cookies

    FunctionPurpose
    session_start()Start/resume a session
    $_SESSION['key']Get/set session data
    session_destroy()Destroy entire session
    setcookie()Create/update a cookie
    $_COOKIE['name']Read a cookie value
    session_regenerate_id()Prevent session fixation

    ๐ŸŽ‰ Lesson Complete!

    You now understand state management in PHP! Next, level up with Object-Oriented Programming.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service