Metropolitan State University

ICS 325

Building Services on the Internet

 

Lab 19

Using Cookies in PHP Scripting

(Linux Redhat 7.1)

 

 

Log onto redhat.ics.metrostate.edu using your loginid and password given in class.

 

$ cd

$ cd public_html

$ mkdir lab19

$ cd lab19

1.         Use a simple cookie

§           Edit the following file with the name as “cookieOne.php” using pico:

 

<?php

// print("<h1>Here is the first example on Cookies</h1>");

// No output is allowed before setting up cookies

// No echo, print, <html>, <head>, or any output in front setcookie

// Otherwise, a warning message will shown on your screen.

 

// Retrieve the trial number from a cookie (if it exists)

$tryOne = $_COOKIE["tryOne"];

 

if (empty($tryOne))  {        // no existing cookie

      setcookie("tryOne", "1");

      $tryOne = 1;

} else   {                    // update trial number

      $tryOne++;

      setcookie("tryOne", (int)$tryOne, 120);   // save cookie for

// 120 seconds

}

 

$myTime = getdate();    // get associative array with date/time

 

// first time

if (empty($_COOKIE['oldSecond']) || empty($_COOKIE['oldMinute']) ||

empty($_COOKIE['oldHour']))   {

      setcookie("oldHour", $myTime["hours"], 120);    

      setcookie("oldMinute", $myTime["minutes"], 120);

      setcookie("oldSecond", $myTime["seconds"], 120);

}

 

$lastsec = $_COOKIE["oldSecond"];   // retrieve previous time

$lastmin = $_COOKIE["oldMinute"];   // from cookie

$lasthou = $_COOKIE["oldHour"];

 

setcookie("oldSecond", $myTime["seconds"], 120);   // save current

// time

setcookie("oldMinute", $myTime["minutes"], 120);

setcookie("oldHour", $myTime["hours"], 120);

 

print("<h2>This is your visit number $tryOne.<br> Welcome

back!</h2>"); 

print("Today is ". $myTime["weekday"].", ".$myTime["month"]);

print(" ".$myTime["mday"].", ".$myTime["year"]);

print("<br>The current time is "

.$myTime["hours"].":".$myTime["minutes"]);

print(":".$myTime["seconds"]);

 

// Display info retrieved from cookie

print("<br>Your last visit was at"

.$lasthou.":".$lastmin.":".$lastsec);

?>

</body>

</html>   

   

 

§           Invoke the script using the following link,

http://redhat.ics.metrostate.edu/~f04325??/lab19/cookieOne.php   

§           Pause for 5 seconds and then click on “Refresh” button of the browser. If you can see the time for the current time has been changed and the previous time has been printed on the last line, your program is working properly. Otherwise, you have to check your program and see if there is an error there.

§           Close the browser, reopen it, and point it to ‘cookieOne.php’ again. What happened?

 

 

2.         Use a simple cookie with an expiration time.

  • Edit the following file with the name as “cookieTwo.php” using pico:

 

<?php

setcookie("again", "y", 600 )    // cookie persists for 600 sec

?>

<html>

<body>

<?php

if ( $again == "y" ) {           // $again is the cookie variable

   print "<h1> Welcome back! </h1>";

}

else  {

   print "<h1> Welcome aboard! </h1>";

}

?>    

 

§           Invoke the script using the following link,

http://redhat.ics.metrostate.edu/~f04325??/lab19/cookieTwo.php

§           Click on “Refresh” button of the browser.   What happened?

§           Close the browser, reopen it, and point it to ‘cookieTwo.php’ again. What happened?

§           Delete the cookie manually.  From inside Internet Explorer, select Tools | Internet Options, then click on Delete Cookies.

§           Close the browser, reopen it, and point it to ‘cookieTwo.php’ again. What happened?


 

3.         Delete a cookie

  • Edit the following file with the name as “cookieThree.php” using pico:

 

<?php

setcookie("again")         // sets cookie to have no value

?>

<html>

<body>

<?php

if ( $again == "y" ) {

   print "<h1> Welcome back! </h1>";

}

else  {

   print "<h1> Welcome aboard! </h1>";

}

?>    

 

 

§           Invoke the script using the following link,

http://redhat.ics.metrostate.edu/~f04325??/lab19/cookieThree.php

§           Click on “Refresh” button of the browser.   What happens?

§           Close the browser, reopen it, and point it to ‘cookieThree.php’ again. What happens?