Strengthen your PHP session and cookie management skills with this set of 30 multiple-choice questions. Explore key topics like session management, cookie handling, and security concerns, crucial for web development.
Which PHP function is used to start a session? A) session_start() B) start_session() C) begin_session() D) session_initialize()
What does the $_SESSION array hold in PHP? A) Server variables B) Cookies data C) User session data D) File data
How do you store a value in a session variable? A) $_SESSION[“key”] = “value”; B) session_store(“key”, “value”); C) store_session(“key”, “value”); D) session_set(“key”, “value”);
Which function is used to destroy a session in PHP? A) session_destroy() B) end_session() C) session_end() D) destroy_session()
What will happen if session_start() is not called at the beginning of a PHP script? A) Session data will not be accessible B) The script will throw an error C) The script will run normally without sessions D) It will automatically start the session
What is the default session expiration time in PHP? A) 15 minutes B) 30 minutes C) 60 minutes D) No expiration time
How can you regenerate a session ID in PHP for security purposes? A) session_regenerate_id() B) regenerate_id() C) session_reset_id() D) session_create_id()
What does the session_unset() function do in PHP? A) Deletes a session B) Clears all session variables C) Ends the session D) Regenerates the session ID
How do you access a session variable in PHP? A) $_SESSION[] B) session_get() C) $_SESSION{} D) session_array[]
Which of the following is true about session_destroy()? A) It removes all session variables but keeps the session active B) It clears the session and deletes the session file C) It ends the session and redirects the user D) It starts a new session
Topic 2: Cookies – Setting, Retrieving, and Deleting (10 Questions)
Which function is used to set a cookie in PHP? A) setcookie() B) cookie_set() C) create_cookie() D) add_cookie()
How do you retrieve the value of a cookie in PHP? A) $_COOKIE[“cookie_name”] B) cookie_get(“cookie_name”) C) $_COOKIE[] D) get_cookie(“cookie_name”)
Which of the following is required when setting a cookie? A) Cookie name B) Cookie value C) Expiration time D) All of the above
What will happen if the expiration time for a cookie is set to a past time? A) The cookie will be deleted B) The cookie will never be set C) The cookie will expire immediately D) The cookie will be stored forever
How long will a cookie last if the expiration time is set to 0? A) Until the session ends B) Until the browser is closed C) 1 year D) 30 days
Which of the following is correct for deleting a cookie in PHP? A) setcookie(“cookie_name”, “”, time() – 3600); B) delete_cookie(“cookie_name”); C) unset_cookie(“cookie_name”); D) clear_cookie(“cookie_name”);
What is the default path value when setting a cookie in PHP? A) / B) /cookie/ C) /user/ D) null
Which function can be used to check if a cookie is set in PHP? A) isset() B) check_cookie() C) cookie_exists() D) is_cookie_set()
Can cookies be accessed by JavaScript? A) Yes, cookies are accessible via JavaScript B) No, cookies are only accessible by PHP C) Only for secure cookies D) Only on the server side
Which of the following affects cookie storage in PHP? A) HTTP only flag B) Secure flag C) Expiration time D) All of the above
Topic 3: Session Management (5 Questions)
What does the session_start() function do in PHP? A) Starts a new session or resumes an existing one B) Initializes a session array C) Creates a cookie for session D) Initializes the session storage
Where are session variables stored by default in PHP? A) In the database B) In browser cookies C) In server-side files D) In the session variable array
What is the main purpose of session_regenerate_id()? A) To restart a session B) To generate a new session ID for the current session C) To clear all session data D) To validate session data
What does session_destroy() do? A) Destroys the current session and all its variables B) Deletes the session file C) Ends the current session D) Both A and B
Can you change session variables after calling session_start()? A) Yes, session variables can be modified B) No, they are read-only C) You can only delete them D) You can only add new variables
Topic 4: Security Concerns with Sessions and Cookies (5 Questions)
Which PHP function can help protect session data from session fixation attacks? A) session_regenerate_id() B) session_unset() C) session_destroy() D) session_start()
What is the primary security risk with cookies? A) They can be read by the client-side script B) They can be stored on the server C) They are not encrypted D) They are not necessary for session management
How can you make cookies more secure in PHP? A) By setting the secure flag and HTTP only flag B) By using encryption C) By setting short expiration times D) By using session cookies only
What does the httponly flag in cookies do? A) Prevents JavaScript from accessing the cookie B) Makes the cookie accessible only via the browser C) Makes the cookie persistent D) Encrypts the cookie
What is the risk of not using secure cookies for sensitive data? A) Data can be exposed to man-in-the-middle attacks B) Data can be stored forever C) The session will expire immediately D) Cookies cannot be used with sessions
Answer Key
Qno
Answer (Option with the text)
1
A) session_start()
2
C) User session data
3
A) $_SESSION[“key”] = “value”;
4
A) session_destroy()
5
A) Session data will not be accessible
6
D) No expiration time
7
A) session_regenerate_id()
8
B) Clears all session variables
9
A) $_SESSION[]
10
A) Removes all session variables but keeps the session active
11
A) setcookie()
12
A) $_COOKIE[“cookie_name”]
13
D) All of the above
14
A) The cookie will be deleted
15
A) Until the session ends
16
A) setcookie(“cookie_name”, “”, time() – 3600);
17
A) /
18
A) isset()
19
A) Yes, cookies are accessible via JavaScript
20
D) All of the above
21
A) Starts a new session or resumes an existing one
22
C) In server-side files
23
B) To generate a new session ID for the current session
24
D) Both A and B
25
A) Yes, session variables can be modified
26
A) session_regenerate_id()
27
A) They can be read by the client-side script
28
A) By setting the secure flag and HTTP only flag
29
A) Prevents JavaScript from accessing the cookie
30
A) Data can be exposed to man-in-the-middle attacks