Complete the code to start a session in PHP.
<?php
[1]();
?>In PHP, session_start() is used to begin a session.
Complete the code to set a cookie named 'user' with value 'Alice' that expires in 1 hour.
<?php setcookie('user', 'Alice', time() [1] 3600); ?>
To set a cookie expiration 1 hour from now, add 3600 seconds to current time.
Fix the error in the code to check if a cookie named 'user' exists.
<?php if (isset($_COOKIE[1])) { echo 'User cookie is set'; } ?>
To access a cookie value, use square brackets with the cookie name as a string key.
Fill both blanks to store and retrieve a session variable named 'username'.
<?php session_start(); $_SESSION[1] = 'Bob'; echo $_SESSION[2]; ?>
Session variables are accessed using square brackets with the variable name as a string.
Fill all three blanks to decide when to use a session or a cookie for storing user data.
<?php // Use session when data is [1] and cookie when data is [2] // Example: if ($data_is_sensitive) { session_start(); $_SESSION[3] = 'secret'; } else { setcookie('info', 'public', time() + 3600); } ?>
Sessions are used for secure (sensitive) data that should not persist on client side. Cookies are for non-sensitive data that can persist.
The session variable key should be a string in square brackets.