<?
if(isset($_GET['do']) && $_GET['do'] == "echo")
{
  echo "{\r\n";
  $bFirst=true;
  foreach($_COOKIE as $name => $value)
  {
    if($bFirst) $bFirst=false;
    else echo ",\r\n";
    echo  "\"".$name."\":\"".
          addslashes($value)."\"";
  }
  echo "\r\n}";
}
else if(isset($_GET['do']) && $_GET['do'] == "set")
{
  if(isset($_GET['name']) && isset($_GET['value']))
  {
    // Set cookie for 1 hour
    setcookie($_GET['name'], $_GET['value'], time()+3600);
    echo "{\"status\":\"success\"}";
  }
  else
  {
    echo "{\"status\":\"error\"}";
  }
}
else if(isset($_GET['do']) && $_GET['do'] == "unset")
{
  if(isset($_GET['name']))
  {
    // Set cookie for 1 hour
    setcookie($_GET['name'], "", -1);
  }
  else
  {
    foreach($_COOKIE as $name => $value)
    {
      setcookie($name, "", -1);
    }
  }
  echo "{\"status\":\"success\"}";
}
else
{
  echo "<h1>Usage:</h1>";
  echo  "<table border=\"1\">".
          "<tr>".
            "<td>".
              "<b>Print cookies</b>".
            "</td>".
            "<td>".
              "http://localhost/cookie.php?do=echo".
            "</td>".
          "</tr>".
          "<tr>".
            "<td>".
              "<b>Set cookie</b>".
            "</td>".
            "<td>".
              "http://localhost/cookie.php?do=set&name=test&value=test-value".
            "</td>".
          "</tr>".
          "<tr>".
            "<td>".
              "<b>Unset cookie</b>".
            "</td>".
            "<td>".
              "Unset specific:<br />".
              "http://localhost/cookie.php?do=unset&name=test".
              "<br />".
              "Unset all:<br />".
              "http://localhost/cookie.php?do=unset".
            "</td>".
          "</tr>".
        "</table>";
}
