PHP Cookies Aptitude: This section contains aptitude questions and answers on Cookies.
By Nidhi Last updated : December 15, 2023
1) There are the following statements that are given below, which of them are correct about cookies in PHP?
- A cookie is used to identify the user.
- A cookie is a file which is stores in the user's computer by the web applications.
- The cookies are used to improve the performance of web applications.
- In PHP, we can create cookies and we can also retrieve data from cookies.
Options:
- A and B
- A and C
- B and C
- A, B, C, and D
Correct answer: 4
A, B, C, and D
Explanation:
All given statements are correct about the cookies.
2) Which of the following function is used to save a cookie in the user's computer?
- savecookie()
- createcookie()
- create_cookie()
- setcookie()
Correct answer: 4
setcookie()
Explanation:
The setcookie() function is used to save a cookie in the user's computer, the syntax of setcookie() function is given below:
setcookie(name, value, expire, path, domain, secure, httponly);
In the above syntax, the name parameter is mandatory and others are optional.
3) How can we retrieve information from the specified cookie?
- Using getcookie() function
- Using readcookie() function
- Using $_COOKIE superglobal variable
- None of the above
Correct answer: 3
Using $_COOKIE superglobal variable
Explanation:
The $_COOKIE superglobal variable is used to retrieve the value of the specified cookie, we can print cookie value using the below statement:
echo $_COOKIE[$cookie_name];
4) Which of the following function is used to check the cookie is set or not?
- iscookie_set()
- is_cookie_set()
- isset()
- is_set()
Correct answer: 3
isset()
Explanation:
The isset() function is used to check the cookie is set or not. We can understand the use of isset() function using below example:
<?php
if (!isset($_COOKIE["ABC"]))
{
echo "Cookie is not set!";
}
else
{
echo "Value: " . $_COOKIE["ABC"];
}
?>
Here, ABC is the name of the cookie.
5) Is it true that the setcookie() function must be called before the HTML tag in the PHP script?
- Yes
- No
Correct answer: 1
Yes
Explanation:
Yes, the setcookie() function must be called before the HTML tag in the PHP script.
6) What is the default expire time of a cookie, if not specified in the setcookie() function?
- 1 day
- 1 week
- 8 hours
- When session expires
Correct answer: 4
When session expires
Explanation:
If we do not specify cookie expire time then it will expire automatically when the session end or browser gets closed.
7) Can we modify the already created cookie in PHP?
- Yes
- No
Correct answer: 1
Yes
Explanation:
Yes, we can modify an already created cookie by setting again using the setcookie() function.
8) Which of the following function is used to delete a cookie?
- setcookie()
- delete_cookie()
- deletecookie()
- removecookie()
Correct answer: 1
setcookie()
Explanation:
We can delete a specified cookie by setting past time. For example:
setcookie("cookie1", "", time() - 3600);
Here, we set cookie expire time one hour ago.
9) Which of the following function is used to count cookie in PHP?
- count()
- cookie_count()
- total_cookie()
- None of the above
Correct answer: 1
count()
Explanation:
The count() function is used to count the total number of cookies like this:
count($_COOKIE);
10) Can we set multiple values in a single cookie in PHP?
- Yes
- No
Correct answer: 2
No
Explanation:
No, we cannot set multiple values in a single cookie directly, but we can set multiple values by concatenating them.
setcookie("mycookie", $username . ":" . $password);
Here, we set username and password both in a single cookie "mycookie" by concatenating them.