2012年12月25日火曜日

開発環境

『初めてのPHP5』 (David Sklar 著、 桑村 潤 翻訳、 廣川 類 翻訳、 オライリー・ジャパン、2005年、ISBN978-4-87311-257-2)の8章(クッキーとセッションでユーザーを記憶)8.8(演習問題)3を解いてみる。

3.

HTML、PHPのソースコード(BBEdit)

sample100.php

<?php
  require 'formhelpers.php';
  session_start();
  $colors = array("#ff0000" => 'red',
                  "#00ff00" => 'green',
                  "#0000ff" => 'blue');
  if(array_key_exists('_submit_check', $_POST)){
    process_form();
  } else {
    show_form();
  }
  function show_form(){
    print '<form method="POST" action"' . $_SERVER['PHP_SELF'] . '">';
    input_select('color', $_POST, $GLOBALS['colors']);
    input_submit('submit', 'select color');
    print '<input type="hidden" name="_submit_check" value="1" />';
    print '</form>';
  }
  function process_form(){
    $_SESSION['color'] = $_POST['color'];
    print '<a href="./sample101.php">背景色: ' . 
      $GLOBALS['colors'][$_SESSION['color']] . 
      'のサイトへ移動</a>';
  };
?>

sample101.php

<?php
  session_start();
  print '<body bgColor="' . $_SESSION['color'] . '">';
  print '<a href="./sample100.php">背景色の変更ページへ戻る</a>';
  print '</body>';
?>

0 コメント:

コメントを投稿