Difference between revisions of "PHP113"

From mi-linux
Jump to navigationJump to search
 
 
(17 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
[[Main Page]] >> [[CP2228|Web Application Development]] >> [[PHP|Workbook]] >> Cookies
 +
 
== State and Stateless ==
 
== 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.
+
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? ===
 
=== 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.
+
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 ==
  
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.
+
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 ==
 
== A simple cookie example ==
  
create this file, and save it as "page1.php" - COPY IT and save it as page2.php, page3.php as well
+
'''NOTE:''' you'll need the CSS files from [[PHP105 and a half]]
<nowiki>
+
 
 +
Create this file, and save it as "changestyle.php"
 +
<pre>
 +
<?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>
 
  <html>
 
   <head>
 
   <head>
     <title>Cookie Test</title>
+
     <title>Cookies - the style change page</title>
 +
    <link rel="stylesheet" href="style<?= $thisStyle; ?>.css">
 
   </head>
 
   </head>
 
   <body>
 
   <body>
     <h1>The Cookie Example...</h1>
+
     <h1>This is the colour of a heading number 1</h1>
     <?
+
     <form method="post" action="<?= $_SERVER["PHP_SELF"];?>">
      if (isset($_COOKIE["numberOfPagesViewed"]))
+
       <input type="submit" name="changeStyle" value="0"><BR>
       {
+
      <input type="submit" name="changeStyle" value="1"><BR>
        echo "<p>You have visited ".$_COOKIE["numberOfPagesViewed"]." pages so far</p>";
+
      <input type="submit" name="changeStyle" value="2"><BR>
        $_COOKIE["numberOfPagesViewed"]++;
+
    </form>
      }
+
    <p><a href="page1.php">Link back to a page</a></p>
      else
 
      {
 
        echo "<p>this is your first page viewed</p>";
 
        $_COOKIE["numberOfPagesViewed"]=1;
 
      }
 
    ?>
 
 
   </body>
 
   </body>
  </html></nowiki>
+
  </html>
 +
</pre>
 +
 
 +
Create this file and call it "page1.php"
 +
 
 +
<pre>
 +
<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>
 +
</pre>
 +
 
 +
Open your browser and point it at page1.php
 +
 
 +
Here is a working example: [https://mi-linux.wlv.ac.uk/~in9352/php/page1.php 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? ==
  
point your browser to page1.php, then to page2, then page3, then back to page1, etc.  See how the counter increments?
+
Cookies are the first way to achieve state - there are a number of reasons why sessions are preferable - next section [[PHP114|PHP114 - Sessions]]

Latest revision as of 11:58, 20 September 2023

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