Imagine you're building a multi-step form in PHP. At the third step, you need to access data from the first step without re-submitting the form. Which PHP feature would allow you to do this?
- Sessions
- Cookies
- Hidden Fields
- Server Variables
Using Hidden Fields allows data from a previous step to be included in the form without resubmitting, enabling continuity in a multi-step process.
What is the primary purpose of using PHP's htmlspecialchars() function in form handling?
- To store form data securely in the database
- To remove special characters from user input
- To convert user input into HTML entities
- To encrypt sensitive data
PHP's htmlspecialchars() function converts special characters to their HTML entities, preventing Cross-Site Scripting (XSS) attacks by rendering user input harmless.
The base class for all exceptions in PHP is ________.
- Exception
- Throwable
- Error
- RuntimeException
In PHP, the base class for all exceptions is 'Throwable.' It serves as the base class for both exceptions and errors.
Which PHP superglobal contains information about headers, paths, and script locations?
- $_SERVER
- $_ENV
- $_GLOBALS
- $_LOCALS
The $_SERVER superglobal in PHP contains an array of information about the server environment, including headers, paths, and script locations. It's often used for server-related tasks.
Which of the following PHP functions checks if a file or directory exists?
- file_exists()
- include_once()
- require_once()
- class_exists()
The file_exists() function in PHP is used to determine if a file or directory exists. It returns a boolean value, true if the file or directory exists and false if it doesn't.
You have a string "OpenAI" and you want to replace "Open" with "Close". Which PHP function will you use?
- str_replace("Open", "Close", $str)
- str_replace("Close", "Open", $str)
- str_concat("Open", "Close", $str)
- str_split("Open", "Close", $str)
To replace a substring in a string, you should use the str_replace function in PHP. It replaces all occurrences of the specified substring with another string within the given string.
What is the output of the following code? echo count(array("apple", "banana", "cherry"));
- 3
- 6
- 0
- 1
The count function in PHP returns the number of elements in an array. In this case, it counts the elements in the given array, which is 3. So, the output will be 3.
Which of the following is a common technique used to prevent cross-site scripting (XSS) attacks?
- Input validation
- Using strong passwords
- Escaping user-generated content
- Storing user data in plain text format
To prevent cross-site scripting (XSS) attacks, it's common to escape user-generated content before rendering it in web pages. This prevents code execution.
You are auditing a web application and notice that the session IDs are predictable and sequential. Why might this be a security concern?
- Session fixation attack
- XSS attack
- CSRF attack
- SQL injection attack
Predictable and sequential session IDs make the application vulnerable to a session fixation attack, where an attacker can set a user's session ID. This poses a significant security risk. Other attacks like XSS, CSRF, and SQL injection are unrelated to session ID predictability.
How can you pass a variable by reference to a function in PHP?
- Using the '&' symbol before the variable
- Using the 'ref' keyword before the variable
- Prefixing the variable with a '#' symbol
- Using the '*' symbol before the variable
To pass a variable by reference in PHP, you use the '&' symbol before the variable name. This allows changes made to the variable within the function to affect the original variable outside the function. The other options are incorrect.