PHP
PHP
2. The attack which involves the insertion of malicious code into a page frequented by
other users is known as _______________
a) basic sql injection
b) advanced sql injection
c) cross-site scripting
d) scripting
Answer: c
Explanation: The cross-site scripting attack is among one of the top five security attacks
carried out across the Internet. It is also known as XSS, this attack is a type of code
injection attack which is made possible by incorrectly validating user data, which
usually gets inserted into the page through a web form or using an altered hyperlink.
3. When you use the $_GET variable to collect data, the data is visible to ___________
a) none
b) only you
c) everyone
d) selected few
Answer: c
Explanation: The information sent from a form with the method GET is visible to
everyone i.e. all variable names and values are displayed in the URL.
4. When you use the $_POST variable to collect data, the data is visible to ___________
a) none
b) only you
c) everyone
d) selected few
Answer: b
Explanation: The information sent from a form with the method POST is invisible to
others i.e. all names/values are embedded within the body of the HTTP request.
5. Which variable is used to collect form data sent with both the GET and POST
methods?
a) $BOTH
b) $_BOTH
c) $REQUEST
d) $_REQUEST
Answer: d
Explanation: In PHP the global variable $_REQUEST is used to collect data after
submitting an HTML form.
6. Which one of the following should not be used while sending passwords or other
sensitive information?
a) GET
b) POST
c) REQUEST
d) NEXT
Answer: a
Explanation: The information sent from a form with the method GET is visible to
everyone i.e. all variable names and values are displayed in the URL. So, it should not
be used while sending passwords or other sensitive information.
7. Which function is used to remove all HTML tags from a string passed to a form?
a) remove_tags()
b) strip_tags()
c) tags_strip()
d) tags_remove()
Answer: b
Explanation: The function strip_tags() is used to strip a string from HTML, XML, and PHP
tags.
8. What will be the value of the variable $input in the following PHP code?
1. <?php
2. $input = "Swapna<td>Lawrence</td>you are really<i>pretty</i>!";
3. $input = strip_tags($input,"<i></i>");
4. echo $input;
5. ?>
10. How many validation filters like FILTER_VALIDATE_EMAIL are currently available?
a) 5
b) 6
c) 7
d) 8
Answer: c
Explanation: There are seven validation filters. They are FILTER_VALIDATE_EMAIL,
FILTER_VALIDATE_BOOLEAN, FILTER_VALIDATE_FLOAT, FILTER_VALIDATE_INT,
FILTER_VALIDATE_IP, FILTER_VALIDATE_REGEXP, FILTER_VALIDATE_URL
Answer: b
Explanation: The variables PHP use to authenticate a user are
$_SERVER[‘PHP_AUTH_USER’] and $_SERVER[‘PHP_AUTH_PW’].
i) $_SERVER['PHP_AUTH_USER'].
ii) $_SERVER['PHP_AUTH_USERS'].
iii) $_SERVER['PHP_AUTH_PU'].
iv) $_SERVER['PHP_AUTH_PW'].
a) i) and ii)
b) ii) and iv)
c) i) and iv)
d) ii) and iii)
Answer: c
Explanation: $_SERVER[‘PHP_AUTH_USER’] and $_SERVER[‘PHP_AUTH_PW’] store the
username and password values, respectively.
3. Which of the following PHP function is commonly used when handling authentication
via PHP?
i) header()
ii) footer()
iii) inset()
iv) isset()
a) i) and iv)
b) ii) and iv)
c) ii) and iii)
d) i) and iii)
Answer: a
Explanation: The function isset () is used to check whether a variable is set or not and
the function header() sends a raw HTTP header to a client.
4. Which function is used to verify whether a variable contains a value?
a) header()
b) footer()
c) inset()
d) isset()
Answer: d
Explanation: The isset() function determines whether a variable has been assigned a
value. Its prototype follows: boolean isset(mixed var [,mixed var [,…]]).
Answer: c
8. Which function is used to split a string into a series of substrings, with each string
boundary is determined by a specific separator?
a) break()
b) divide()
c) explode()
d) md5()
Answer: c
Explanation: Although they are a similar function, you should use explode() instead of
split(). In fact split() function has been deprecated altogether.
i) small list
a) i) and iv)
b) i) and iii)
c) ii) and iii)
d) ii) and iv)
Answer: c
Explanation: Such requirements are better satisfied by implementing a database based
solution.
10. Which is the most powerful authentication method among the four?
a) Hard-coding a login pair directly into the script
b) File-based authentication
c) Data-based authentication
d) PEAR’S HTTP authentication
Answer: c
Explanation: It not only enhances administrative convenience and scalability but also
can be integrated into a larger database infrastructure.
Which of the following is used to collect form data submitted using the GET method?
(a) $_GET
(b) $_POST
(c) $REQUEST
(d) $_REQUEST
Explanation: The $_GET superglobal variable is used to collect form data submitted
using the GET method.
MCQ 2:
Which of the following is used to collect form data submitted using the POST method?
(a) $_GET
(b) $_POST
(c) $REQUEST
(d) $_REQUEST
MCQ 3:
Which of the following is used to collect form data submitted using both the GET and
POST methods?
(a) $_GET
(b) $_POST
(c) $REQUEST
(d) $_REQUEST
Explanation: The $REQUEST superglobal variable is used to collect form data submitted
using both the GET and POST methods.
MCQ 4:
(a) filter_var()
(b) validate_var()
(c) strip_tags()
MCQ 5:
Which of the following is used to protect against cross-site scripting (XSS) attacks?
(a) filter_var()
(b) validate_var()
(c) htmlspecialchars()
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
$errors = array();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($userCodeAnswer)) {
} else {
// Example validation:
// if (some_validation_condition) {
// }
if (empty($errors)) {
exit;
?>
<?php if (!empty($errors)) : ?>
<ul>
</ul>
</div>
<label>
</label><br>
<br>
</form>
</body>
</html>
Ques: Make a Form which takes Name, Email, Age and Users website as input from the
user and perform validation and sanitization on the data in another file.
<!DOCTYPE html>
<html>
<head>
<title>Form Validation and Sanitization</title>
</head>
<body>
<form action="validation.php" method="post">
<input type="text" name="name"
placeholder="Your name">
<input type="email" name="email"
placeholder="Your email">
<input type="number" name="age"
placeholder="Your age">
<input type="url" name="website"
placeholder="Your website">
<input type="submit" value="Submit">
</form>
</body>
</html>
Now in a new file validation.php, let’s
handle the data posted by form.
<?php
// Get the form data
$name = $_POST['name'];
$email = $_POST['email'];
$age = $_POST['age'];
$website = $_POST['website'];
if (empty($name)) {
} else {
?>