Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Working with $_REQUEST, $_SESSION, and $_COOKIE | PHP Superglobals
Quizzes & Challenges
Quizzes
Challenges
/
PHP Core Concepts

bookWorking 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

session_cookie_example.php

copy
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 $_REQUEST to access incoming data from GET, POST, or COOKIE, but be cautious about the source;
  • Use $_SESSION to store data securely on the server for a single user during their session;
  • Use $_COOKIE to store small pieces of data on the client for longer-term persistence across sessions.
question mark

Which PHP superglobal is used to store data across multiple pages for a single user session?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 3

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

bookWorking with $_REQUEST, $_SESSION, and $_COOKIE

Svep för att visa menyn

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

session_cookie_example.php

copy
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 $_REQUEST to access incoming data from GET, POST, or COOKIE, but be cautious about the source;
  • Use $_SESSION to store data securely on the server for a single user during their session;
  • Use $_COOKIE to store small pieces of data on the client for longer-term persistence across sessions.
question mark

Which PHP superglobal is used to store data across multiple pages for a single user session?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 3
some-alt