Exercise
1. Create and retrieve a cookie named "username" with the value "Patience Zulu". The cookie should expire after 20 days. Check if the cookie is set and display the value of the cookie.
2. Delete the cookie from the previous question.
3. Create the file “People.php” and write a php script that will open a file for writing “WritePeople.txt” file and close the file after writing.
4. Create the file “Cars.php” and write a php script that that will open a file for reading “CarNames.txt” file and close the file after writing.
1. Creating and retrieving a cookie named "username" with the value "Patience Zulu"
which should expire after 20 days, Checking if it set and displaying its value.
ANSWER.
<?php
$cookie_name = "username";
$cookie_value = "Patience Zulu";
setcookie($cookie_name, $cookie_value, time() + (86400 * 20), "/");
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
2. Delete the cookie from the previous question.
ANSWER.
<?php
setcookie("username", "", time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'username' is deleted.";
?>
</body>
</html>
3. Create the file “People.php” and write a php script that will open a file for writing “WritePeople.txt” file and close the file after writing.
ANSWER
//file name is People.php
<?php
$my_write_file = fopen("WritePeople.txt", "w");
// some code to be executed....
fclose($my_write_file);
?>
4. Create the file “Cars.php” and write a php script that that will open a file for reading “CarNames.txt” file and close the file after writing.
ANSWER
//file name is Cars.php
<?php
$my_read_file = fopen("CarNames.txt", "r");
// some code to be executed....
fclose($my_read_file);
?>
Comments
Leave a comment