Page 277 - Computer Software Application TP - Volume 1
P. 277
COMPUTER SOFTWARE APPLICATION - CITS
TASK 7: Setting a cookie
I Creating a session cookie
1 Open the text editor
2 Write the following codes
<html>
<body>
<?php
session_start();
// Set the session variable
$sessionId = session_id();
echo “session started with id :”.$sessionId
$_SESSION[‘username’] = ‘JohnDoe’;
// Set a cookie with the session ID
setcookie(session_name(), session_id(), time() + 60 * 60); // Expires in 1 hour
echo “Cookie set successfully!”;
?>
</body>
</html>
3 Save the program in C:\Apache24\htdocs in a folder with .php extension
4 Run the Apache services from windows services
5 Open the browser and type the following address
http://localhost/foldername/
6 Click the php file to run and verify the output
II Access session data on other pages
1 Open the text editor
2 Write the following codes
<html>
<body>
<?php
session_start();
if (isset($_SESSION[‘username’]))
{
262
CITS : IT & ITES - Computer Software Application - Exercise 54