PHP Exam 4: Sessions, Cookies, and Files (10 mixed exercises)
PHP Exam 4: Advanced Features and Files
Test your ability to manage user state (sessions/cookies) and work with files and time.
Exercise 1
Date & Time
Display the current time:
- Print today's date in "Y/m/d" format.
- Print the current time in "h:i:sa" format.
- Set the timezone to "Africa/Cairo".
Solution
<?php
date_default_timezone_set("Africa/Cairo");
echo "Today is " . date("Y/m/d") . "<br>";
echo "The time is " . date("h:i:sa");
?>
Exercise 2
Include & Require
Organize your code into files:
- Assume a `footer.php` file exists.
- Use `include` to insert it.
- Explain the difference between `include` and `require` in a comment.
Solution
<?php
include 'footer.php';
// Difference:
// include: warning if file missing, script continues.
// require: fatal error if file missing, script stops.
?>
Exercise 3
File Handling - Read
Read a text file:
- Assume `webdictionary.txt` exists.
- Use `readfile()` to read it and print bytes.
- Or use `fopen`, `fread`, `fclose`.
Solution
<?php
echo readfile("webdictionary.txt");
// Or:
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile, filesize("webdictionary.txt"));
fclose($myfile);
?>
Exercise 4
File Handling - Write
Write to a file:
- Create/open `newfile.txt` in write mode.
- Write "Hello PHP" then close the file.
- Explain that write mode overwrites existing content.
Solution
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$text = "Hello PHP";
fwrite($myfile, $text);
fclose($myfile);
// Note: write mode overwrites the file content.
?>
Exercise 5
File Append
Append to a file:
- Open `newfile.txt` in append mode.
- Add a new line: "This is a new line".
- Close the file.
Solution
<?php
$myfile = fopen("newfile.txt", "a") or die("Unable to open file!");
$txt = "This is a new line\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
Exercise 6
Sessions
Create and use sessions:
- Start a session with `session_start()`.
- Set `$_SESSION['username'] = "Ali"`.
- Print the session value.
Solution
<?php
session_start();
$_SESSION['username'] = "Ali";
echo "User: " . $_SESSION['username'];
?>
Exercise 7
Destroy Session
End a session:
- Call `session_start()`.
- Unset all session variables.
- Destroy the session.
Solution
<?php
session_start();
session_unset();
session_destroy();
?>
Exercise 8
Cookies
Set and read a cookie:
- Create a cookie named `user` with value "Ali" for 1 hour.
- Read and print the cookie value.
Solution
<?php
setcookie("user", "Ali", time() + (3600), "/");
echo $_COOKIE['user'];
?>
Exercise 9
Check Cookies
Check if cookies are enabled:
- If cookies are enabled, print a message.
- If not, print a different message.
Solution
<?php
if (count($_COOKIE) > 0) {
echo "Cookies are enabled";
} else {
echo "Cookies are disabled";
}
?>
Exercise 10
Upload File (Basics)
Handle a file upload:
- Assume a file input named `fileToUpload`.
- Move the uploaded file to an `uploads/` folder.
- Use `move_uploaded_file`.
Solution
<?php
$targetDir = "uploads/";
$targetFile = $targetDir . basename($_FILES["fileToUpload"]["name"]);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetFile)) {
echo "The file has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
?>