Don't forget to hit the ⭐ if you like this repo.
Please answer the following 50 multiple-choice questions to test your knowledge in Form & Session & Cookies. You must place your answer file in the submission folder. Within the submission/Q6/set_a
folder, create a folder called your github_id
.
-
Which PHP superglobal variable is used to retrieve form data sent with the HTTP GET method?
- a) $_GET
- b) $_POST
- c) $_REQUEST
- d) $_SERVER
-
How can you start a new session in PHP?
- a) session_open()
- b) session_start()
- c) start_session()
- d) open_session()
-
Which function is used to set a cookie in PHP?
- a) set_cookie()
- b) create_cookie()
- c) setcookie()
- d) createcookie()
-
Which PHP superglobal variable is used to access session data?
- a) $_SESSION
- b) $_COOKIE
- c) $_REQUEST
- d) $_SERVER
-
How can you destroy a session in PHP?
- a) session_destroy()
- b) destroy_session()
- c) unset_session()
- d) end_session()
-
Which function is used to check if a cookie is set in PHP?
- a) isset_cookie()
- b) cookie_isset()
- c) cookie_set()
- d) isset($_COOKIE['cookie_name'])
-
How can you retrieve the value of a specific cookie in PHP?
- a) $_COOKIE['cookie_name']
- b) $_SESSION['cookie_name']
- c) $_GET['cookie_name']
- d) $_POST['cookie_name']
-
How can you unset a specific session variable in PHP?
- a) unset($_SESSION['variable_name'])
- b) unset_session('variable_name')
- c) session_unset('variable_name')
- d) session_unset($_SESSION['variable_name'])
-
How can you set the expiration time for a cookie in PHP?
- a) By using the
set_cookie_expiration()
function - b) By specifying the expiration time in the
setcookie()
function - c) By setting the
$_COOKIE_EXPIRATION
constant - d) Cookies do not have an expiration time in PHP
- a) By using the
-
How can you check if a session variable is set in PHP?
- a) isset($_SESSION['variable_name'])
- b) session_isset('variable_name')
- c) isset_session('variable_name')
- d) isset_variable('variable_name')
-
Which PHP function is used to sanitize user input to prevent security vulnerabilities?
- a) sanitize_input()
- b) secure_input()
- c) filter_input()
- d) clean_input()
-
How can you retrieve the number of elements in an array stored in a session variable?
- a) count($_SESSION['array_variable'])
- b) sizeof($_SESSION['array_variable'])
- c) length($_SESSION['array_variable'])
- d) lengthof($_SESSION['array_variable'])
-
How can you redirect the user to another page using PHP?
- a) redirect('new_page.php')
- b) header('Location: new_page.php')
- c) go_to('new_page.php')
- d) change_page('new_page.php')
-
How can you retrieve the current session ID in PHP?
- a) $_SESSION_ID
- b) session_id()
- c) get_session_id()
- d) current_session_id()
-
Which function is used to encode special characters in a string to prevent cross-site scripting (XSS) attacks?
- a) strip_tags()
- b) htmlspecialchars()
- c) htmlentities()
- d) escape_chars()
-
How can you retrieve the value of a specific query string parameter in PHP?
- a) $_GET['parameter_name']
- b) $_REQUEST['parameter_name']
- c) $_POST['parameter_name']
- d) $_QUERY['parameter_name']
-
How can you retrieve the IP address of the client accessing your PHP script?
- a) $_SERVER['IP_ADDRESS']
- b) $_CLIENT_IP
- c) $_SERVER['REMOTE_ADDR']
- d) $_USER_IP
-
How can you store an array in a session variable in PHP?
- a) $_SESSION['array_variable'] = array(...)
- b) $_SESSION['array_variable'][] = ...
- c) $_SESSION[] = array_variable(...)
- d) session_array_variable(...) = array(...)
-
How can you prevent a form from being submitted if certain conditions are not met using JavaScript and PHP?
- a) By using the
preventDefault()
function in JavaScript and validating the form on the server-side using PHP - b) By using the
return false
statement in JavaScript and validating the form on the server-side using PHP - c) By using the
onsubmit
attribute in the HTML form tag and validating the form on the server-side using PHP - d) By using the
stopPropagation()
function in JavaScript and validating the form on the server-side using PHP
- a) By using the
-
How can you check if a specific cookie is set in PHP?
- a) isset($_COOKIE['cookie_name'])
- b) cookie_isset('cookie_name')
- c) cookie_set('cookie_name')
- d) isset_cookie('cookie_name')
-
How can you display a success message after submitting a form using PHP?
- a) By echoing a success message in the PHP script that processes the form
- b) By using the
header()
function to redirect to a success page with a success message - c) By setting a session variable with a success message and displaying it on the redirected page
- d) By using the
print_success_message()
function in PHP to display the message
-
How can you delete a specific cookie in PHP?
- a) setcookie('cookie_name', '', time() - 3600);
- b) delete_cookie('cookie_name');
- c) unset($_COOKIE['cookie_name']);
- d) cookie_delete('cookie_name');
-
How can you check if a form input field is empty using PHP?
- a) if ($_POST['field_name'] === '')
- b) if ($_POST['field_name'] == '')
- c) if (empty($_POST['field_name']))
- d) if (isset($_POST['field_name']))
-
How can you retrieve the value of a specific session variable in PHP?
- a) $_SESSION['variable_name']
- b) get_session_variable('variable_name')
- c) session_get('variable_name')
- d) $_SESSION_GET['variable_name']
-
How can you prevent cross-site scripting (XSS) attacks when displaying user input on a webpage in PHP?
- a) By using the
strip_tags()
function to remove HTML tags from the input - b) By using the
htmlspecialchars()
function to encode special characters in the input - c) By using the
htmlentities()
function to encode special characters in the input - d) By using the
validate_input()
function to validate the input against a set of rules
- a) By using the
-
How can you check if a session is active in PHP?
- a) if (session_status() === PHP_SESSION_ACTIVE)
- b) if (session_active())
- c) if (isset($_SESSION))
- d) if (session_active() === true)
-
How can you set the lifetime of a session in PHP?
- a) By setting the
session_lifetime
configuration directive in the php.ini file - b) By using the
session_set_lifetime()
function - c) By setting the
session.cookie_lifetime
configuration directive in the php.ini file - d) By using the
set_session_lifetime()
function
- a) By setting the
-
How can you retrieve the value of a specific form input field using PHP?
- a) $_POST['input_field_name']
- b) $_REQUEST['input_field_name']
- c) $_GET['input_field_name']
- d) $_FORM['input_field_name']
-
How can you set a session variable in PHP?
- a) $_SESSION['variable_name'] = value;
- b) set_session_variable('variable_name', value);
- c) session_set('variable_name', value);
- d) $_SET['variable_name'] = value;
-
How can you display the current session ID in PHP?
- a) echo session_id();
- b) echo $_SESSION_ID;
- c) echo get_session_id();
- d) echo current_session_id();
-
How can you prevent a form from being submitted multiple times using PHP?
- a) By disabling the submit button after the form is submitted
- b) By using the
header()
function to redirect after the form is submitted - c) By setting a session variable after the form is submitted and checking its value on subsequent submissions
- d) By using JavaScript to validate and disable the form submission button
-
How can you retrieve the value of a specific cookie using PHP?
- a) $_COOKIE['cookie_name']
- b) get_cookie('cookie_name')
- c) cookie_get('cookie_name')
- d) $_COOKIE_GET['cookie_name']
-
How can you store a value in a cookie that will expire in one hour using PHP?
- a) setcookie('cookie_name', 'value', time() + 3600);
- b) create_cookie('cookie_name', 'value', time() + 3600);
- c) $_COOKIE['cookie_name'] = 'value'; (The expiration time is set automatically)
- d) set_cookie_expiration('cookie_name', 'value', 3600);
-
How can you check if a specific session variable is set in PHP?
- a) isset($_SESSION['variable_name'])
- b) session_isset('variable_name')
- c) isset_session('variable_name')
- d) isset_variable('variable_name')
-
How can you prevent form injection attacks in PHP?
- a) By using prepared statements and parameterized queries when interacting with a database
- b) By validating and sanitizing user input before processing it
- c) By implementing CAPTCHA to ensure the user is not a bot
- d) By encrypting sensitive form data before storing it
-
How can you unset all session variables in PHP?
- a) session_unset()
- b) unset_session_variables()
- c) $_SESSION = array()
- d) destroy_session()
-
How can you store an array of values in a cookie using PHP?
- a) By serializing the array and storing it in a single cookie value
- b) By creating multiple cookies, each
storing a single value from the array - c) By converting the array to JSON and storing it in a cookie - d) Cookies cannot store arrays in PHP
-
How can you retrieve the value of a specific form input field using the POST method in PHP?
- a) $_POST['input_field_name']
- b) $_REQUEST['input_field_name']
- c) $_GET['input_field_name']
- d) $_FORM['input_field_name']
-
How can you retrieve the value of a specific session variable using PHP?
- a) $_SESSION['variable_name']
- b) get_session_variable('variable_name')
- c) session_get('variable_name')
- d) $_SESSION_GET['variable_name']
-
How can you clear all cookies in PHP?
- a) By setting each cookie to an empty value using the
setcookie()
function - b) By using the
clear_cookies()
function - c) By using the
unset($_COOKIE)
statement - d) Cookies cannot be cleared programmatically
- a) By setting each cookie to an empty value using the
-
How can you store a value in a session variable using PHP?
- a) $_SESSION['variable_name'] = value;
- b) set_session_variable('variable_name', value);
- c) session_set('variable_name', value);
- d) $_SET['variable_name'] = value;
-
How can you check if a specific cookie is set in PHP?
- a) isset($_COOKIE['cookie_name'])
- b) cookie_isset('cookie_name')
- c) cookie_set('cookie_name')
- d) isset_cookie('cookie_name')
-
How can you retrieve the value of a specific session variable using the GET method in PHP?
- a) $_GET['variable_name']
- b) $_REQUEST['variable_name']
- c) $_SESSION['variable_name']
- d) $_SESSION_GET['variable_name']
-
How can you delete a specific cookie in PHP?
- a) setcookie('cookie_name', '', time() - 3600);
- b) delete_cookie('cookie_name');
- c) unset($_COOKIE['cookie_name']);
- d) cookie_delete('cookie_name');
-
How can you check if a form input field is empty using PHP?
- a) if ($_POST['field_name'] === '')
- b) if ($_POST['field_name'] == '')
- c) if (empty($_POST['field_name']))
- d) if (isset($_POST['field_name']))
-
How can you retrieve the current session ID in PHP?
- a) session_id()
- b) $_SESSION_ID
- c) get_session_id()
- d) current_session_id()
-
How can you set a cookie that is accessible only through HTTP and not JavaScript in PHP?
- a) setcookie('cookie_name', 'value', time() + 3600, '/', '', false, true);
- b) setcookie('cookie_name', 'value', time() + 3600, '/', '', true, false);
- c) setcookie('cookie_name', 'value', time() + 3600, '/', '', true, true);
- d) setcookie('cookie_name', 'value', time() + 3600, '/', '', false, false);
-
How can you retrieve the client's browser name and version in PHP?
- a) $_SERVER['HTTP_USER_AGENT']
- b) $_BROWSER['user_agent']
- c) $_CLIENT_AGENT
- d) get_browser()
-
How can you store user preferences using cookies in PHP?
- a) By storing the preferences in a JSON-encoded string and setting it as a cookie value
- b) By creating a separate cookie for each preference
- c) By storing the preferences in a session variable instead of a cookie
- d) Cookies cannot be used to store user preferences
-
How can you retrieve the number of elements in an array stored in a session variable in PHP?
- a) count($_SESSION['array_variable'])
- b) sizeof($_SESSION['array_variable'])
- c) length($_SESSION['array_variable'])
- d) lengthof($_SESSION['array_variable'])
Please answer the following 50 multiple-choice questions to test your knowledge in Form & Session & Cookies. You must place your answer file in the submission folder. Within the submission/Q6/set_b
folder, create a folder called your github_id
.
-
Which superglobal variable is used to retrieve data from a form submitted using the POST method?
- a)
$_POST
- b)
$_GET
- c)
$_REQUEST
- d)
$_SERVER
- a)
-
How can you check if a form has been submitted in PHP?
- a) By checking if
$_POST
or$_GET
is not empty - b) By checking if
$_SERVER['REQUEST_METHOD']
is equal to 'POST' - c) By checking if
$_REQUEST
contains form data - d) By checking if
$_COOKIE
is set
- a) By checking if
-
Which function is used to start a session in PHP?
- a)
session_start()
- b)
session_open()
- c)
session_init()
- d)
session_begin()
- a)
-
How can you set a session variable in PHP?
- a) By assigning a value to
$_SESSION['variable']
- b) By using the
set_session()
function - c) By using the
session_set()
function - d) By assigning a value to
$_COOKIE['variable']
- a) By assigning a value to
-
What is the purpose of the
session_destroy()
function in PHP?- a) To destroy all session data
- b) To remove a specific session variable
- c) To close the session and write session data
- d) To regenerate the session ID
-
How can you retrieve the value of a session variable in PHP?
- a) By accessing
$_SESSION['variable']
- b) By using the
get_session()
function - c) By accessing
$_COOKIE['variable']
- d) By using the
retrieve_session()
function
- a) By accessing
-
Which function is used to set a cookie in PHP?
- a)
setcookie()
- b)
create_cookie()
- c)
store_cookie()
- d)
cookie_set()
- a)
-
How can you retrieve the value of a cookie in PHP?
- a) By accessing
$_COOKIE['cookieName']
- b) By using the
get_cookie()
function - c) By accessing
$_SESSION['cookieName']
- d) By using the
retrieve_cookie()
function
- a) By accessing
-
How can you delete a cookie in PHP?
- a) By setting the cookie with an empty value and an expired timestamp
- b) By using the
delete_cookie()
function - c) By accessing
$_COOKIE['cookieName']
and assigning it a null value - d) By using the
unset_cookie()
function
-
What is the purpose of the
htmlspecialchars()
function in PHP when working with form input?- a) To convert special characters to HTML entities
- b) To validate form input against a specified pattern
- c) To sanitize form input and prevent SQL injection
- d) To encode form input in Base64 format
-
Which PHP function is used to check if a session variable is set?
- a)
isset()
- b)
empty()
- c)
session_isset()
- d)
session_exists()
- a)
-
How can you prevent cross-site scripting (XSS) attacks when displaying user input on a webpage?
- a) By using
htmlspecialchars()
to encode the output - b) By using
strip_tags()
to remove HTML tags - c) By using
preg_match()
to validate the input - d) By using
urlencode()
to encode the output
- a) By using
-
Which PHP function is used to check if a cookie is set?
- a)
isset()
- b)
empty()
- c)
cookie_isset()
- d)
cookie_exists()
- a)
-
How can you restrict the lifetime of a cookie in PHP?
- a) By setting the cookie with an expiration timestamp in the future
- b) By setting the cookie with an expiration timestamp in the past
- c) By using the
setcookie_lifetime()
function - d) By using the
cookie_set_lifetime()
function
-
What is the purpose of the
session_regenerate_id()
function in PHP?- a) To regenerate the session ID to prevent session fixation attacks
- b) To retrieve the current session ID
- c) To destroy the current session and start a new one
- d) To update the session data with a new ID
-
How can you store an array in a session variable in PHP?
- a) By assigning the array to
$_SESSION['variable']
- b) By using the
serialize()
function before assigning the array - c) By using the
json_encode()
function before assigning the array - d) By converting the array to a string using
implode()
before assigning it
- a) By assigning the array to
-
Which PHP function is used to check if a form input field is empty?
- a)
empty()
- b)
isset()
- c)
is_null()
- d)
is_empty()
- a)
-
How can you retrieve the value of a selected option from a dropdown menu in PHP?
- a) By accessing
$_POST['fieldname']
- b) By using the
get_option()
function - c) By accessing
$_REQUEST['fieldname']
- d) By using the
retrieve_option()
function
- a) By accessing
-
What is the purpose of the
session_write_close()
function in PHP?- a) To close the session and write session data
- b) To destroy all session data
- c) To start a new session
- d) To regenerate the session ID
-
How can you prevent session fixation attacks in PHP?
- a) By regenerating the session ID after successful login
- b) By using HTTPS for secure session communication
- c) By validating session data against a predefined pattern
- d) By encrypting session data using a secret key
-
Which PHP function is used to retrieve the current session ID?
- a)
session_id()
- b)
session_get_id()
- c)
session_current_id()
- d)
session_retrieve_id()
- a)
-
How can you destroy a specific session variable in PHP?
- a) By using the
unset()
function on$_SESSION['variable']
- b) By using the
destroy_session_variable()
function - c) By setting
$_SESSION['variable']
to null - d) By using the
remove_session_variable()
function
- a) By using the
-
What is the purpose of the
session_unset()
function in PHP?- a) To remove all session variables
- b) To remove a specific session variable
- c) To close the session and write session data
- d) To regenerate the session ID
-
How can you set a cookie with an expiration date in the past to delete it in PHP?
- a) By setting the cookie with an expiration timestamp in the past
- b) By using the
delete_cookie()
function - c) By accessing
$_COOKIE['cookieName']
and assigning it a null value - d) By using the
unset_cookie()
function
-
What is the purpose of the
session_id()
function in PHP?- a) To retrieve the current session ID
- b) To generate a new session ID
- c) To validate a session ID against a predefined pattern
- d) To update the session data with a new ID
-
How can you check if a session variable is empty in PHP?
- a) By using
empty($_SESSION['variable'])
- b) By using
isset($_SESSION['variable'])
- c) By using
is_null($_SESSION['variable'])
- d) By using
$_SESSION['variable'] == ''
- a) By using
-
Which PHP function is used to set a session timeout?
- a)
session_set_timeout()
- b)
session_timeout()
- c)
set_session_timeout()
- d)
ini_set('session.gc_maxlifetime')
- a)
-
How can you check if a cookie is empty in PHP?
- a) By using
empty($_COOKIE['cookieName'])
- b) By using
isset($_COOKIE['cookieName'])
- c) By using
is_null($_COOKIE['cookieName'])
- d) By using
$_COOKIE['cookieName'] == ''
- a) By using
-
What is the purpose of the
urlencode()
function in PHP?- a) To encode a string for safe use in a URL
- b) To convert special characters to HTML entities
- c) To sanitize form input and prevent SQL injection
- d) To encrypt a string using a secret key
-
How can you retrieve the number of items in a session array in PHP?
- a) By using the
count()
function on$_SESSION['array']
- b) By using the
sizeof()
function on$_SESSION['array']
- c) By accessing
$_SESSION['array']['length']
- d) By using the
get_array_length()
function
- a) By using the
-
Which PHP function is used to check if a session is active?
- a)
session_is_active()
- b)
session_status()
- c)
session_active()
- d)
session_check()
- a)
-
How can you check if a checkbox is checked in PHP?
- a) By using
isset($_POST['checkboxName'])
- b) By using
empty($_POST['checkboxName'])
- c) By using
$_POST['checkboxName'] == 'checked'
- d) By using
is_null($_POST['checkboxName'])
- a) By using
-
What is the purpose of the
session_commit()
function in PHP?- a) To close the session and write session data
- b) To destroy all session data
- c) To start a new session
- d) To regenerate the session ID
-
How can you store multiple values in a single cookie in PHP?
- a) By serializing an array and assigning it to the cookie value
- b) By using
setcookie()
multiple times with different cookie names - c) By concatenating the values and assigning them to the cookie value
- d) By using the
set_cookie_array()
function
-
Which PHP function is used to check if a session has been started?
- a)
session_is_started()
- b)
session_status()
- c)
session_started()
- d)
session_check_started()
- a)
-
How can you prevent session hijacking in PHP?
- a) By using a secure session ID generated with a secret key
- b) By encrypting session data using a private key
- c) By validating session data against a predefined pattern
- d) By using CSRF tokens for form submissions
-
How can you check if a session is empty in PHP?
- a) By using
empty($_SESSION)
- b) By using
isset($_SESSION)
- c) By using
is_null($_SESSION)
- d) By using
$_SESSION == ''
- a) By using
-
What is the purpose of the
session_abort()
function in PHP?- a) To discard session changes and close the session
- b) To destroy all session data
- c) To regenerate the session ID
- d) To update the session data with a new ID
-
How can you restrict the access to a specific page to only logged-in users in PHP?
- a) By checking if a session variable representing the user's login status is set
- b) By using the
authenticate_user()
function - c) By validating the user's credentials against a database
- d) By encrypting the page content with a private key
-
Which PHP function is used to check if a cookie domain is secure?
- a)
is_cookie_secure()
- b)
cookie_domain_secure()
- c)
cookie_is_secure()
- d)
$_COOKIE['secure']
- a)
-
How can you prevent session data from being accessible by JavaScript in PHP?
- a) By setting the
HttpOnly
flag when setting session cookies - b) By using JavaScript to encrypt the session data
- c) By validating session data against a predefined pattern
- d) By using AJAX to retrieve session data asynchronously
- a) By setting the
-
What is the purpose of the
session_gc()
function in PHP?- a) To perform garbage collection on expired session data
- b) To retrieve the current session ID
- c) To destroy the current session and start a new one
- d) To update the session data with a new ID
-
How can you set a session timeout in PHP?
- a) By modifying the
session.gc_maxlifetime
configuration directive - b) By using the
set_session_timeout()
function - c) By setting
$_SESSION['timeout']
to a specific value - d) By using the
session_set_timeout()
function
- a) By modifying the
-
Which PHP function is used to retrieve the value of a cookie?
- a)
$_COOKIE['cookieName']
- b)
retrieve_cookie('cookieName')
- c)
get_cookie_value('cookieName')
- d)
cookie_value('cookieName')
- a)
-
How can you restrict the access to a specific page to only users with admin privileges in PHP?
- a) By checking if a session variable representing the user's admin status is set to true
- b) By using the
validate_admin_user()
function - c) By validating the user's credentials against an admin database
- d) By encrypting the page content with an admin key
-
What is the purpose of the
session_destroy()
function in PHP?- a) To destroy all session data
- b) To remove a specific session variable
- c) To close the session and write session data
- d) To regenerate the session ID
-
How can you retrieve the value of a specific cookie in PHP?
- a) By accessing
$_COOKIE['cookieName']
- b) By using the
get_cookie_value('cookieName')
function - c) By using
retrieve_cookie('cookieName')
- d) By using the
cookie_value('cookieName')
function
- a) By accessing
-
Which PHP function is used to set the domain for a cookie?
- a)
set_cookie_domain()
- b)
cookie_set_domain()
- c)
cookie_domain()
- d)
setcookie()
function with thedomain
parameter
- a)
-
How can you prevent session fixation attacks in PHP?
- a) By regenerating the session ID after successful login
- b) By using HTTP-only cookies for session management
- c) By validating session data against a predefined pattern
- d) By using CSRF tokens for form submissions
-
What is the purpose of the
session_save_path()
function in PHP?- a) To retrieve the current session save path
- b) To set the save path for session files
- c) To destroy the current session and start a new one
- d) To update the session data with a new ID
Please create an Issue for any improvements, suggestions or errors in the content.
You can also contact me using Linkedin for any other queries or feedback.