星期二, 8月 03, 2010

20100803 PHP-MySQL 上課小記-Class 5

20100803 PHP-MySQL 上課小記-Class 5

Session 與 Cookie 的比較

session
  • 儲存位置
    • 在瀏覽器視窗
    • server
  • 保存期限
    • 預設瀏覽器關閉則清除( 但 server 檔案預設會暫時保存)
  • 限制
    • Server 端若空間不足 或是 資源不足, 將無法產生 session



cookie
  • 儲存位置
    • 使用者端以檔案方式儲存
  • 保存期限
    • 依照cookie設定
    • 依照瀏覽器設定
  • 限制
    • 使用者提高瀏覽器安全性
    • 該瀏覽器可儲存的 cookie 數目已滿, 無法再儲存



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

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

Lab: 觀察 cookie

執行 cookie1.php 以及 cookie2.php 觀察

cookie1.php 內容如下

<?ob_start();?>
<html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"> <title>儲存Cookie</title></head> <body><?  
setcookie("a", "php", time()+1800);//1800代表1800秒
 echo '<a href="cookie2.php">查詢Cookies</a><br>';  echo '<a href="cookie3.php">刪除1</a><br>';  echo '<a href="cookie4.php">刪除2</a><br>';

 ?></body></html>




cookie2.php 內容如下

<html><head><meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>顯示Cookie</title></head> <body><?
if (isset($_COOKIE["a"]))
  echo "cookie內容為".$_COOKIE["a"];    else       echo "沒有資料";  echo '<br><a href="cookie1.php">建立Cookies</a><br>';  echo '<a href="cookie3.php">刪除1</a><br>';  echo '<a href="cookie4.php">刪除2</a><br>';

 ?></body></html>


cookie
  • setcookie(  ) 內有 6 個參數, 一般設定為前面 3 個即可.
  • setcookie(a,b,c)
    • a: 變數名稱
    • b: 變數內容
    • c: 保存期限
  • cookie 變數如何消失?
    • 逾時
    • 變數沒有內容


刪除 cookie (時間到期)
執行 cookie3.php

cookie3.php 內容如下

<?ob_start();?>
<html><head><meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>刪除Cookie</title></head>
<body><?
setcookie("a","php",time()-1800);
 echo '<a href="cookie1.php">建立Cookies</a><br>';
 echo '<a href="cookie2.php">查詢Cookies</a><br>';
 echo '<a href="cookie4.php">刪除2</a><br>';
 ?></body></html>

刪除 cookie (變數沒有內容)

執行 cookie4.php 來觀察cookie4.php 來觀察

cookcookie4.php 內容如下

<?ob_start();?>
<html><head><meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>刪除Cookie2</title></head>
<body><?
       setcookie("a","",time()+1800);
 echo '<a href="cookie1.php">建立Cookies</a><br>';
 echo '<a href="cookie3.php">刪除1</a><br>';
 echo '<a href="cookie2.php">查詢Cookies</a><br>';
 ?></body></html>

Notes:
  • time()
    • server 目前的時間(以秒為單位)
  • 因瀏覽器會優先讀取暫存資料, 所以請重新整理頁面, 才能看到正確資訊(有些user可能出現此問題, 須提醒他們重新整理頁面)
  • mktime(時,分,秒,月,日,年)
    • 設定時間值為 1970年1月1日至今的秒數(該日為 Unix 誕生日子)
  • date(  )
    • 抓取現在 server 的年月日時分秒, 時區等資料
    • 日期時間格式化
    • 抓取現在 server 日期, 時間


Lab: cookie 綜合練習

練習 cookie 與時間

顯示目前的時間, 以及將時間存入陣列
showcokie1.php 內容如下

<?ob_start();?>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> cookies設定綜合練習</title></head><body>
<?$tomorrow  = mktime (0,0,0,date("m"),date("d")+1,date("Y"));  //mktime
 $nextmonth = mktime (0,0,0,date("m")+1,date("d"),date("Y"));
 $nextyear  = mktime (0,0,0,date("m"),date("d"),date("Y")+1);  
 $yourname="第6個cookie,Cookie變數的內容由變數取得資料";
 $testtime=mktime(0,0,0,7,13,2010);   
 setcookie ("a[0]","設定保存1800秒", time()+1800);
 setcookie ("a[1]","設定保存至2010年7月13日",$testtime);
 setcookie ("a[2]","設定保存至明天",$tomorrow);
 setcookie ("a[3]","設定保存至下一個月",$nextmonth);
 setcookie ("a[4]","設定保存至明年",$nextyear);
 setcookie("a[5]","$yourname",time()+1800);
 $tomorrow=mktime();
 echo "現在時間: ".date("Y-m-d",$tomorrow);
 echo "<br>";
 echo '<a href="showcokie2.php">查詢Cookies</a>';
?></body></html>

使用 for 迴圈觀察 cookie

showcokie2.php 內容如下

<?ob_start();?>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> cookies設定綜合練習查詢</title></head><body><?
 $value1=$_COOKIE["a"];
 $tomorrow=mktime();
 echo "現在時間: ".date("Y-m-d",$tomorrow)."<br>";
 echo "第1的內容為設定保存1800秒<br>";
 echo "第2的內容為設定保存至2010年7月13日<br>";
 echo "第3的內容為設定保存至明天<br>";
 echo "第4的內容為設定保存至下一個月<br>";
 echo "第5的內容為設定保存至明年<br><br>";
 for($for1=0;$for1<5;$for1++)
   {
    $a=$for1+1;
    echo "第".$a."的內容為".$value1[$for1]."<br>";
   }
 //補充
 echo '<a href="showcokie1.php">重新建立Cookies</a><br>';
 echo '<a href="showcokie3.php">刪除幾個Cookies</a><br>';  
?></body></html>


顯示 cookie的內容
showcokie3.php 內容如下

<?ob_start();?>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> cookies設定綜合練習刪除</title></head><body><?
  echo "設定保存1800秒,現在減1800秒"."<br>";
  echo "設定保存至2010年7月13日,現在內容沒了"."<br>";
//補充   
 echo "設定保存 1800 秒, 現在減 1800秒"."<br>";
 echo "設定保存至 2010 年 7月 31日, 現在內容沒了"."<br>";
 setcookie("a[0]","",time()-1800); //內容沒了
 setcookie("a[1]","");
 echo '<a href="showcokie1.php">重新建立Cookies</a><br>';
 echo '<a href="showcokie3.php">刪除幾個Cookies</a><br>';
?></body></html>


Q: 若表單上加上 hidden 隱藏欄位, 安全否?
A: 隱藏欄位網頁頁面看不到, 但原始碼有,重要資料勿用  hidden 欄位

Notes:
  • 重要資料請用 session, hidden 適合 ”不想給 user 看但是不重要” 的資料


header 用途
  1. 轉換網頁
  2. 更改網頁呈現方式(例如以圖片顯示)
  3. 將 html 的 <head> 區域移至 php 內


Notes:
  • cookie 抓取的時間是 server 的時間, 但比對是否逾期, 比對的是使用者的時間
    • user 任意改變自己 PC 的時間, 很可能逾期或是有其他的問題


Lab: 帳號驗證 以及 利用 header 轉址

login.html 內容如下

<html><head><meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>帳號密碼的輸入</title></head>
<body>
<form name="form1" method="post" action="loginpass.php" charset="utf-8">請輸入帳號密碼:<br>
帳號:<input type="text"  name="username" maxlength="6" size="10"><br>
密碼:<input type="password" name="passwd" maxlength="6" size="10"><br>
<input type="submit"><input type="reset"></form>
</body></html>


驗證的網頁 loginpass.php ( 在這邊我把他改成, 驗證成功連線到 google, 失敗到 yahoo)

loginpass.php 內容如下

<? 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>驗證帳號與密碼</title>
   </head>
   <body>
       <?php
       if(($_POST['username']=='php') and ($_POST['passwd']=='mysql'))
           header("Location:http://www.google.com");
       else
           header("Location:http://tw.yahoo.com");
       // put your code here
       ?>
   </body>
</html>

Q: 為何 doc, pdf 等檔案 google 找的到?
A: 當網站成為網際網路一員(脈絡, 當別人的網頁有 link 到你的網頁), 搜尋引擎機器人, 就會進來建立連結紀錄.


Notes:
  • session 或 cookie 的驗證可保護網頁, 但無法保護其他檔案
  • 網站根目錄內加入 robots.txt 檔案, 可訂定那些目錄或檔案不要被搜尋引擎紀錄


robots.txt
  • user-agent搜尋引擎
  • disallow: 拒絕登錄
    • disallow: /*.pdf  拒絕所有 pdf 檔案登陸


include 及 require
  1. 被引用的 php 檔案不需要完整的網頁架構
  2. 若引用失敗(例如: 找不到檔案 或 存取異常?)
    1. include 會出現其他的頁面資料
    2. require 不會出現頁面資料, 只顯示錯誤訊息
    3. 可以在 前面 加上 @ 符號, 壓抑錯誤訊息
 

利用 script 更新台灣的openSUSE套件庫

有時候安裝完 openSUSE Linux
套件庫預設是去抓 software.opensuse.org 的套件庫

有時候連線的速度很慢, 所以就寫了一個懶人的 script 來更換台灣這邊的套件庫

目前 openSUSE 11.3  高品質學術網路還沒有 sync, 所以先用原來的套件庫

shell script 內容如下

#!/bin/bash
echo "Print the rep  in use"
zypper  lr -u


#Define openSUSE Version
VERSION=`cat /etc/SuSE-release | grep VERSION | cut -d ' ' -f 3`
echo "This openSUSE version is $VERSION"


echo "Remove Ori Rep"
zypper rr 1
zypper rr 1
zypper rr 1
zypper rr 1
zypper rr 1
zypper rr 1


echo "Add Taiwan's rep "


## for OSS
zypper ar -f http://ftp.twaren.net/Linux/OpenSuSE/distribution/$VERSION/repo/oss/  Twaren-Oss
#zypper ar -f http://download.opensuse.org/distribution/$VERSION/repo/non-oss/ suse_non-oss
#zypper ar -f http://ftp.ncnu.edu.tw/Linux/opensuse/distribution/$VERSION/repo/non-oss/ ncnu_non-oss


## for Non-OSS
#zypper ar -f http://ftp.cse.yzu.edu.tw/pub/Linux/openSUSE/distribution/$VERSION/repo/non-oss/  cse_non-oss
zypper ar -f http://ftp.twaren.net/Linux/OpenSuSE/distribution/$VERSION/repo/non-oss/ Twaren-Non-oss


## for update
#zypper ar http://ftp.twaren.net/Linux/OpenSuSE/update/$VERSION/repodata/  twaren_update
zypper ar -f http://ftp.twaren.net/Linux/OpenSuSE/update/$VERSION/  Twaren-Update
#zypper ar -f http://download.opensuse.org/update/$VERSION/  suse_update


## for Pacman Rep
zypper ar -f http://ftp.nchc.org.tw/Linux/Packman/suse/$VERSION/ NCHC-Pacman
#zypper ar -f http://ftp.twaren.net/Linux/Packman/suse/$VERSION/ twaren_packman



先記下來  ︿︿
以後在新的電腦, 複製貼上來執行即可

enjoy it ~

openSUSE 11.3 x86_64 安裝小記

從openSUSE 11.3 開始, 開始決定使用 64 位元版本
很早以前( 五年多前吧 ^^|| )有安裝過 32 位元的 Linux 作業系統,
但是因為套件對 64 位元的支援度不佳, 後來就放棄

現在由於 Notebook 的記憶體都有 4GB 了
64 位元的套件也比較普及了
所以這次就開始使用 64 位元的套件

把一些額外安裝的套件記下來

OS: openSUSE 11.3 with Gnome


額外安裝的套件

  • Google chrome 瀏覽器
  • xen 虛擬化套件
  • pidgin 聊天軟體
  • freemind 0.9.0 RC8 心智圖編輯工具
  • skype 2.1 由於 skype 沒有 64 位元版本, 所以要安裝 32 位元的套件
    • xorg-x11-libXv-32bit
    • libqt4-32bit 
    • libqt4-x11-32bit 
    • libpng12-0-32bit
  • yast2-qt 因為還是習慣 QT 的 YaST 介面, 所以安裝 yast2-qt
  • poedit 修改 .po 翻譯檔案的工具
  • VMWare
    • kernel-desktop-devel 為了要編譯 VMWare Module

目前一切順利 ^^ 除了 skype 是  32 位元的以外

先記下來 ^^

星期一, 8月 02, 2010

openSUSE Edu Li-f-e 11.3 available now!

openSUSE Edu Li-f-e 11.3 available now!

openSUSE 有Edu 團隊, 一直是我覺得非常貼心的地方

現在 openSUSE Edu Life 11.3 已經可以下載

相關網頁可以參考

http://news.opensuse.org/2010/07/17/opensuse-edu-li-f-e-11-3-available-now/


轉貼部份介紹


openSUSE Education team is very thrilled to announce the availability of openSUSE Edu: Linux for Education(Li-f-e). Li-f-e is built on openSUSE 11.3. The aim of this DVD is to provide complete education and development resources for parents, students, teachers as well as IT admins running labs at educational institutes

It comes bundled with a wealth of softwares carefully selected to meet every need. Educational softwares covering wide range of subjects such as IT, mathematics, chemistry, astronomy, electronics etc catering to students right from preschool to research.

Softwares for graphic designers include GIMP, Inkscape, Blender and more. Office applications to work on pdfs and any office formats including those of the latest MicroSoft Office is included.

If you are a developer or a student wanting to learn programming on Linux platform, there is everything you can hope for in one place: Java, C, C++, Perl, Python, LAMP stack, databases, IDEs, the list goes on…