Session and cookies Questions with Answers

15 practice questions on Session and cookies (PHP) with answers and step-by-step solutions. Try 10 samples below, then practise all 15 free in the AptiNest app.

Sample questions

Question 1Medium

Which one of the following is the very first task executed by a session enabled page?

  1. Delete the previous session
  2. Start a new session
  3. Check whether a valid session exists
  4. 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

  1. The question asks what a session enabled page does first.
  2. The source says the page first checks whether a valid session exists.
  3. Only when a valid session exists does the next task run.
  4. 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....

  1. 3600 sec
  2. 3600 min
  3. 3600 hrs
  4. 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

  1. session.cookie_lifetime is given in seconds.
  2. 3600 seconds is one hour.
  3. 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?

  1. start_session()
  2. session_start()
  3. session_begin()
  4. 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

  1. PHP requires session_start() to initialize a session before any HTML.
  2. This function must be called at the very start of the PHP script.
  3. Other options like start_session() or begin_session() are not valid PHP functions.
  4. session_start() is the correct and only valid function to start a session.
  5. 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?

  1. 1800
  2. 3600
  3. 1440
  4. 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

  1. The session data lifetime is set by session.gc_maxlifetime.
  2. Its default value is 1440 seconds, which is 24 minutes.
  3. 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[]?

  1. Used to register a global variable
  2. Used to initialize a session.
  3. Used to store variables of the current session
  4. 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

  1. The SESSION superglobal array stores data for the current user session.
  2. Values are saved as key and value pairs and read back on later pages of the same session.
  3. 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?

  1. DOMAIN
  2. REMOTE_ADDR
  3. GLOBALS
  4. _ENV

Answer: B. REMOTE_ADDR

Explanation

The syntax for storing IP address is

<?php
$userip = $_SERVER['REMOTE_ADDR'];
?>

Step-by-step solution

  1. IP address is stored in the REMOTE_ADDR server variable.
  2. PHP uses $_SERVER['REMOTE_ADDR'] to get the client's IP address.
  3. Other options like DOMAIN, GLOBALS, and _ENV do not store IP addresses.
  4. Correct answer is REMOTE_ADDR as it directly holds the user's IP.
  5. 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

  1. Enable or disable cookie support
  2. Declare cookie variables
  3. Store data in cookie variable
  4. 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

  1. setcookie() sends data to the browser as a cookie.
  2. Cookies store information on the user's device.
  3. The function sets values in the cookie variable.
  4. It does not declare variables or enable/disable cookies.
  5. 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?

  1. session_start();
  2. session_set_save_handler();
  3. mysql_query();
  4. 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

  1. The question asks for a function that stores session data in a database.
  2. session_set_save_handler() replaces PHP's default session storage with your own functions, which can write to a database.
  3. session_start() only starts the session for the script, and mysql_query() only runs a SQL query.
  4. 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.

  1. session.domain
  2. session.path
  3. session.cookie_path
  4. 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

  1. session.cookie_domain defines the domain of the cookie.
  2. If not set, browser uses server's hostname as default.
  3. Other options do not control cookie domain.
  4. Thus, neglecting session.cookie_domain sets domain to server name.
  5. 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?

  1. PHPSESSID
  2. PHPSESID
  3. PHPSESSIONID
  4. 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

  1. Default PHP session name is set by session.name directive.
  2. The default value is 'PHPSESSID' as per PHP documentation.
  3. Checking options, 'PHPSESSID' matches option A.
  4. 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 →

Related topics

See all PHP topics · All topics