Sample questions
Question 1Medium
Which one of the following is the very first task executed by a session enabled page?
- Delete the previous session
- Start a new session
- Check whether a valid session exists
- Handle the session
Answer: C. Check whether a valid session exists
Explanation
session enabled page firstly check whether a valid session exists. If yes then only next task is executed.
Step-by-step solution
- The question asks what a session enabled page does first.
- The source says the page first checks whether a valid session exists.
- Only when a valid session exists does the next task run.
- The correct answer is option C, Check whether a valid session exists.
Why the other options are wrong
- A. Deleting the previous session is not the first step; the page checks first.
- B. Starting a new session comes after the check for an existing valid session.
- D. Handling the session comes after the check.
Tip: For first task questions, look for the check that comes before any action.
Question 2Medium
If the directive session.cookie_lifetime is set to 3600, the cookie will live until....
- 3600 sec
- 3600 min
- 3600 hrs
- the browser is restarted
Answer: A. 3600 sec
Explanation
The lifetime is specified in seconds, so if the cookie should live 1 hour, this directive should be set to 3600.
Step-by-step solution
- session.cookie_lifetime is given in seconds.
- 3600 seconds is one hour.
- So the cookie lives for 3600 sec, option A, the correct answer.
Why the other options are wrong
- B. The unit is seconds, not minutes.
- C. The unit is seconds, so 3600 is not hours.
- D. A set lifetime ends the cookie after that time, not when the browser restarts.
Tip: Cookie lifetime settings are in seconds; 3600 means one hour.
Question 3Medium
Which one of the following function is used to start a session?
- start_session()
- session_start()
- session_begin()
- begin_session()
Answer: B. session_start()
Explanation
Before you can store user information in your PHP session, you must first start up the session. The session_start() function must appear BEFORE the <html> tag.
E.g.
<?php session_start(); ?>
<html>
<body>
</body>
</html>
Step-by-step solution
- PHP requires session_start() to initialize a session before any HTML.
- This function must be called at the very start of the PHP script.
- Other options like start_session() or begin_session() are not valid PHP functions.
- session_start() is the correct and only valid function to start a session.
- Final answer: session_start()
Why the other options are wrong
- A. start_session() is not a PHP function; session_start() starts a session.
- C. session_begin() is not a PHP function; session_start() starts a session.
- D. begin_session() is not a PHP function; session_start() starts a session.
Tip: Remember: session_start() is the only correct function to start a PHP session.
Question 4Medium
What is the default time(in seconds) for which session data is considered valid?
- 1800
- 3600
- 1440
- 1540
Answer: C. 1440
Explanation
The session.gc_maxlifetime directive determines this duration. It can be set to any required value.
Step-by-step solution
- The session data lifetime is set by session.gc_maxlifetime.
- Its default value is 1440 seconds, which is 24 minutes.
- 1440 is option C, the correct answer.
Why the other options are wrong
- A. 1800 is not the default value.
- B. 3600 is one hour and is not the default.
- D. 1540 is not the default value.
Tip: The default session lifetime is 1440 seconds (24 minutes); the setting is session.gc_maxlifetime.
Question 5Medium
What is the purpose of $_SESSION[]?
- Used to register a global variable
- Used to initialize a session.
- Used to store variables of the current session
- none of the above
Answer: C. Used to store variables of the current session
Explanation
The correct way to store and retrieve session variables is to use the PHP $_SESSION variable:
< ?php session_start();
// store session data
$_SESSION['views']=1;
? >
Step-by-step solution
- The SESSION superglobal array stores data for the current user session.
- Values are saved as key and value pairs and read back on later pages of the same session.
- Option C describes this, so it is the correct answer.
Why the other options are wrong
- A. It does not register a global variable; it holds data for the session.
- B. A session is started with session_start(), not by this array.
- D. Option C is correct, so none of the above is wrong.
Tip: The SESSION superglobal holds per-user data; call session_start() before using it.
Question 6Medium
In which variable is the users IP address stored?
- DOMAIN
- REMOTE_ADDR
- GLOBALS
- _ENV
Answer: B. REMOTE_ADDR
Explanation
The syntax for storing IP address is
<?php
$userip = $_SERVER['REMOTE_ADDR'];
?>
Step-by-step solution
- IP address is stored in the REMOTE_ADDR server variable.
- PHP uses $_SERVER['REMOTE_ADDR'] to get the client's IP address.
- Other options like DOMAIN, GLOBALS, and _ENV do not store IP addresses.
- Correct answer is REMOTE_ADDR as it directly holds the user's IP.
- Final answer: B (REMOTE_ADDR).
Why the other options are wrong
- A. DOMAIN is not a PHP server variable that holds the client IP address.
- C. GLOBALS holds all global variables, not the client IP address.
- D. _ENV holds environment variables, not the client IP address.
Tip: Remember REMOTE_ADDR is used for client IP in PHP.
Question 7Medium
The function setcookie( ) is used to
- Enable or disable cookie support
- Declare cookie variables
- Store data in cookie variable
- All of above
Answer: C. Store data in cookie variable
Explanation
The setcookie() function sends an HTTP cookie to a client. A cookie is a variable, sent by the server to the browser. A cookie is typically a small text file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too
Step-by-step solution
- setcookie() sends data to the browser as a cookie.
- Cookies store information on the user's device.
- The function sets values in the cookie variable.
- It does not declare variables or enable/disable cookies.
- Thus, setcookie() is used to store data in cookie variable.
Why the other options are wrong
- A. Cookie support is controlled by browser and server settings, not by the setcookie() function.
- B. setcookie() does not declare cookie variables; it sends a cookie to the browser.
- D. All of above is wrong because setcookie() does not enable cookies or declare variables.
Tip: Remember: setcookie() only saves data, not declares variables or enables cookies.
Question 8Medium
Which of the following functions allows you to store session data in a database?
session_start();session_set_save_handler();mysql_query();- You cannot store session data in a database.
Answer: B. session_set_save_handler();
Explanation
Answer B is correct. You can use session_set_save_handler() to override PHP's default session-handling functions and store session data any way you want.
session_start() only activates PHP sessions for the current script. mysql_query() only executes a query with MySQL and does not affect the behavior of PHP's session mechanism.
Step-by-step solution
- The question asks for a function that stores session data in a database.
- session_set_save_handler() replaces PHP's default session storage with your own functions, which can write to a database.
- session_start() only starts the session for the script, and mysql_query() only runs a SQL query.
- So the answer is option B, session_set_save_handler().
Why the other options are wrong
- A. session_start() only starts the session; it does not choose where the data is stored.
- C. mysql_query() runs SQL but does not change how sessions are stored.
- D. This is wrong, because session_set_save_handler() lets you store session data in a database.
Tip: Override the session save handler to store session data anywhere you need.
Question 9Medium
Neglecting to set which of the following cookie will result in the cookie's domain being set to the host name of the server which generated it.
- session.domain
- session.path
- session.cookie_path
- session.cookie_domain
Answer: D. session.cookie_domain
Explanation
session.cookie_domain sets the domain that the session cookie is valid for. If it is not set, the cookie's domain becomes the host name of the server that sent it. The path settings control the path, not the domain.
Step-by-step solution
- session.cookie_domain defines the domain of the cookie.
- If not set, browser uses server's hostname as default.
- Other options do not control cookie domain.
- Thus, neglecting session.cookie_domain sets domain to server name.
- Final answer: session.cookie_domain.
Why the other options are wrong
- A. session.domain is not the cookie domain setting; that setting is session.cookie_domain.
- B. session.path does not set the cookie domain; it is not the cookie domain setting.
- C. session.cookie_path sets the path the cookie applies to, not the domain.
Tip: Remember: session.cookie_domain controls cookie domain; if missing, it defaults to server name.
Question 10Medium
Which one of the following is the default PHP session name?
- PHPSESSID
- PHPSESID
- PHPSESSIONID
- PHPIDSESS
Answer: A. PHPSESSID
Explanation
The default session name is PHPSESSID, the cookie name that holds the session ID. You can change it with the session.name setting.
Step-by-step solution
- Default PHP session name is set by session.name directive.
- The default value is 'PHPSESSID' as per PHP documentation.
- Checking options, 'PHPSESSID' matches option A.
- Therefore, the correct answer is A (PHPSESSID).
Why the other options are wrong
- B. PHPSESID is missing one S; the default session name is PHPSESSID.
- C. PHPSESSIONID is not the default; the default session name is PHPSESSID.
- D. PHPIDSESS is not the default; the default session name is PHPSESSID.
Tip: Remember the default PHP session name is always 'PHPSESSID'.
Practise all 15 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →