Free Website And Web Server Programming Question and Answers — Questions and Answers
Question 1: Use the PHP function _______ to establish a connection to a MySQL database server.
- Mysqli_connect() (Correct answer)
- Open_connect()
- Open_mysqli()
- Connection_mysqli()
Correct answer: Mysqli_connect()
In PHP, the `mysqli_connect()` function is specifically designed and used to establish a connection to a MySQL database server. It takes parameters such as the hostname, username, password, and database name to create a successful connection for subsequent database operations, forming the foundation for interacting with MySQL.
Question 2: What syntax should be used to create a temporary cookie with the value "blue"?
- Setcookie("blue");
- Setcookie("color","blue"); (Correct answer)
- $Color = setcookie("blue");
- Setcookie("blue","color");
Correct answer: Setcookie("color","blue");
In PHP, the `setcookie()` function is used to send an HTTP cookie to the client's browser. The correct syntax requires at least two arguments: the name of the cookie and its value. Therefore, `setcookie("color", "blue");` correctly creates a cookie named "color" with the value "blue".
Question 3: How would you access a virtual site you created at port 8000 in a browser?
- Http://localhost.8000
- Http://localhost:8000 (Correct answer)
- Http://localhost8000
- None of the above
Correct answer: Http://localhost:8000
When accessing a web server or virtual site running on your local machine (localhost) on a specific port, the standard URL format is `http://localhost:port_number`. The colon `:` correctly separates the hostname from the port number, making `http://localhost:8000` the proper way to access a site listening on port 8000.
Question 4: Information about the session status is kept in the $_Session _______
- Cookie
- Function
- Script
- Autoglobal (Correct answer)
Correct answer: Autoglobal
In PHP, `$_SESSION` is a superglobal (also known as an autoglobal) array used to store session variables. Superglobals are built-in variables that are always available in all scopes throughout a script, making `$_SESSION` the mechanism for managing session-specific data across multiple page requests for a single user.
Question 5: A ______ can be added to the end of a URL as a technique to keep data safe after a visitor has seen the page.
- Auto-global
- Query string (Correct answer)
- Identifier
- Function
Correct answer: Query string
A query string is a part of a URL that allows data to be passed from one web page to another, typically after a question mark (?). It consists of key-value pairs and is commonly used to maintain state, filter content, or track user information across stateless HTTP requests. While not a security mechanism, it helps persist specific data related to a visitor's interaction with a page.
Question 6: Which of the following does not qualify as a PHP state management tool?
- Cookies
- Hidden form fields
- Query strings
- Functions (Correct answer)
Correct answer: Functions
PHP state management tools are techniques used to maintain data across multiple, otherwise stateless, HTTP requests. Cookies, hidden form fields, and query strings are all common methods for storing and retrieving user-specific or session data. Functions, however, are blocks of reusable code that perform specific tasks within a script and do not inherently manage or store state across different web requests.
Question 7: The setcookie() method in PHP takes a necessary ________ parameter and five optional arguments to construct a cookie.
- Path
- Expires
- Value
- Name (Correct answer)
Correct answer: Name
The `setcookie()` method in PHP is used to send an HTTP cookie to the client's browser. The most fundamental and necessary parameter it requires is the `name` of the cookie, which is a string identifier. While other parameters like `value`, `expires`, `path`, `domain`, `secure`, and `httponly` are crucial for defining the cookie's behavior and scope, they are all optional.
Use the PHP function _______ to establish a connection to a MySQL database server.