Tärning | Visa källkod | Hangman | XHTML | CSS | How to become a hacker

Visa källkod

Nedanstående filer finns i denna katalogen. Klicka på en fil för att visa dess innehåll.

dice1/

common.php
dice.php
source.php
template.php

dice.php
<?php
// ===========================================================================================
//
// dice.php
//
// Description: Kasta lite tärning
//
// Author: Mikael Roos
//


// -------------------------------------------------------------------------------------------
//
// Error handling on/off
//
error_reporting(E_ALL | E_STRICT);
$debug = "";
$debugEnable = TRUE;  // TRUE to enable debugging, FALSE to not print out debug information


// -------------------------------------------------------------------------------------------
//
// Take care of GET variables, and validate them
//
//$antal    = (empty($_GET['antal'])    ? "" : $_GET['antal']);
$antal = filter_input(INPUT_GET, 'antal', FILTER_VALIDATE_INT);
if($antal === FALSE) die("Felaktigt värde."); 


// -------------------------------------------------------------------------------------------
// 
// Create a form for managing input.
//
$form = <<< EOD
<div>
    <form action='{$_SERVER['PHP_SELF']}' method='get'>
    <input type='text' name='antal' value='{$antal}' />
        <button type='submit'>Kasta</button>
    </form>
    <a href='{$_SERVER['PHP_SELF']}'>Starta om</a>
</div>

EOD;


// -------------------------------------------------------------------------------------------
//
// Throw the dice
//
//$antal = 6; // antal slag
$summa = 0;
$serie = "";
for($i=$antal; $i>0; $i--) {
    $slag = rand(1, 6);
    $summa += $slag;
    $serie .= "{$slag},";
}

$dice = "<p>";
if($antal) {
    $medel = round($summa/$antal, 1);
    $dice .= <<<EOD
Slagserien är: {$serie}<br />
Medelvärdet av slagserien är: {$medel}<br />

EOD;
} else {
    $dice .= "<a href='http://en.wikipedia.org/wiki/Alea_iacta_est'>Alea iacta est...</a>";
}
$dice .= "</p>";


// -------------------------------------------------------------------------------------------
//
// The content of the page
//
$html = <<<EOD
<div>
<h1>Min Tärning</h1>
<p>
Välkommen att kasta lite tärning, ange hur många kast du vill göra.
</p>
{$form}
</div>
$dice

EOD;


// -------------------------------------------------------------------------------------------
//
// Create and print out the html-page
//
require_once('common.php');
$title         = "Min tärning";
$charset     = "iso-8859-1";
$language    = "sv";
$debug         = $debugEnable ? $debug : "";

$html = <<< EOD
<?xml version="1.0" encoding="{$charset}" ?> 
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="{$language}" lang="{$language}">  
    <head>
        <meta http-equiv="Content-Type" content="text/html; {$charset};" />
        <title>{$title}</title>
    </head>
    <body>
        {$header}
        {$html}
        <div style='font-size: small;'>
            {$debug}
        </div>
        {$footer}
        </body>
</html>
EOD;

header("Content-Type: application/xhtml+xml; charset={$charset}");
echo $html;
exit;

?>
 .                        
..:    © Mikael Roos, 2009