PHP113

From mi-linux
Jump to navigationJump to search

Main Page >> Web Application Development >> Workbook >> Cookies

State and Stateless

The World Wide Web, by design, is a stateless system, in that "state" is not maintained between transactions. Basically, when you request a webpage, information is returned to you, then the connection is broken, thus no information is maintained at the server between visits.

So what?

Can you imagine trying to make an eCommerce solution where you can't store information between pages - anything you add to a "basket" would be lost when you move to the next page. How can we create a "pseudo-state" between visits? There are two possible solutions, and a combination of these should provide a solution.

Cookies

Cookies are small variables held in the web client (browser) and can hold information that the server can access. When you access a given server (amazon.com) for example, the server can write a small file to your computer holding some variables and their corresponding values, assuming you have set your web browser privileges to allow cookies. When you revisit the server, your cookies are sent back to server.

A simple cookie example

NOTE: you'll need the CSS files from PHP105 and a half

Create this file, and save it as "changestyle.php"

<?php
 $thisStyle = 0;

 if (isset($_COOKIE["selectedStyle"])) { // has the cookie already been set
   $thisStyle = $_COOKIE["selectedStyle"];
 }

 if (isset($_POST["changeStyle"])) { // changing the style
   $thisStyle = $_POST["changeStyle"];
 }

 setcookie("selectedStyle", $thisStyle); // update or create the cookie
?>

 <html>
   <head>
     <title>Cookies - the style change page</title>
     <link rel="stylesheet" href="style<?= $thisStyle; ?>.css">
   </head>
   <body>
     <h1>This is the colour of a heading number 1</h1>
     <form method="post" action="<?= $_SERVER["PHP_SELF"];?>">
       <input type="submit" name="changeStyle" value="0"><BR>
       <input type="submit" name="changeStyle" value="1"><BR>
       <input type="submit" name="changeStyle" value="2"><BR>
     </form>
     <p><a href="page1.php">Link back to a page</a></p>
   </body>
 </html>

Create this file and call it "page1.php"

<html>
 <head>
   <title>Cookies - a normal page</title>
   <?php
   if (isset($_COOKIE["selectedStyle"])) { // if style has been set
     $style = $_COOKIE["selectedStyle"];
   } else { // if style not yet set, default to 0
     $style = 0;
   }
   ?>
   
  <link rel="stylesheet" href="style<?= $style; ?>.css">
 </head>
 <body>
   <h1>This is the colour of a heading number 1</h1>
   <p>This is any page of your website - the style of this page is taken from a cookie, if it has been set</p>
   <p><a href="changestyle.php">This is a link to a page where you can Change The Style</a></p>
 </body>
</html>

Open your browser and point it at page1.php

Here is a working example: https://mi-linux.wlv.ac.uk/~in9352/php/page1.php

EXERCISE: experiment with using cookies for different things in your site - maybe prompt to enter a name and store it in a cookie.

Ready to move on?

Cookies are the first way to achieve state - there are a number of reasons why sessions are preferable - next section PHP114 - Sessions