星期四, 7月 29, 2010

20100729 PHP-MySQL 上課小記

20100729 PHP-MySQL 上課小記

請將 session 目錄 copy 到專案資料夾內

Q: session_test.php為何是亂碼?
A:
  • 請先用 NotePad++ 確認編碼
  • 請以記事本開啟( 有時因記事本編輯過, 造成編碼異常, 因windows 預設的UTF-8碼, 包含BOM, 會造成php 執行可能異常)
  • 若記事本開啟可以看到正確的中文, 請複製到 Notepad++



session_test.php 內容如下

<?  session_start(); ?><!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
   <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       <title></title>
   </head>
   <body>
       <?
       echo "a:".$_SESSION['a']."<br>";
       echo session_id()."<br>";
       $_SESSION['a']=5;
       echo "a:".$_SESSION['a']."<br>";
       echo session_id()."<br>";
       session_unset();
       echo "a:".$_SESSION['a']."<br>";
       echo session_id()."<br>";
       session_destroy();
       echo "a:".$_SESSION['a']."<br>";
       echo session_id()."<br>";
       ?>
       <a href="session_test2.php">session是否可以看見?</a>
   </body>
</html>

Windows 的session 預設在 C:\Documents and Settings\使用者帳號\Local Settings\Temp
  1. session_start( )  於server 上產生一個session檔案
  2. session_id(  )  顯示session 檔案名稱
  3. 當產生session 變數, session檔案內容會新增,當session變數內容變更, session檔內容會更改
  4. session_unset(  )  清除所有 session 變數
  5. session_destroy(  ) 刪除 session id 檔案


Q: 可否只執行  session_unset(  ) or session_destroy(  )?
A: 如同郵差送資料到你家. session_unset( ) 取消資料,  但是郵差仍在. session_destroy(  ) 取消郵差, 但是資料仍在, 不能傳送.

*瀏覽器關閉, session 自然會消失, 但為避免 user 不關瀏覽器, 而只關分頁, 建議加上 ”登出” 功能消除 session.

Lab: 限制 session
建立一個 session_counter.php 內容如下(必須注意 <?ob_start();?> 必須放在第一行 )

<?ob_start();?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
   <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       <title>session 只能使用一次</title>
   </head>
   <body>
       <?php
       session_start();
       //判斷 $_SESSION 變數, 若不存在
       if(!isset ($_SESSION['counter']))
          //建立 session變數, 值為1
       {
           $_SESSION['counter']=1;
           echo "歡迎光臨";

       }
       else  //代表 session 變數存在
           echo "已登錄過請勿重複登入";
       // put your code here
       ?>
   </body>
</html>

Q: 可否只清除一個session 變數?
A: 所有變數(包含一般, POST, GET, SESSION, COOKIE) 若為空字串, 內容就清空.  但若要使變數不見, 可用 unset(變數) 刪除

Lab: 了解 session_unset 與 session_destroy 的差異
session_test3.php 內容如下( 藉由取消註解來了解)

<?session_start(); ?><!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
   <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       <title></title>
   </head>
   <body>
       <?
      if (isset($_SESSION['b']))
          {
          echo "b存在"."<br>";
          echo  $_SESSION['b']."<br>";
          }
      else
          {
           echo "b不存在";
           $_SESSION['b']=50;
           echo  $_SESSION['b']."<br>";
          }
       echo "a:".$_SESSION['a']."<br>";
       echo session_id()."<br>";        
       $_SESSION['a']=5;

       echo "a:".$_SESSION['a']."<br>";
       echo session_id()."<br>";    
     /*  session_unset();
       echo "a:".$_SESSION['a']."<br>";
       echo session_id()."<br>";

       session_destroy();
       echo "a:".$_SESSION['a']."<br>";
       echo session_id()."<br>";     */
       ?>
       <a href="session_test4.php">session是否可以看見?</a>
   </body>
</html>

Lab: 表單與 session
利用 3 個 php 檔案來進行表單與 session 的練習

1.php 負責 form 與 login

<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>表單登入</title></head><body>
<form name="form1" method="post" action="2.php">請輸入姓名:
<input type="text" name="username" maxlength="6" size="8"><br>
請輸入密碼:
<input type="password" name="passwd" maxlength=”6” size="8"><br>
<input type="submit"><input type="reset"></form> </body></html>


2.php
  • 產生 session 接收 form 內容(檢驗 form 是否輸入資料)
  • 若表單有輸入資料, 則可產生 session


<? session_start( ); ?><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>驗證表單資料</title></head><body> <?  
 if(!isset($_POST['username']))
 {  ?>
       <script>
           window.alert('請輸入帳號');
           history.back();
       </script>
    <?
 }
 if(!isset($_POST['passwd']))
 {  ?>
       <script>
          windows.alert('請輸入密碼');
          history.back();
       </script>
    <?
 }  
 $_SESSION['username']=$_POST['username'];
 $_SESSION['passwd']=$_POST['passwd'];  
 echo '<br/><a href="3.php">第三頁</a>';
 ?></body></html>


3.php 
  • 檢驗是否有 session 變數, 若無代表沒有經過 2.php, 代表沒有經過 1.php
  • 因為要產生 session, 必須先有表單資料, 所以必須經過 1.php


<? session_start( );  ?><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>查閱資料</title></head><body> <?  
 if(!isset($_SESSION['username']))
 {
  ?>
       <script>
           window.alert('請輸入帳號');
           location.href='1.php';
       </script>
  <?
 }
 if(!isset($_POST['passwd']))
 {
   ?>
       <script>
           window.alert('請輸入密碼');
           location.href='1.php';
       </script>
   <?
 }  
 echo '歡迎光臨';
 ?></body></html>


php 語法內加入 java script
  • 請留意 php 語法結束與開始的符號
  • java script 於 user 端執行,  所以php 可送資料給 script, 但 java script 預設是不能送資料給 php


Notes:
  • history.back( );
    • 回到前一頁, ( 但是如果沒有前一頁, 會停留在目前頁面, 要注意)
  • window.alert( );
    • 彈出訊息, 請注意是 window, 不是 windows
  • location.href=”   “;
    • 轉換至指定網頁
  • 以上三個均是 java script, 非 php 語法
 

星期二, 7月 27, 2010

20100727 PHP-MySQL上課小記

20100727 上課小記

建立一個 Netbean 的PHP 專案 class3
建立新專案
File → New Project
PHP: PHP Application → Next
點選 Browse 按鈕 建立一個 class3 資料夾( source 按鈕調整於 C:\Appserv\www 目錄下 [網站根目錄] )
Finish

將\\PC1 上面預先準備好的資料 複製到 C:\Appserv\www\class3 下面

執行 year.html 若輸入的年齡 大於 18, 結果為?
year.html 內容如下
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 </head>
 <body>
<form action="year.php" method="post" name="form1">
請輸入年齡:<input type="text" name="years">
<input type="submit" value="ok" /><br></form>
</body>
</html>


year.php 內容如下
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
</head>
<body>
<?
if (!isset($_POST["years"]))
{
  die("沒有資料") ;
}
$year1=$_POST["years"];
if ($year1>=12)
{  echo "可看輔導級,不可看限制級"; }
elseif($year1>=18)
{echo "可看限制級";}
elseif ($year1>=6)
{  echo "可看保護級"; }
else
{  echo "可看普遍級"; }
?>
</body>
</html>


if (條件1)
    else if (條件2)
else  以上條件均不成立時

Notes:
  • 若有多個條件要依序來作分析, 建議條件由大而小, 或由小而大依序執行
  • if, else 是前一個條件成立,  就不在執行 elseif 及 else


因為 18 歲以上只會顯示一種, 所以做一下修改
將year.html 另存為 year2.html,  action 改為 year2.php
將yearphp 改為 year2.php

year2.html 內容如下
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
   <title></title>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 </head>
 <body>
   <form action="year2.php" method="post" name="form1">
請輸入年齡:<input type="text" name="years">
<input type="submit" value="ok" /><br></form>
</body>
</html>

year2.php 內容如下 (將所有的 elseif 改為 if 並將 else 註解)
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
   <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       <title></title>
   </head>
   <body>
<?
if (!isset($_POST["years"]))
{
  die("沒有資料") ;
}
$year1=$_POST["years"];
if ($year1>=12)
{  echo "可看輔導級,不可看限制級"; }
if($year1>=18)
   {echo "可看限制級";}
if ($year1>=6)
{  echo "可看保護級"; }
//else
{  echo "可看普遍級"; }
?>

   </body>
</html>


isset( )
  • 判斷( ) 內的變數是否存在, 存在傳回true


if( ) 內若使用 !  代表相反,  例如 if( !isset())  代表當檔案或變數不存在時
die("訊息”顯示訊息後停止網頁運作
  • 不執行之後的html 及 php 語法


只要表單有送出資料, 接收資料的 php 檔就會建立 $_POST 或 $_GET 變數

Lab: 偵測內容是否有被輸入

使用 from1.html 來輸入資料, 內容如下
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>text與passwd</title>
</head>

<body>
<form action="form1.php" method="post">
請輸入姓名:<input type="text"  size="10" name="username" maxlength="6"><br>
請輸入密碼:<input type="password"  size="10" name="passwd" maxlength="6"><br>
<input type="submit"><input type="reset">
</form>
</body>
</html>


接收的 form1.php 內容如下
<html><head><title>接收text與passwd</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <? if (!isset($_POST["username"])) {   die("沒有姓名") ; } if (!isset($_POST["passwd"])) {   die("沒有密碼") ; }

//用來 Debug 用
echo $_POST['username'];
echo $_POST['passwd'];

if($_POST['username'] == '')
      die("姓名是空的");   if($_POST['passwd'] == '')      die("密碼是空的");

  ?><br> 接收的姓名為<? echo $_POST['username'];?><br> 接收的密碼為<? echo $_POST['passwd'];?><br> </body> </html>





for 迴圈
  • 最嚴謹及複雜的迴圈形式
  • for ( 1 ; 2 ; 3 )
    • 1: 變數的初始值
    • 2: 可執行範圍
    • 3: 每跑完一次迴圈固定變化
  • for 迴圈有初始值,有離開的條件,有固定的變化, 可知道固定的範圍


Q: 如何在 form , html 上作資料檢查?
A:
  • 可自寫 java script 或 jQuery
  • 但瀏覽器可關閉 script 功能, 所以 php 能須作檢查


jQuery:
  • java script 套件.
  • 可用較少的語法, 使用套件內提供的功能


while(  )
{
//先判斷再執行
//可能一次都不執行
}

範例 while.php
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>迴圈介紹:while </title></head><body>
 <?
$i = 1;
while ($i<=10){   //while是先做判斷再執行迴圈
echo "i=".$i."<br>";
   $i++;
}
  ?>
</body>
</html>



do
{
//先執行再判斷
//至少執行一次
}while (  ); //請注意 ;

範例 
dowhile1.php
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>dowhile:當條件成立時 </title></head>
<body><?
$i = 1;
do
{   
echo "i=".$i."<br>";
$i++;
}while ($i<10);
   echo "離開迴圈時 i=".$i."<br>";
?> </body></html>

dowhile2.php
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>dowhile:當條件不成立時 </title></head>
<body><?
$i = 11;
do
{   
echo "i=".$i."<br>";
$i++;
}while ($i<10);
   echo "離開迴圈時 i=".$i."<br>";
?> </body></html>

continue:
  • 中止此次迴圈, 繼續跑下一個值


continue 範例
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> conitue </title></head>
<body><?
for ($i=1;$i<=10;$i++)
{
       if ($i==5)
{
    echo "迴圈停止<br>";
    continue;
}         
echo "i->".$i."<br>";
}
echo "結束執行";
?></body></html>


break:
  • 跳出迴圈


break 範例
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>break </title></head>
<body><?
for ($i=1;$i<=10;$i++)
{
  if ($i==5)
{
    echo "迴圈停止<br>";
    break;
}
echo "i->".$i."<br>";
}
echo "結束執行";
?></body></html>


exit:
  • 中止後面網頁運作


exit 範例
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> exit </title></head>
<body><?
for ($i=1;$i<=10;$i++)
{  
if ($i==5)
{
    echo "迴圈停止<br>";
    exit;
}
echo "i->".$i."<br>";
}
echo "結束執行";
?></body></html>

Array
  • [ ]  代表陣列
  • php 的 陣列預設由 index=0 開始, index 代表索引值
  • 若沒給索引值, 預設由 0 或是前一個索引值開始編號
  • 以 array 方式規劃
    • $陣列名稱=array(索引值);
  • php 陣列索引值允許以文字代替


範例 array1.php
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>第一個陣列</title></head>
<body><?
$chinese[0] = 80;
$chinese[1] = 60;
$chinese[2] = 90;      
$chinese[3] = 50;
$chinese[4] = 70;
for ($a=0; $a<5; $a++)
 echo "$chinese[$a] <br>" ;
?></body></html>

範例 array2.php
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>第一個陣列,不給予索引值編號</title></head>
<body><?
$chinese[] = 80;
$chinese[] = 60;
$chinese[] = 90;      
$chinese[] = 50;
$chinese[] = 70;
for ($a=0; $a<5; $a++)
 echo "$chinese[$a] <br>" ;
?></body></html>

範例 array3.php
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>以array方式建立陣列,索引值編號任意給</title></head>
<body><?
$chinese=array(
1=>80,
3=>60,      
6=>90,
8=>50,
9=>70
);
for ($a=0; $a<5; $a++)
 echo "$chinese[$a] <br>" ;
?></body></html>

範例 array4.php
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>以array方式建立文字型態索引值陣列</title></head>
<body><?
   $a=array(
    "Jan" => "一月",
    "Feb" => "二月",
    "Mar" => "三月"
   );
  echo $a["Mar"]."<br>";       
?></body></html>


foreach 有兩種用法
  • foreach($陣列 as $值)  可將每一筆資料逐一列出(空值省略)
  • foreach($陣列 as $值  => $值)  可
  • 文字為索引值時必須使用foreach



for 與 foreach 的比較
for
  • 有範圍
  • 列出所有資料(包含空值)

foreach
  • 執行至陣列結束
  • 列出的值不包含空值



Homework:
輸入3位同學的國英數三科成績, 並計算各科平均
(若時間允許, 再算每個人平均)
以陣列方式作業
-- class end --