WorksheetsIntroduction to Sessions and Cookies
Total questions: 17
Worksheet time: 9mins
What is the primary purpose of using sessions in PHP?
To store data on the server for a specific user
To track user preferences across multiple visits
To store large amounts of data permanently
To ensure that all users share the same data
What is a key difference between sessions and cookies?
Sessions store data on the client-side, whereas cookies store data on the server
Cookies expire after the browser is closed, while sessions persist indefinitely
Sessions are stored on the server, whereas cookies are stored on the client
Cookies cannot store user authentication details
What PHP function is used to start a session?
start_session()
session_begin()
session_start()
init_session()
Which of the following is TRUE about cookies?
They are stored in the client’s browser
They can store sensitive data securely
They can be accessed using $_COOKIE in PHP
They are deleted as soon as the user leaves the website
How can you set a cookie in PHP?
create_cookie('user', 'JohnDoe', time() + 3600);
setcookie('user', 'JohnDoe', time() + 3600, '/');
store_cookie('user', 'JohnDoe', time() + 3600);
cookie_set('user', 'JohnDoe', time() + 3600);
How do you delete a cookie in PHP?
deletecookie('user');
setcookie('user', '', time() - 3600, '/');
remove_cookie('user');
unsetcookie('user');
What function is used to destroy a session?
session_delete();
session_end();
session_unset(); session_destroy();
session_remove();
Which PHP superglobal is used to retrieve session data?
$_SESSION
$_COOKIE
$_REQUEST
$_SERVER
What is the default behavior of a PHP session when a browser is closed?
It continues indefinitely
It gets deleted automatically
It remains stored in a cookie
It refreshes and resets the session ID
How do you retrieve a cookie value in PHP?
$_COOKIE['user']
getcookie('user')
cookie_get('user')
$_SESSION['user']
What happens if session_start() is not called before using $_SESSION?
The script will produce an error
The session variables will not persist
The script will work fine
The session will start automatically
What parameter in setcookie() sets the expiration time?
The first parameter
The second parameter
The third parameter
The fourth parameter
What are some common use cases for cookies?
Remembering login credentials
Tracking user behavior on websites
Storing large database records
Maintaining temporary session data on the server
What happens if a cookie is set without an expiration time?
It will never be deleted
It will be deleted when the browser is closed
It will expire after 24 hours
It will be stored permanently
Which of the following is a correct way to set a cookie for all pages on a website?
setcookie('user', 'JohnDoe', time() + 3600, '/');
setcookie('user', 'JohnDoe', time() + 3600, '/home');
setcookie('user', 'JohnDoe', time() + 3600, 'domain.com');
setcookie('user', 'JohnDoe', time() + 3600, 'index.php');
What happens when a cookie's expiration time is set to a past timestamp?
The cookie remains unchanged
The cookie's expiration time is extended
The cookie is converted to a session cookie
The cookie is deleted by the browser
Which of the following are true about session cookies?
They are automatically deleted when the browser is closed
They can store complex data structures like arrays directly
They are stored on the client-side permanently
They are used to maintain user state across multiple requests
