Skip to content

Instantly share code, notes, and snippets.

Created October 12, 2012 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3881162 to your computer and use it in GitHub Desktop.
Save anonymous/3881162 to your computer and use it in GitHub Desktop.
index
<?php
require_once "include/Session.php";
$session = new Session();
if (!isset($session->valid)) { // user hasn't validated
$target = $_SERVER['REQUEST_URI']; // where I'm trying to go
require_once "login.php"; // forward request to login.php
exit();
}
require_once "include/menu.php";
$params = (object) $_REQUEST;
print_r($params);
$choices->order = array_fill_keys($menu, "");
//print_r($choices->order);
//print_r($prices);
//if the session gets seriously messed up,
//uncomment the following statement, run, and then re-comment
//$session->unsetAll();
if(isset($params->add)){
$pick = $_GET['order'];
$order = preg_replace("/[^A-z0-9\ -]/","",$_GET['order']);
$sum = 0;
$qty = 1;
$session->selections[$order] = 1;
$choices->order[$order] = "selected";
}
else {
$session->selections = array();
$price = "";
$order = "";
$sum = "";
$pick = "";
$num = "";
}
$selections = $session->selections;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Takeout Order</title>
<style type="text/css">
body {
padding: 10px 20px;
}
form {
margin: 10px 0;
}
#logout {
position: absolute;
top: 10px;
right: 10px;
}
table {
width: 250px;
}
</style>
</head>
<body>
<form action="">
<button type='submit'>Start Over</button>
</form>
<h2>Your Order</h2>
<form action="" method="get">
items:
<select name="order">
<?php foreach ($menu as $key => $value): ?>
<option <?php echo $choices->order[$key] ?>> <?php echo $key?></option>
<?php endforeach ?>
</select>
<input type="submit" name="add" value="Add" />
</form>
<table>
<tr id = "category">
<td><?php echo "Item" ?></td>
<td><?php echo "Price" ?></td>
<td><?php echo "#" ?></td>
</tr>
<?php foreach ($selections as $order => $value ): ?>
<?php $sum += ($menu[$order]); ?>
<tr>
<td><?php echo $order ?></td>
<td><?php echo ($menu[$order])?></td>
<td><?php echo (++$session->selections[$order]) ?></td>
</tr>
<?php endforeach ?>
<tr>
<td><?php echo "Total: "?></td>
<td><?php echo $sum ?></td>
</tr>
</table>
<form id="logout" action="logout.php">
<input type="hidden" name="target" value="." />
<input type="submit" value="logout" />
</form>
</body>
</html>
<?php
$menu = array(
"Nachos" => "7.79",
"Buffalo Wings" => "5.99",
"Egg Rolls" => "4.99",
"Tostada Chips" => "1.99",
"Chicken Fajita Salad" => "6.89",
"Spinach Salad" => "6.79",
"Chicken Caesar Salad" => "7.49",
"Margarita Grilled Chicken" => "8.99",
"Margarita Grilled Tuna" => "8.49",
"Grilled Pork Chops" => "10.99",
"Bacon Burger" => "6.49",
"OldTimer Burger" => "5.59",
"Mushroom-Swiss Burger" => "6.29",
);
<?php
session_start();
define( "SECRET", "-set-this-to-make-sessions-secure-" );
define( "HASH", md5(dirname(__FILE__) . SECRET) );
define( "KEY", "sess_" . HASH );
class Session {
public function __set($name,$value) {
$_SESSION[KEY][$name] = $value;
}
public function & __get($name) {
return $_SESSION[KEY][$name];
}
public function __toString() {
return print_r($_SESSION[KEY],true);
}
public function __isset($name) {
return isset($_SESSION[KEY][$name]);
}
public function __unset($name) {
unset($_SESSION[KEY][$name]);
}
public function unsetAll() {
unset($_SESSION[KEY]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment