Working with $_REQUEST, $_SESSION, and $_COOKIE
PHP provides several built-in variables known as superglobals that allow you to access and manage data throughout your application. Among these, $_REQUEST, $_SESSION, and $_COOKIE are especially useful for handling user data across different pages and requests, but each serves a distinct purpose.
session_cookie_example.php
123456789101112131415161718192021222324<?php // Start the session session_start(); // Set a session variable $_SESSION['user'] = 'Alice'; // Set a cookie that expires in 1 hour setcookie('favorite_color', 'blue', time() + 3600); // Retrieve and display the session variable if (isset($_SESSION['user'])) { echo "Session user: " . $_SESSION['user'] . "<br>"; } else { echo "No session user found.<br>"; } // Retrieve and display the cookie value if (isset($_COOKIE['favorite_color'])) { echo "Favorite color from cookie: " . $_COOKIE['favorite_color'] . "<br>"; } else { echo "No favorite color cookie found.<br>"; } ?>
To summarize:
- Use
$_REQUESTto access incoming data from GET, POST, or COOKIE, but be cautious about the source; - Use
$_SESSIONto store data securely on the server for a single user during their session; - Use
$_COOKIEto store small pieces of data on the client for longer-term persistence across sessions.
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Can you give examples of how to use each of these superglobals in PHP?
What are the security considerations when using $_REQUEST, $_SESSION, and $_COOKIE?
When should I use $_SESSION instead of $_COOKIE?
Awesome!
Completion rate improved to 5
Working with $_REQUEST, $_SESSION, and $_COOKIE
Veeg om het menu te tonen
PHP provides several built-in variables known as superglobals that allow you to access and manage data throughout your application. Among these, $_REQUEST, $_SESSION, and $_COOKIE are especially useful for handling user data across different pages and requests, but each serves a distinct purpose.
session_cookie_example.php
123456789101112131415161718192021222324<?php // Start the session session_start(); // Set a session variable $_SESSION['user'] = 'Alice'; // Set a cookie that expires in 1 hour setcookie('favorite_color', 'blue', time() + 3600); // Retrieve and display the session variable if (isset($_SESSION['user'])) { echo "Session user: " . $_SESSION['user'] . "<br>"; } else { echo "No session user found.<br>"; } // Retrieve and display the cookie value if (isset($_COOKIE['favorite_color'])) { echo "Favorite color from cookie: " . $_COOKIE['favorite_color'] . "<br>"; } else { echo "No favorite color cookie found.<br>"; } ?>
To summarize:
- Use
$_REQUESTto access incoming data from GET, POST, or COOKIE, but be cautious about the source; - Use
$_SESSIONto store data securely on the server for a single user during their session; - Use
$_COOKIEto store small pieces of data on the client for longer-term persistence across sessions.
Bedankt voor je feedback!