星期四, 7月 22, 2010

20100722 PHP-MySQL 上課小記

20100722
表單與資料接收的練習

建立一個表單  form1.html
內容如下
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
TODO write content
<form name="form1" method="post" action="1.php">
username:<input type="text" name="username" size="6"> <br>
password:<input type="password" name="passwd" maxlength="6" size="10"> <br>
<input type="submit" value="ok"> <input type="reset" value="cancel">
<input type="button" value="what?">
</form>
</body>
</html>

建立接收的 1.php
內容如下
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
echo "1:".$_POST['username']."<br>";
echo "2:".$_POST['passwd']."<br>";
echo "3:".$username."<br>";
echo "4:".$passwd."<br>";
echo "5:".$_GET['username']."<br>";
echo "6:".$_GET['passwd']."<br>";
?>
</body>
</html>



get
  • 資料會出現在網址列
  • 一般用於具分頁功能的網頁
  • 檢查資料是否正確傳送及接收


post
  • 資料不會出現在網址列
  • 檔案上傳一定要用post


Notes:
  • 若表單網頁為html 請不要由本機 Click 開啟, 將無法執行 php 檔
  • 但如果form 修改 action=http://xxxxxx  ,  且php 網頁未作檢查, 個人PC內儲存的html檔案是可以送資料到 server
  • 表單內各項 input 的name, 在 php  內成為變數名稱, 用 post 方式傳送以 $_POST[‘名稱’] 接收, 用 get 方式則以 $_GET[‘名稱’] 接收
  • Linux 預設使用一般變數來接收是關閉的, 設定選項為 register_globals = Off 設定檔在 /etc/php5/apache2/php.ini , 但是 Windows Appserv 預設是打開的
  • php.ini 內 register_globals 設定為 on, 代表一般變數可接收資料, off 是不行.
  • php6 預設是不行, 且無此設定, 建議開始改變習慣, 以 $_POST[‘  ‘]  或是 $_GET[‘  ‘] 接收


Lab: 選擇鈕

建立一個 form3.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>
TODO write content
<form name="form3" method="get" action="3.php">
sex:
<input type="radio" name="sex" value="boy">公的
<input type="radio" name="sex" value="girl">母的
<br>
Blood:
<input type="radio" name="blood" value="O">O
<input type="radio" name="blood" value="A">A
<input type="radio" name="blood" value="B">B
<input type="radio" name="blood" value="AB" checked>AB
<br>
<input type="submit" value="ok"> <input type="reset" value="cancel">
</form>
</body>
</html>

建立一個 3.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>
sex is <? echo $_GET['sex'];?><br>
blood is <? echo $_GET['blood'];?><br>
</body>
</html>



選擇鈕
  • name --> 名稱 [同名稱的radio只能選一個]
  • value --> 傳送的內容
  • checked --> 預設選項


單行文字框
  • maxlength --> 最大字數
  • value  -->  預設值
  • name --> 名稱


建立一個 form4.html
內容如下





核選框
  • 可複選
  • value, checked, disabled 同 radio
  • 名稱name若相同, 只會取最後一個
    • name 名稱若是變數, 則每一個checkbox 必須不同name
    • name 名稱若是陣列, 名稱後加上  [  ], 例如  name=”win[ ]” ,就可以將checkbox 分組


( 待補 form4 and form 3因為  3.php 沒有收到結果)

增加驗證
  • session
  • cookie
  • 圖形驗證
  • ip 及  資料庫過濾


session
  • 儲存於網頁視窗及主機(Server)
  • 當視窗關閉, 視窗的 session 就會消失
  • Windows 主機建議將 session  資料存在非系統磁區
  • session_start( )
    • 必須放在所有輸出之前, 若不能放在最前面, 請加上ob_start( ) 開啟輸出緩衝區暫存資料
    • session_start會在 server 上產生檔案及視窗內產生一個session id, 若要使資料傳送, 每一頁均要有 session_start( )
  • user 端不能由網址列更改 session 內容
  • php.ini 內session.save_path 設定 session 儲存
  • 每一個分頁均視為同一個視窗, 若要測試session, 請關閉視窗再測試
  • 為何session  在 server  上的資料未清除?
    • Server 不清楚 user 端是關閉瀏覽器或在看資料, 所以不建議修改 php.ini 內的 session.gc 之相關設定
    • 觀察 session 於 php.ini 相關設定如下
    • # grep session /etc/php5/apache2/php.ini  | grep -v \;
    • session.save_handler = files
    • session.save_path = "/var/lib/php5"
    • session.use_cookies = 1
    • session.use_only_cookies = 1
    • session.name = PHPSESSID
    • session.auto_start = 0
    • session.cookie_lifetime = 0
    • session.cookie_path = /
    • session.cookie_domain =
    • session.cookie_httponly =
    • session.serialize_handler = php
    • session.gc_probability = 1
    • session.gc_divisor = 1000
    • session.gc_maxlifetime = 1440
    • session.bug_compat_42 = Off
    • session.bug_compat_warn = Off
    • session.referer_check =
    • session.entropy_length = 0
    • session.entropy_file = /dev/urandom
    • session.entropy_length = 16
    • session.cache_limiter = nocache
    • session.cache_expire = 180
    • session.use_trans_sid = 0
    • session.hash_function = 3
    • session.hash_bits_per_character = 5
    • 預設1440 秒刪除 1 %沒有回應的 session


時區
  • php4 依作業系統
  • php5 為格林威治時間
  • php6 必須自己設定
  • 調整方式: php.ini 內 ;date.timezone = 取消註解, 並加上時區


-- class end --

星期二, 7月 20, 2010

20100720 PHP-MySQL 上課小記

20100720 PHP-MySQL 上課小記

葉建榮

PHP ASP 之前就已經誕生, asp是模仿php 3
**PHP + MySQL + Apache:
  • php等三者均是free
  • 若搭配Linux, 均為free
  • Apache:    提供網頁服務伺服器( Web Server )
  • MySQL:    提供資料存取的資料庫伺服器
  • PHP:        web Server 上作資料運用


Q: Windows 上如何安裝 Apache + PHP + MySQL?
A: 建議下載 AppServ 整合 Apache + MySQL + PHP

Q: 可否支援 IIS ?
A: php 可在 IIS 環境跑, 但效能差( IIS 7才有改善)

Lab:
**安裝 AppServ 2.5.10
  • root 密碼設定為phpmysql
  • 預設網頁目錄為 C:\Appserv\www


安裝netbeans npp5.6.2


Q:MySQL 安裝在Linux, Windows 上的軟體是否可以存取?
A: MySQL 官網提供 .NET ODBC 的連接方式, 可讓Windows的軟體存取

**Netbeans
  • 針對已知的語法錯誤提出警告
  • 自動帶出語法
  • 以專案進行管理


**NotePad++
  • 純文字編輯器
  • 可以快速轉換Big5 / UTF-8
  • 可以閱讀 Linux 環境文件


**PHP 優點:
  • free
  • 可使用網路套件快速架站
  • MySQL 緊密結合
  • Linux 指令結合 ( Windows 2008及雲端亦支援 php但須用 SQL Server)




Lab: 開啟一個新專案
使用 netbeans
建立新專案
File → New Project
PHP: PHP Application → Next
點選 Browse 按鈕( source 按鈕調整於 C:\Appserv\www 目錄下 [網站根目錄] )
Finish

說明 title and body

練習 於 <? php 範圍內輸入
echo "hello";
?>

--class brake--

Lab: 說明 Big5 UTF-8 差異
使用 NotePad ++ 請將 test1.php 另存在 www 目錄下,
並修改 echo "hello";
改為
echo "";

來了解編碼不同的差異


**php的缺點
  • 對於Big5支援異常, 某些Big5碼中文於php會產生編碼衝突.
  • php3 ~ php5 將中文視為兩個字, php6將中文視為1個字.

   
MySQL 4.1版之後預設為UTF-8[ 之前為拉丁碼], 且針對資料存取進行編碼檢查

IE 環境可以透過 meta標籤 來判斷編碼


**網頁在 IE 會是亂碼或空白?
  • 請在php檔案內加入完整 html 語法, 並請加入 meta 標籤
  • meta 必須在 title 之前
  • 網頁必須是 UTF-8 (Byte Order Mark ) 格式
  • (可以使用 NotePad ++ 的編輯 → 轉換至 UTF-8 (檔首無BOM) )


echo    " "    ;
  • echo 會將後面的字串顯示在瀏覽器上面
  • php 的字串可以使用 ' ' 或是 " "
  • 每一行結束加上 ;
  • 大小寫有差異



**php 變數
  • 要以 $ 為開始
  • 無須宣告就可以使用
  • 變數可儲存系統或使用者輸入的資料
  • 變數與字串連結使用


Notes:
  • php 語法包在 <? ?> 之間
  • “ ” 代表著php 可以解析, php 就會分析
  • ‘  ‘ 代表php 不會解析裡面的語法
  • 為何 ’<p>’ 會有效果?, 因為<p> 是 html 語法


說明表單 <form> </form> 相關用法
表單網頁
  • 讓 user 輸入資料的網頁標籤群


<from  1  >  表單由此開始

</from> 表單結束

在from  標籤內 基本的 3 個屬性 ( 上方紅色數字 1 的位置 )
action:        資料送到哪邊?
name:        表單名稱
method:    傳送方式 [ get 或 post ]

<input  type=
text:        單行文字框
password:    密碼 
submit:    送出資料
reset:        清除資料
button:        可自訂動作的按鈕

說明  GET 與 POST 的差異, 練習相關練習

--class end--

PHP 開發使用 Netbeans IDE @ openSUSE

最近有機會旁聽 葉建榮 老師的 PHP and MySQL
因為上課的環境 是Windows 但是 大部分自己的環境都是 Linux
所以就想要在 openSUSE 上面實做

上課的開發環境是 Netbeans
openSUSE 11.2 預設的 Netbeans 版本為 6.5
透過software.opensuse.org 搜尋可以看到目前有支援 Netbeans 6.8
http://software.opensuse.org/search?q=netbeans&baseproject=openSUSE%3A11.2&lang=zh_TW&exclude_filter=home%3A&exclude_debug=true

於是就使用 單鍵安裝來安裝 Netbeans 6.8
安裝完成之後, 開啟新的專案沒有看到 PHP 的專案類別

解決的方式為

Tool  --> Plugs
在 Available Plugin 勾選 PHP 開頭的Plugin

下載安裝完成之後, 就會發現之後開啟新專案的時候就有 PHP 的專案可以選取

^__^

星期一, 7月 19, 2010

解決 Agent admitted failure to sign using the key 問題 with ssh

之前如果建立 ssh 金鑰,
只要將公鑰複製到 ~/.ssh/authorized_keys 就可以利用金鑰登入
而不需要建立密碼.

現在的 ssh 使用同樣的方法會出現錯誤訊息

Agent admitted failure to sign using the key

解決方式 使用 ssh-add 指令將私鑰 加進來
# ssh-add   ~/.ssh/id_dsa


先記起來

^__^

再來查詢相關細節

星期四, 7月 15, 2010

[ 整理草稿 ] SUSE Linux 下的 postfix 小記

SUSE Linux 下的 postfix
說明文件
/usr/share/doc/packages/postfix/html/index.html

RedHat 下面的/etc/postfix/main.cf

[root@localhost postfix]# grep -v '^#' main.cf | grep -v '^queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/libexec/postfix
mail_owner = postfix
inet_interfaces = localhost
mydestination = $myhostname, localhost.$mydomain, localhost
unknown_local_recipient_reject_code = 550
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
debug_peer_level = 2
debugger_command =
PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
xxgdb $daemon_directory/$process_name $process_id & sleep 5
sendmail_path = /usr/sbin/sendmail.postfix
newaliases_path = /usr/bin/newaliases.postfix
mailq_path = /usr/bin/mailq.postfix
setgid_group = postdrop
html_directory = no
manpage_directory = /usr/share/man
sample_directory = /usr/share/doc/postfix-2.3.3/samples
readme_directory = /usr/share/doc/postfix-2.3.3/README_FILES

SUSE Linux下面的 /etc/postfix/main.cf

linux-lab:/etc/postfix # egrep -v '^#|^$|^\ ' main.cf
queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/lib/postfix
mail_owner = postfix
unknown_local_recipient_reject_code = 550
debug_peer_level = 2
debugger_command =
PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
xxgdb $daemon_directory/$process_name $process_id & sleep 5
sendmail_path = /usr/sbin/sendmail
newaliases_path = /usr/bin/newaliases
mailq_path = /usr/bin/mailq
setgid_group = maildrop
html_directory = /usr/share/doc/packages/postfix/html
manpage_directory = /usr/share/man
sample_directory = /usr/share/doc/packages/postfix/samples
readme_directory = /usr/share/doc/packages/postfix/README_FILES
inet_protocols = all
biff = no
mail_spool_directory = /var/mail
canonical_maps = hash:/etc/postfix/canonical
virtual_alias_maps = hash:/etc/postfix/virtual
virtual_alias_domains = hash:/etc/postfix/virtual
relocated_maps = hash:/etc/postfix/relocated
transport_maps = hash:/etc/postfix/transport
sender_canonical_maps = hash:/etc/postfix/sender_canonical
masquerade_exceptions = root
masquerade_classes = envelope_sender, header_sender, header_recipient
myhostname = linux-lab.example.com
program_directory = /usr/lib/postfix
inet_interfaces = 127.0.0.1 ::1
masquerade_domains =
mydestination = $myhostname, localhost.$mydomain
defer_transports =
mynetworks_style = subnet
disable_dns_lookups = no
relayhost =
mailbox_command =
mailbox_transport =
strict_8bitmime = no
disable_mime_output_conversion = no
smtpd_sender_restrictions = hash:/etc/postfix/access
smtpd_client_restrictions =
smtpd_helo_required = no
smtpd_helo_restrictions =
strict_rfc821_envelopes = no
smtpd_recipient_restrictions = permit_mynetworks,reject_unauth_destination
smtp_sasl_auth_enable = no
smtpd_sasl_auth_enable = no
smtpd_use_tls = no
smtp_use_tls = no
alias_maps = hash:/etc/aliases
mailbox_size_limit = 0
message_size_limit = 10240000

要注意的是
在main.cf有提到有很多參數都在SUSEconfig.postfix中設定

-----------------------------------------------------------------------
# NOTE: Many parameters have already been added to the end of this file
# by SuSEconfig.postfix. So take care that you don't uncomment
# and set a parameter without checking whether it has been added
# to the end of this file.
# -----------------------------------------------------------------------


script 在 /sbin/conf.d/SuSEconfig.postfix | grep -v '^\ '
queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/libexec/postfix
mail_owner = postfix
inet_interfaces = localhost
mydestination = $myhostname, localhost.$mydomain, localhost
unknown_local_recipient_reject_code = 550
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
debug_peer_level = 2
debugger_command =
PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
xxgdb $daemon_directory/$process_name $process_id & sleep 5
sendmail_path = /usr/sbin/sendmail.postfix
newaliases_path = /usr/bin/newaliases.postfix
mailq_path = /usr/bin/mailq.postfix
setgid_group = postdrop
html_directory = no
manpage_directory = /usr/share/man
sample_directory = /usr/share/doc/postfix-2.3.3/samples
readme_directory = /usr/share/doc/postfix-2.3.3/README_FILES

SUSE Linux下面的 /etc/postfix/main.cf

linux-lab:/etc/postfix # egrep -v '^#|^$|^\ ' main.cf
queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/lib/postfix
mail_owner = postfix
unknown_local_recipient_reject_code = 550
debug_peer_level = 2
debugger_command =
PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
xxgdb $daemon_directory/$process_name $process_id & sleep 5
sendmail_path = /usr/sbin/sendmail
newaliases_path = /usr/bin/newaliases
mailq_path = /usr/bin/mailq
setgid_group = maildrop
html_directory = /usr/share/doc/packages/postfix/html
manpage_directory = /usr/share/man
sample_directory = /usr/share/doc/packages/postfix/samples
readme_directory = /usr/share/doc/packages/postfix/README_FILES
inet_protocols = all
biff = no
mail_spool_directory = /var/mail
canonical_maps = hash:/etc/postfix/canonical
virtual_alias_maps = hash:/etc/postfix/virtual
virtual_alias_domains = hash:/etc/postfix/virtual
relocated_maps = hash:/etc/postfix/relocated
transport_maps = hash:/etc/postfix/transport
sender_canonical_maps = hash:/etc/postfix/sender_canonical
masquerade_exceptions = root
masquerade_classes = envelope_sender, header_sender, header_recipient
myhostname = linux-lab.example.com
program_directory = /usr/lib/postfix
inet_interfaces = 127.0.0.1 ::1
masquerade_domains =
mydestination = $myhostname, localhost.$mydomain
defer_transports =
mynetworks_style = subnet
disable_dns_lookups = no
relayhost =
mailbox_command =
mailbox_transport =
strict_8bitmime = no
disable_mime_output_conversion = no
smtpd_sender_restrictions = hash:/etc/postfix/access
smtpd_client_restrictions =
smtpd_helo_required = no
smtpd_helo_restrictions =
strict_rfc821_envelopes = no
smtpd_recipient_restrictions = permit_mynetworks,reject_unauth_destination
smtp_sasl_auth_enable = no
smtpd_sasl_auth_enable = no
smtpd_use_tls = no
smtp_use_tls = no
alias_maps = hash:/etc/aliases
mailbox_size_limit = 0
message_size_limit = 10240000

要注意的是
在main.cf有提到有很多參數都在SUSEconfig.postfix中設定

-----------------------------------------------------------------------
# NOTE: Many parameters have already been added to the end of this file
# by SuSEconfig.postfix. So take care that you don't uncomment
# and set a parameter without checking whether it has been added
# to the end of this file.
# -----------------------------------------------------------------------


script 在 /sbin/conf.d/SuSEconfig.postfix

lab 測試:
1.
#vi /etc/sysconfig/postfix

註解原來的參數並使用範例(會反應在/etc/postfix/main.cf內的 mydestination)
POSTFIX_LOCALDOMAINS="\$myhostname, \$mydomain, localhost.\$mydomain"


2.使用/sbin/conf.d/SuSEconfig.postfix 來產生新的main.cf 檔
Warning! MD5DIR is not set: you probably called this script outside SuSEconfig...!
Using MD5DIR="/var/adm/SuSEconfig/md5"...
No changes for /etc/postfix/master.cf
Setting up postfix local as MDA...
Setting SPAM protection to "off"...
Installing new /etc/postfix/main.cf

(P.S 之前參考SUSE Linux Enterprise Server 10網管實戰寶典
內有提到會有產生main.cf.SuSEconfig, 實做上沒有觀察到)

3.比較一下修改之後與原來的main.cf差異
linux-lab:/etc/postfix # cat main.cf | grep ^mydes
mydestination = $myhostname, $mydomain, localhost.$mydomain

linux-lab:/etc/postfix # cat main.cf.bak | grep ^mydes
mydestination = $myhostname, localhost.$mydomain

就會發現剛剛設定的參數有反應在mydestination上面

4.在修改一下/etc/postfix/main.cf (註解只Listen 本機及加入mynetwork信任網段)
#vi /etc/postfix/main.cf

#inet_interfaces = 127.0.0.1 ::1
inet_interfaces = all
mynetworks = 10.10.36.0/24, 127.0.0.0/8

5.使用/usr/sbin/postfix check 檢查語法是否有錯 (如果沒有錯誤就沒有錯誤訊息)

linux-lab:/etc/postfix # /usr/sbin/postfix check
linux-lab:/etc/postfix #

6.重新啟動postfix

linux-lab:/etc/postfix # rcpostfix restart
Shutting down mail service (Postfix) done
Starting mail service (Postfix) done

7.觀察是否有對全部的ip 服務
linux-lab:/etc/postfix # netstat -tupln | grep :25

tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 4212/master
tcp 0 0 :::25 :::* LISTEN 4212/master

此時使用MUA 軟體(Outlook Express or Thunderbird)應該可以寄信
如果主機在mynetworks的範圍之類就可以Relay 原因為/etc/postfix/main.cf 中
smtpd_recipient_restrictions = permit_mynetworks,reject_unauth_destination

但是不能收信 --> 因為pop服務尚未啟用

linux-lab:/etc/postfix # netstat -tupln | grep :110

8.開啟pop服務
先檢查是否有裝qpopper套件,如果沒有的話使用yast 安裝(需要有CD or internet source)
linux-lab:/etc/postfix # rpm -qa | grep qp
linux-lab:/etc/postfix # yast -i qpopper

此時會使用yast自動安裝
linux-lab:/etc/postfix # rpm -qa | grep qp
qpopper-4.0.8-13.2

但是port 110尚未Listen
linux-lab:/etc/postfix # netstat -tupln | grep :110
linux-lab:/etc/postfix #

qpopper 為以xinetd 為主的服務, 類似RedHat之前的pop3d

linux-lab:/etc/postfix # ls /etc/xinetd.d/
chargen cvs echo netstat rsync swat time-udp
chargen-udp daytime echo-udp pure-ftpd servers systat vnc
cups-lpd daytime-udp fam qpopper services time

目前的狀態為關閉
linux-lab:/etc/postfix # chkconfig qpopper --list
xinetd based services:
qpopper: off

啟用qpopper
linux-lab:/etc/postfix # chkconfig qpopper on
linux-lab:/etc/postfix # chkconfig qpopper --list
xinetd based services:
qpopper: on

但是port 110也是尚未Listen 因為要重新啟動xinetd才可以
linux-lab:/etc/postfix # netstat -tupln | grep :110
linux-lab:/etc/postfix # rcxinetd restart
Shutting down xinetd: done
Starting INET services. (xinetd) done

重新啟動之後就會發現port 110 有服務在 Listen
linux-lab:/etc/postfix # netstat -tupln | grep :110
tcp 0 0 0.0.0.0:110 0.0.0.0:* LISTEN 6182/xinetd



此時使用MUA 軟體(Outlook Express or Thunderbird)應該可以收信

利用 command line 方式安裝 .ymp 檔案

最近由於安裝 nagios  套件
所以會使用 .ymp 安裝
但是又想寫成 shell script 所以想要使用指令安裝

可以透過 OneClickInstall 指令來進行安裝
#OneClickInstallCLI   http://xxxxxxx.xxxxxx.xxxxx.xxxx/xxxx.ymp

# which OneClickInstallCLI
/sbin/OneClickInstallCLI

# rpm -qf `which OneClickInstallCLI`
yast2-metapackage-handler-0.8.10-0.1.1.noarch

但是考慮有時候畫面會 延遲的狀況(整理metadata會讓使用者覺得好像hang住)
還是使用 #OneClickInstallUI  使用圖形介面的方式也許較好

^^