Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Introduction to Sessions and Cookies

Total questions: 17

Worksheet time: 9mins

Name
Class
Date
1.

What is the primary purpose of using sessions in PHP?

a)

To store data on the server for a specific user

b)

To track user preferences across multiple visits

c)

To store large amounts of data permanently

d)

To ensure that all users share the same data

2.

What is a key difference between sessions and cookies?

a)

Sessions store data on the client-side, whereas cookies store data on the server

b)

Cookies expire after the browser is closed, while sessions persist indefinitely

c)

Sessions are stored on the server, whereas cookies are stored on the client

d)

Cookies cannot store user authentication details

3.

What PHP function is used to start a session?

a)

start_session()

b)

session_begin()

c)

session_start()

d)

init_session()

4.

Which of the following is TRUE about cookies?

a)

They are stored in the client’s browser

b)

They can store sensitive data securely

c)

They can be accessed using $_COOKIE in PHP

d)

They are deleted as soon as the user leaves the website

5.

How can you set a cookie in PHP?

a)

create_cookie('user', 'JohnDoe', time() + 3600);

b)

setcookie('user', 'JohnDoe', time() + 3600, '/');

c)

store_cookie('user', 'JohnDoe', time() + 3600);

d)

cookie_set('user', 'JohnDoe', time() + 3600);

6.

How do you delete a cookie in PHP?

a)

deletecookie('user');

b)

setcookie('user', '', time() - 3600, '/');

c)

remove_cookie('user');

d)

unsetcookie('user');

7.

What function is used to destroy a session?

a)

session_delete();

b)

session_end();

c)

session_unset(); session_destroy();

d)

session_remove();

8.

Which PHP superglobal is used to retrieve session data?

a)

$_SESSION

b)

$_COOKIE

c)

$_REQUEST

d)

$_SERVER

9.

What is the default behavior of a PHP session when a browser is closed?

a)

It continues indefinitely

b)

It gets deleted automatically

c)

It remains stored in a cookie

d)

It refreshes and resets the session ID

10.

How do you retrieve a cookie value in PHP?

a)

$_COOKIE['user']

b)

getcookie('user')

c)

cookie_get('user')

d)

$_SESSION['user']

11.

What happens if session_start() is not called before using $_SESSION?

a)

The script will produce an error

b)

The session variables will not persist

c)

The script will work fine

d)

The session will start automatically

12.

What parameter in setcookie() sets the expiration time?

a)

The first parameter

b)

The second parameter

c)

The third parameter

d)

The fourth parameter

13.

What are some common use cases for cookies?

a)

Remembering login credentials

b)

Tracking user behavior on websites

c)

Storing large database records

d)

Maintaining temporary session data on the server

14.

What happens if a cookie is set without an expiration time?

a)

It will never be deleted

b)

It will be deleted when the browser is closed

c)

It will expire after 24 hours

d)

It will be stored permanently

15.

Which of the following is a correct way to set a cookie for all pages on a website?

a)

setcookie('user', 'JohnDoe', time() + 3600, '/');

b)

setcookie('user', 'JohnDoe', time() + 3600, '/home');

c)

setcookie('user', 'JohnDoe', time() + 3600, 'domain.com');

d)

setcookie('user', 'JohnDoe', time() + 3600, 'index.php');

16.

What happens when a cookie's expiration time is set to a past timestamp?

a)

The cookie remains unchanged

b)

The cookie's expiration time is extended

c)

The cookie is converted to a session cookie

d)

The cookie is deleted by the browser

17.

Which of the following are true about session cookies?

a)

They are automatically deleted when the browser is closed

b)

They can store complex data structures like arrays directly

c)

They are stored on the client-side permanently

d)

They are used to maintain user state across multiple requests