0
0
PHPprogramming~5 mins

Starting and using sessions in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a session in PHP?
A session in PHP is a way to store information (variables) to be used across multiple pages. It helps keep user data while they browse a website.
Click to reveal answer
beginner
How do you start a session in PHP?
Use the function session_start(); at the very beginning of your PHP script before any output. This creates or resumes a session.
Click to reveal answer
beginner
How do you store data in a PHP session?
You store data by assigning values to the $_SESSION superglobal array, like $_SESSION['key'] = 'value';.
Click to reveal answer
beginner
How do you access session data on another page?
First, call session_start(); on the page, then read the data from $_SESSION['key'].
Click to reveal answer
intermediate
What must you avoid before calling session_start();?
You must avoid sending any output (like HTML or echo) before session_start(); because it sends HTTP headers.
Click to reveal answer
Which PHP function starts or resumes a session?
Asession_start()
Bstart_session()
Cbegin_session()
Dinit_session()
Where should session_start() be placed in your PHP script?
AAfter HTML output
BAnywhere in the script
CBefore any output
DInside a function only
How do you store a user's name in a session variable?
Aset_session('name', 'John');
Bsession['name'] = 'John';
Csession->name = 'John';
D$_SESSION['name'] = 'John';
What happens if you try to access $_SESSION without calling session_start()?
AYou get an error or empty data
BSession data is automatically loaded
CSession is created automatically
DNothing happens, it works fine
Which superglobal array holds session variables?
A$_POST
B$_SESSION
C$_GET
D$_COOKIE
Explain how to start a session and store a user's favorite color in PHP.
Remember to start the session first, then save data in $_SESSION.
You got /4 concepts.
    Describe why it is important to call session_start() before any output in PHP.
    Think about how PHP sends session cookies in headers.
    You got /4 concepts.