PHP Communication & Stakeholder Relations 5 — Questions and Answers
Question 1: Which PHP session function must be called before accessing $_SESSION variables?
- session_register()
- session_start() (Correct answer)
- session_open()
- session_init()
Correct answer: session_start()
session_start() initializes the session mechanism and must be called before any output or session variable access.
Question 2: What is the best practice for passing sensitive API keys from a PHP application to external stakeholder APIs?
- Embed them directly in the PHP source code
- Pass them via URL query parameters
- Store them in environment variables and access via getenv() (Correct answer)
- Log them in a database for auditing
Correct answer: Store them in environment variables and access via getenv()
Environment variables accessed via getenv() keep secrets out of source code and version control, following the 12-factor app principle.
Question 3: In PHP, which function verifies an HMAC signature on an incoming webhook payload to ensure it came from a trusted source?
- md5()
- hash_hmac() (Correct answer)
- sha1()
- openssl_verify()
Correct answer: hash_hmac()
hash_hmac() generates an HMAC digest that can be compared against the signature provided by the webhook sender to verify authenticity.
Question 4: When a PHP API needs to notify stakeholders of an event in real time without them polling, which technology is most appropriate?
- XML-RPC
- Server-Sent Events (SSE) or WebSockets (Correct answer)
- SOAP with WS-Notification
- Long-polling via plain HTTP
Correct answer: Server-Sent Events (SSE) or WebSockets
Server-Sent Events and WebSockets enable real-time push notifications from a PHP server to connected clients without repeated polling.
Question 5: Which PHP function sets an HTTP cookie that can be used to maintain state between a server and a stakeholder's browser session?
- session_set_cookie()
- setcookie() (Correct answer)
- cookie_set()
- header('Set-Cookie: ...')
Correct answer: setcookie()
setcookie() sends an HTTP Set-Cookie header and is the standard PHP function for creating browser cookies.
Question 6: What PHP cURL option must be set to true to make PHP follow HTTP redirect responses (301/302) automatically?
- CURLOPT_REDIRECT
- CURLOPT_FOLLOWLOCATION (Correct answer)
- CURLOPT_AUTOREDIRECT
- CURLOPT_MAXREDIRS
Correct answer: CURLOPT_FOLLOWLOCATION
CURLOPT_FOLLOWLOCATION tells cURL to automatically follow Location headers when a 3xx redirect response is received.
Question 7: Which PHP function encodes a string into Base64, commonly used when embedding binary data in JSON API payloads?
- base64()
- base64_encode() (Correct answer)
- bin2base64()
- encode_base64()
Correct answer: base64_encode()
base64_encode() converts binary data to a Base64-encoded ASCII string safe for inclusion in JSON, XML, or HTTP headers.
Which PHP session function must be called before accessing $_SESSION variables?