星期四, 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 語法
 

沒有留言: