This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Session Handling-2”.
1. Which function is used to erase all session variables stored in the current session?
a) session_destroy()
b) session_change()
c) session_remove()
d) session_unset()
View Answer
Explanation: The function session_unset() frees all session variables that is currently registered. This will not completely remove the session from the storage mechanism. If you want to completely destroy the session, you need to use the function session_destroy().
2. What will the function session_id() return is no parameter is passed?
a) Current Session Identification Number
b) Previous Session Identification Number
c) Last Session Identification Number
d) Error
View Answer
Explanation: The function session_id() will return the session id for the current session or the empty string (” “) if there is no current session.
3. Which one of the following statements should you use to set the session username to Nachi?
a) $SESSION[‘username’] = “Nachi”;
b) $_SESSION[‘username’] = “Nachi”;
c) session_start(“nachi”);
d) $SESSION_START[“username”] = “Nachi”;
View Answer
Explanation: You need to refer the session variable ‘username’ in the context of the $_SESSION superglobal.
4. What will be the output of the following PHP code? (Say your previous session username was nachi.)
unset($_SESSION['username']);
printf("Username now set to: %s", $_SESSION['username']);
a) Username now set to: nachi
b) Username now set to: System
c) Username now set to:
d) Error
View Answer
Explanation: If someone want to destroy a single session variable then they can use the function unset () to unset a session variable. To delete the session variable ‘username’ we use the unset () function.
5. An attacker somehow obtains an unsuspecting user’s SID and then using it to impersonate the user in order to gain potentially sensitive information. This attack is known as __________
a) session-fixation
b) session-fixing
c) session-hijack
d) session-copy
View Answer
Explanation: The attack session fixation attempts to exploit the vulnerability of a system that allows one person to set another person’s session identifier. You can minimize this risk by regenerating the session ID on each request while maintaining the session-specific data. PHP offers a convenient function named session_regenerate_id() that will replace the existing ID with a new one.
6. Which parameter determines whether the old session file will also be deleted when the session ID is regenerated?
a) delete_old_file
b) delete_old_session
c) delete_old_session_file
d) delete_session_file
View Answer
Explanation: The parameter delete_old_session determines whether the old session file will also be deleted when the session ID is regenerated.
7. Which function effectively deletes all sessions that have expired?
a) session_delete()
b) session_destroy()
c) session_garbage_collect()
d) SessionHandler::gc
View Answer
Explanation: SessionHandler::gc is used to clean up expired sessions. It is called randomly by PHP internally when a session_start() is invoked.
8. Which function is used to transform PHP’s session-handler behavior into that defined by your custom handler?
a) session_set_save()
b) session_set_save_handler()
c) Session_handler()
d) session_save_handler()
View Answer
Explanation: The function session_set_save_handler() is used to set the user-level session storage functions which are used for storing and retrieving data associated with a session.
9. The session_start() function must appear _________
a) after the html tag
b) after the body tag
c) before the body tag
d) before the html tag
View Answer
Explanation: Like this: <?php session_start(); ?> <html>
10. What is the return type of session_set_save_handler() function?
a) boolean
b) integer
c) float
d) character
View Answer
Explanation: Returns TRUE on success or FALSE on failure.
Sanfoundry Global Education & Learning Series – PHP Programming.
To practice all questions on PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
- Practice MCA MCQs
- Check MCA Books
- Practice Programming MCQs
- Apply for Programming Internship
- Check Information Technology Books