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:

  1. Print today's date in "Y/m/d" format.
  2. Print the current time in "h:i:sa" format.
  3. 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:

  1. Assume a `footer.php` file exists.
  2. Use `include` to insert it.
  3. 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:

  1. Assume `webdictionary.txt` exists.
  2. Use `readfile()` to read it and print bytes.
  3. 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:

  1. Create/open `newfile.txt` in write mode.
  2. Write "Hello PHP" then close the file.
  3. 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:

  1. Open `newfile.txt` in append mode.
  2. Add a new line: "This is a new line".
  3. 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:

  1. Start a session with `session_start()`.
  2. Set `$_SESSION['username'] = "Ali"`.
  3. Print the session value.
Solution
<?php
session_start();
$_SESSION['username'] = "Ali";

echo "User: " . $_SESSION['username'];
?>
Exercise 7 Destroy Session

End a session:

  1. Call `session_start()`.
  2. Unset all session variables.
  3. Destroy the session.
Solution
<?php
session_start();
session_unset();
session_destroy();
?>
Exercise 8 Cookies

Set and read a cookie:

  1. Create a cookie named `user` with value "Ali" for 1 hour.
  2. 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:

  1. If cookies are enabled, print a message.
  2. 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:

  1. Assume a file input named `fileToUpload`.
  2. Move the uploaded file to an `uploads/` folder.
  3. 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.";
}
?>
Smart Editor

Write code and see the result instantly

Try it free