This page looks plain and unstyled because you're using a non-standard compliant browser. To see it in its best form, please upgrade to a browser that supports web standards. It's free and painless.

Album | Blog | Comment | Profile | Control Panel
標題列表  簡短摘要
whistle | 15 November,2005 14:50

本文係引用自雙子貓世界  Apache/PHP on Windows XP 

很久以前我在 newsgroup 上問過一個很白痴的問題,大意是為什麼我的 SSI 只能在網站上看到,在硬碟端卻起不了作用?對方的回答也很妙:既然名為 SSI (server side includes) 當然就是要在 server 上才能跑啊&哈哈哈我怎麼就沒想到呢(乾笑)

順便介紹一下本站的寫法吧。我其實是對 programming 一竅不通的人,更不喜歡為了技術而技術。這是個單純的個人網站,用最陽春的 HTML+CSS 就已經綽綽有餘。因此,全站都是靜態的網頁,副檔名也一直使用最傳統的 .html。

但問題來了。隨著網站規模愈來愈大,我開始需要簡單的共用元件。至少我不想再為了每過一個年就得改一次 copyright 的時間,也不想為了一個一年三百六十四天不會變的年份去用到不需要的 javascript 了!

由於租用的是 Unix 系的主機,PHP 是最自然的選擇。但我不想把一整站的 .html 手動改成 .php(還得用我不喜歡的 mod_write 去改寫路徑,等 google 更新網址&),天啊你知道這個站有多大嗎(大叫)如果五年前有人警告過我的話,我也許會考慮所謂的 future proof URLs, 但事到如今也來不及了。所幸,我們有無所不知的 google, 還有無所不能的 Apache; 只要經過適當的設定,.html 也能執行 PHP:

with httpd.conf and php.ini access

在 httpd.conf 的 mod_mime.c 段落中加入:
AddType application/x-httpd-php .html

在 php.ini 加入:
include_path = ".:/path/to/folder"

再 restart apache 即可。

without httpd.conf and php.ini access

在 .htaccess 中加入:
AddType application/x-httpd-php .html
php_value include_path .:/path/to/folder
即可。

以上是 server 端的做法。那麼,回到一開始的問題:要怎樣在自己的電腦上執行 server 端限定的服務呢?答案很簡單:把自己的電腦也當成 server. 參考了 Dave Shea 的 Virtual Hosts for Dummies 一文,再加上 google 大神的無邊法力,昨天終於在小深上成功架起了 Apache 和 PHP. 是的,Windows 也可以玩 Apache(心)由於線上文件多以 Mac 為主,XP 的部分比較零散,簡單整理一下昨天遇到的幾個狀況:

Apache 1.3/PHP 4.3 on Windows XP Home

  • 下載 Apache 安裝檔,直接執行即可。需要設定的地方,用 localhost 搪塞就可以了。
  • 接著到 apache.exe 所在的目錄,執行 NET START APACHE 手動開啟。好像只有 XP 需要這樣搞?參考文件:apache.org: Running Apache for Windows as a Service
  • 如果要玩 virtual host 的話,除了照著 Dave 的教學去改 httpd.conf 以外,還需要在 C:\WINDOWS\System32\drivers\etc 的 hosts 中加入新的設定才會有效。
  • 接著下載並安裝 PHP. installer 不知道為什麼抓不下來,我裝的是 zip package.
  • 解壓縮後,把 dll 和 sapi 這兩個目錄的檔案全部移進根目錄(如 C:\PHP),並把 php.ini 移到 C:\WINDOWS 下面!
  • 回去改 Apache 的 httpd.conf, 指定 php.ini 的路徑和基本設定。參考文件:php.net: Apache 1.3.x on Microsoft Windows
  • 如果要在 localhost 上玩 include 的話,要修改 php.ini 的 include_path
  • 如果要讓 localhost 的 .html 也能執行 PHP, 修改 httpd.conf, 方法同上
  • 最後,記得要 restart apache :p

同理可證,如果還要在 localhost 上玩 CGI 的話,就要再去裝 Perl. 不過我不行了,所以就做到這樣就好了。



whistle | 4 August,2005 20:56

使用以下的程式碼可得到server的資訊,如絕對路徑(absolute path)
echo '

';
print_r($_SERVER);
echo '
';
?>

例如:
Array
(
[CONTENT_LENGTH] => 152
[CONTENT_TYPE] => application/x-www-form-urlencoded
[DOCUMENT_ROOT] => /home/whistle/public_html/see
[HTTP_ACCEPT] => image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
[HTTP_ACCEPT_ENCODING] => gzip, deflate
[HTTP_ACCEPT_LANGUAGE] => zh-tw
[HTTP_CACHE_CONTROL] => no-cache, max-stale=0
[HTTP_CLIENT_IP] => 210.85.115.20
[HTTP_HOST] => see.reurl.net
[HTTP_REFERER] => http://see.reurl.net/signup.php?do=signup
[HTTP_USER_AGENT] => Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
[HTTP_VIA] => 1.0 Int-1_6285-211.76.97.249
[HTTP_X_FORWARDED_FOR] => 210.85.115.20
[PATH] => /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
[REMOTE_ADDR] => 211.76.97.249
[REMOTE_PORT] => 8362
[SCRIPT_FILENAME] => /home/whistle/public_html/see/signup.php
[SERVER_ADDR] => 69.50.188.18
[SERVER_ADMIN] => webmaster@see.reurl.net
[SERVER_NAME] => see.reurl.net
[SERVER_PORT] => 80
[SERVER_SIGNATURE] => Apache/1.3.33 Server at see.reurl.net Port 80

[SERVER_SOFTWARE] => Apache/1.3.33 (Unix) mod_throttle/3.1.2 mod_jk/1.2.8 mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 PHP/4.3.11 FrontPage/5.0.2.2635
[GATEWAY_INTERFACE] => CGI/1.1
[SERVER_PROTOCOL] => HTTP/1.0
[REQUEST_METHOD] => POST
[QUERY_STRING] => do=signup
[REQUEST_URI] => /signup.php?do=signup
[SCRIPT_NAME] => /signup.php
[PATH_TRANSLATED] => /home/whistle/public_html/see/signup.php
[PHP_SELF] => /signup.php
[argv] => Array
(
[0] => do=signup
)

[argc] => 1
)


whistle | 3 August,2005 3:37

在網頁上留下email,很容易成為垃圾郵件的目標,不如用一個代寄網頁讓人家在上面留言並以email寄給你。

Make a new file called mail.php in Notepad and copy and paste the following code:CODE






if($content)
{
$from = $_POST["from"]; //gets the name of the person
$email = $_POST["email"]; //gets their email address
$content = "This message is from $from whose email address is $email.\r\n-----------\r\n "; //little intro of your email message that you will see in your inbox
$content = $content . $_POST["content"]; //concatenates the intro with the real content
$content = wordwrap($content, 70); //message must be no longer than 70 characters per line (PHP rule), so we wrap it
mail('snlildude87@gmail.com', 'Someone Sent You Something!!!', $content); //first argument = your email address; second argument = subject of email (make it very catchy so you won't miss it); third argument = the content (DO NOT CHANGE THIS!!)
echo "Your message has been sent, and if I like you, then you will receive a reply. Thank you."; //what you want to tell the user after sending
}
?>


Name:


Email:


Message:







------------------------------------------------------------------------------------------

In this case, the best choice would probably be to use the values entered as the users' name and email. For example:
CODE

mail(
'snlildude87@gmail.com',
'Someone Sent You Something!!!',
$content,
"From: $from <$email>"
);

-------------------------------------------------------------------------------------------

CODE

$headers = "MIME-Version: 1.0\n"; // don't change
$headers .= "Content-type: text/html; charset=iso-8859-1\n"; // don't change
$headers .= "X-Priority: 1\n"; // don't change
$headers .= "X-MSMail-Priority: High\n"; // don't change
$headers .= "X-Mailer: php\n"; // change php in what you like
$headers .= "From: \"" . $from . "\" <" . $email . ">\n";
$headers .= "Reply-To: \"". $from ."\" <". $email .">\n";Now you don't even have to copy/paste the users e-mail adress, you can directly click reply and send them an e-mail.

To make it clear, replace CODE
mail('snlildude87@gmail.com', 'Someone Sent You Something!!!', $content);

with CODE
mail('youremail@yourdomain.com', 'Someone Sent You Something!!!', $content);
and put all the $headers in front of that.