Page 270 - Computer Software Application TP - Volume 1
P. 270
COMPUTER SOFTWARE APPLICATION - CITS
EXERCISE 54 : Demonstrate on E-mail in PHP, anatomy
of a cookie, setting a cookie with PHP,
deleting a cookie, creating session cookie,
working with the query string, creating
query string, starting a session, Working
with session, variables, Destroying
session, passing session IDs, Encoding
and decoding session variables
Objectives
At the end of this exercise you shall be able to
• set and delete cookie using PHP
• create and destroy a session using PHP
• work with a query string using PHP.
Requirements
Tools/Materials
• Computer/Laptop with latest configuration • PHP
• Text editor
• Operating system: windows 10 or 11 • Web browser
• Apache web server
Procedure
TASK 1: Knowing anatomy of a cookie
Cookie Name: Choose a descriptive name that identifies the data stored.
Cookie Value: The data you want to store, like user preferences or a session ID.
Lifetime: How long the cookie should last (in seconds, hours, or days).
Path: The URL path where the cookie should be accessible. Default is current path.
Domain: The domain for which the cookie applies. Default is current domain.
Security: Whether the cookie should only be sent over secure HTTPS connections.
HttpOnly: Whether the cookie should be accessible only through HTTP, protecting it from JavaScript
access.
TASK 2: setting a cookie with PHP
1 Open the text editor
2 Write the following codes
<html>
<body>
<?php
$cookie_name = “username”;
$cookie_value = “ John Carter”;
$expire = time()+30*24*60*60; // 30 days in seconds
$path = “/”; // accessible on entire website
$domain = “”; // default domain
$secure = true; // only send over HTTPS
$httponly = true; // not accessible through JavaScript
setcookie($cookie_name, $cookie_value, $expire, $path, $domain, $secure,
$httponly);
255