แสดงบทความที่มีป้ายกำกับ perl แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ perl แสดงบทความทั้งหมด

วันจันทร์ที่ 29 พฤศจิกายน พ.ศ. 2553

SSI มาจากคำเต็มว่า

SSI มาจากคำเต็มว่า Server Side Include ตัว SSI เป็นลักษณะแนวความคิดแบบ server side script คือสามารถเขียนโค้ด SSI แทรกลงไปในเอกสาร HTML ได้โดยไม่ต้องแยกโค้ดนั้นออกมาเขียนเพื่อรันเป็นอิสระเหมือน CGI ผู้อ่านที่เคยใช้ ASP (Active Server Pages) และ PHP มาก่อน จะทราบว่า ASP และ PHP  ก็เป็น server side script เช่นเดียวกัน

SSI จะยอมให้เรานำข้อมูลไปแทรกลงในเอกสาร HTML ได้อย่างง่าย ๆ โดยไม่จำเป็นต้องเขียน CGI (แต่ SSI จะเรียก CGI ให้มาทำงานก็ได้ ถ้าเราต้องการ)  ในแง่มุมของผู้พัฒนาเว็บเพจแล้ว จะรู้สึกว่า SSI เป็นเพียงแค่ แท็ก ๆ หนึ่ง ที่เขาสามารถเลือกนำไปเขียนแทรกในเอกสาร HTML ตรงไหนก็ได้เหมือนกับการเขียนแท็ก HTML ธรรมดาทั่วไป

ตัวอย่างต่อไปนี้เป็นเอกสาร
HTML ชื่อว่าไฟล์ว่า exam1.shtml (Web Server ที่กำลังใช้อยู่เป็น IIS5 และได้ enable SSI เป็น .shtml) ที่มีโค้ค SSI สั่งให้แสดงวัน-เวลาปัจจุบันของเซิร์ฟเวอร์ ดูผลลัพท์ได้ ที่นี่

exam1.shtml
----------------------------------------------------------------------
<html>
<head><title>
Insert Date & Time to HTML
</title>
</head>
<body>
    <h1>Date & Time</h1>
    <!--#echo var="DATE_local" -->
</body>
</html>
----------------------------------------------------------------------

จากตัวอย่าง exam1.shtml จะเห็นว่าการใช้ SSI นั้นง่ายมาก  เพียงแต่เขียนแท็ก <!--   --> และบอกสิ่งที่ต้องการไว้ในเอกสาร HTML เมื่อมีผู้ใช้ร้องขอเปิดไฟล์เอกสาร HTML เว็บเซิร์ฟเวอร์จะวิ่งไปตรวจดูในเอกสาร HTML ทีละบรรทัดว่ามีการเขียนโค้ด SSI ไว้หรือไม่ ถ้าพบโค้ด SSI ตำแหน่งใด ๆ ก็ตาม  เว็บเซิร์ฟเวอร์จะทำงานตามโค้ด SSI ที่เขียนสั่งไว้  แล้วเอาคำตอบที่ได้มาเขียนแทนโค้ด SSI เดิมที่มีอยู่  ดังนั้นจากตัวอย่างที่เขียนโค้ด SSI ไว้ว่า <!--#echo var="DATE_LOCAL"--> จึงเป็นการสั่งให้แสดงค่าตัวแปรที่ชื่อ DATE_LOCAL (วันที่และเวลาปัจจุบันในเซิร์ฟเวอร์) ของ SSI ออกมา

เว็บเซิร์ฟเวอร์จะหาคำตอบของโค้ด
SSI มาใส่และตัดโค้ด SSI นั้นออก (รวมทั้งแท็ก <!-- และ -->) ทางฝั่งผู้ใช้ที่ได้รับเว็บเพจไปดู  จึงไม่มีโอกาสเห็นโค้ด SSI ที่เขียนแทรกไว้ในเอกสาร HTML เลย

เว็บเซิร์ฟเวอร์รู้ได้อย่างไรว่าเป็น
SSI
สิ่งแรกที่ต้องทราบก็คือว่าเว็บเซิร์ฟเวอร์ที่เราใช้งานอยู่ ยอมให้ใช้ SSI ได้หรือเปล่า  อีกอย่างก็คือไฟล์เอกสาร HTML ที่ยอมให้ใช้ SSI ได้นั้นจะต้องเก็บไว้ด้วยนามสกุลอะไร .shtml หรือว่า .shtm จึงจะใช้งาน SSI ได้  ซึ่งโดยปกติมักจะเป็น .shtml

เว็บเซิร์ฟเวอร์หลายที่ไม่เปิดให้ใช้  SSI กันเพราะทุกครั้งที่มีการร้องขอไฟล์เอกสาร HTML (ที่สามารถใช้ SSI ได้) มายังเว็บเซิร์ฟเวอร์ เว็บเซิรฟเวอร์จะต้องทำงานหนักกว่าปกติ  คือต้องมีการตรวจดูเอกสาร HTML ทุกบรรทัด เพื่อหาโค้ด SSI ที่แทรกอยู่  แล้วหาคำตอบมาใส่แทน พร้อมกับสร้างเอกสาร HTML ใหม่ส่งกลับให้ผู้ใช้ และถึงแม้ว่าไม่มีโค้ด SSI แทรกอยู่เลยก็ตาม เว็บเซิร์ฟเวอร์คงต้องทำอย่างนี้ทุกครั้งเสมอ

เว็บเซิร์ฟเวอร์ที่ยอมให้ใช้ SSI จึงมีการแบ่งกลุ่มไฟล์เอกสาร HTML ไว้ชัดเจนว่า นามสกุลอะไรจึงจะสามารถใช้โค้ด SSI ได้ สมมุติว่าในเว็บเซิร์ฟเวอร์กำหนดให้ไฟล์นามสกุล .shtml เป็นไฟล์ที่สามารถใช้ SSI ได้ ดังนั้นถ้าเว็บเซิร์ฟเวอร์ได้รับคำร้องขอไฟล์ที่ไม่ใช่นามสกุล .shtml เข้ามา เว็บเซิร์ฟเวอร์ก็จะส่งเนื้อหาของไฟล์นั้นกลับไปให้ผู้ร้องขอได้ทันที  โดยไม่ต้องตรวจหา SSI ที่แฝงอยู่  ถึงแมจะมี SSI เขียนไว้ก็จะไม่ได้รับการตีความหมายใด ๆ

เหตุผลที่มีการแทรก
SSI ไว้ในแท็ก <!--  --> ก็เพราะถ้าเกิดในกรณีที่มีการเขียนโค้ด SSI ไว้ในไฟล์นามสกุลที่เว็บเซิร์ฟเวอร์ไม่ให้ใช้ SSI หรือมีข้อผิดพลาดใด ๆ ที่ทำให้ SSI ไม่สามารถทำงานได้ เว็บเบราเซอร์ก็จะได้ไม่ต้องเอาโค้ด SSI นั้นไปแสดงออกทางเว็บเพจ  เพราะโค้ด SSI อยู่ในแท็ก <!--  --> ซึ่งเว็บเบราเซอร์มองว่าเป็นเพียง comment เท่านั้น

ขอดีและข้อเสียของ
SSI
ข้อดีคือทำให้ผู้พัฒนาเว็บเพจทั่ว ๆ ไป สามารถนำสิ่งที่เขาต้องการแทรกลงในเอกสาร HTML ได้ โดยที่ไม่ต้องไปข้องเกี่ยวกับการเขียน CGI ให้ยุ่งยาก สำหรับนักพัฒนา CGI ก็สามารถเอา CGI ที่เขียนมาทำงานร่วมกับ SSI ได้อีกด้วย คือให้ SSI ไปเรียก CGI ทำงาน  แล้วเอาผลการทำงานของ CGI มาแทรกลงในเอกสาร HTML อีกทีหนึ่ง  สรุปก็คือ  ความง่ายและยืดหยุ่นนั่นเองที่เป็นข้อดีของการใช้ SSI

ข้อเสียก็คือทุกครั้งที่เว็บเซิร์ฟเวอร์ได้รับการร้องขอเอกสาร HTML และเอกสารนั้นกำหนดให้ใช้ SSI ได้ด้วยแล้ว  เว็บเซิร์ฟเวอร์จะต้องวิ่งหาโค้ด SSI ที่แทรกอยู่ในเอกสาร  เพื่อปรับเปลี่ยนเป็นข้อมูลแล้วนำไปสร้างเป็นเอกสาร HTML ที่สมบูรณ์  ก่อนจะส่งไปให้ผู้ที่ร้องขอ  ถ้าเอกสาร HTML นั้นมีขนาดเล็กก็ไม่เป็นไร แต่ถ้าขนาดใหญ่แล้ว จะทำให้ประสิทธิภาพในการให้บริการของเว็บเซิร์ฟเวอร์ลดลง

แต่ก็ยังไมอันตรายเท่ากับการเปิดให้ใช้
SSI แบบ EXEC (จะอธิบายต่อไป) คือยอมให้โค้ด SSI เข้าไปเรียกสคริปต์หรือ shell ในเซิร์ฟเวอร์ให้ทำงานได้  เพราะอาจจะเป็นช่องโหว่สำหรับผู้ที่ไม่ประสงค์ดีใช้เป็นช่องทางสำหรับโจรกรรมหรือกระทำการแปลก ๆ กับข้อมูลในเซิร์ฟเวอร์ได้ง่ายพอ ๆ กับ การได้สิทธิใช้โปรแกรม Telnet เข้าไปทำงานในเซิร์ฟเวอร์เลยทีเดียว

ตัวอย่างต่อไปนี้
(monitor.shtml) เป็นการใช้ SSI เพื่อไปเรียกคำสั่ง ในเซิร์ฟเวอร์ให้ทำงาน เนื่องจากเว็บเซิร์ฟเวอร์ที่กำลังใช้งานอยู่นี้เป็น Windows 2000 Server ดังนั้นคำสั่งต้องเป็นคำสั่งของ Window OS ซึ่งในตัวอย่างใช้คำสั่ง dir ถ้าเป็น UNIX OS ก็อาจจะใช้คำสั่งอื่นเช่น Finger, ls เป็นต้น

monitor.shtml
------------------------------------------------------------------------------------
<html>
<head><title>User Monitor</title>
<meta http-equiv="refresh" content ="5;url=monitor.shtml">
</head>
<body>
  <pre>
      <!--#exec cmd="dir"-->
</body>
</html>
------------------------------------------------------------------------------------

ดูผลการทำงาน ที่นี่ ครับ เนื่องจาก IIS5 บน Window 2000 Server ไม่ยอมให้มีใช้คำสั่ง
exec cmd ได้ ดังนั้นในการดูผลให้สังเกตดูด้วยนะครับว่าเป็นการดูผลที่พอร์ต 82 (http://202.129.16.28:82/ssi/monitor.shtml) ซึ่งได้ลง Apache ไว้ที่พอร์ต 82 ครับ นั่นคือ Apache อนุญาตให้คำสั่งนี้ได้

ลองคิดดูสิครับว่า จากตัวอย่างข้างบน ถ้ามีการคำสั่งอื่น ๆ ที่มีอันตรายต่อเซิร์ฟเวอร์จะเกิดอะไรขึ้น คงคิดได้นะครับ

สรุปข้อเสียของ
SSI อันดับแรกก็คือ ประสิทธิภาพและความเร็วในการทำงานเซิร์ฟเวอร์ที่อาจลดลงได้  และข้อเสียที่สำคัญคือเรื่องของความปลอดภัย  ในการใช้คำสั่ง ecex cmd

กฎเกณฑ์เบื้องต้นของการเขียน SSI
ข้อควรระวังในการเขียนโค้ด SSI ก็คือ ตัวอักษรพิมพ์เล็ก-พิมพ์ใหญ่ จะมีผลต่อการตีความหมายด้วย  และยังมีเงื่อนไขปลีกย่อยตามกฎเกณฑ์การเขียนโค้ด SSI ดังต่อไปนี้ :
  • ระหว่างแท็ก <!- กับ #command ต้องเขียนติดกันโดยไม่มีช่องว่างคือเขียนเป็น <!--#command
  • ค่าของพารามิเตอร์จะต้องคร่อมด้วยเครื่องหมายคำพูดแบบ " (Double Qoute)
  • หลังจากที่จบเครื่องหมาย " ของค่าพารามิเตอร์แล้ว จะต้องเว้นช่องว่างก่อนเขียนตัวปิด ->
สำหรับเว็บเซิร์ฟเวอร์บางตัวก็ไม่ได้ยึดตามกฎเกณฑ์นี้มากนัก  จึงต้องทดลองกันเองแต่การเขียนให้ถูกต้องตามกฎเกณฑ์จะช่วยลดปัญหาต่าง ๆที่อาจเกิดขึ้นได้ และมั่นใจได้ว่าจะนำไปใช้งานกับเว็บเซิร์ฟเวอร์อื่น ๆ ได้อย่างไม่มีปัญหาด้วย

คำสั่งของ
SSI
ในตารางต่อไปนี้แสดงคำสั่งต่าง ๆ ของ SSI ซึ่งแต่ละคำสั่งมีพารามิเตอร์และค่าพารามิเตอร์จำแนกแยกย่อยลงไปอีก
 
คำสั่ง หน้าที่การทำงาน
echo แทรกค่าตัวแปร SSI ลงในเอกสาร HTML
flastmod แทรกวันที่แก้ไขไฟล์ครั้งสุดท้ายของไฟล์ใด ๆ ลงในเอกสาร HTML
fsize แทรกขนาดของไฟล์ใด ๆ ลงในเอกสาร HTML
config กำหนดรูปแบบของเวลา, ขนาดไฟล์, และข้อความผิดพลาดที่จะแสดงออกมา
include เอาเนื้อหาของไฟล์ HTML อื่นมาแทรกลงในเอกสาร HTML
exec เรียกโปรแกรม, เชลล์ หรือสคริปต์ CGI ให้ทำงานและนำผลการทำงานแทรกลงในเอกสาร HTML
คำสั่ง echo โชว์ค่าตัวแปรของเซิร์ฟเวอร์
เริ่มจากคำสั่ง echo กันก่อน  เพราะถือว่าเป็นคำสั่งพื้นฐานของ SSI การทำงานของคำสั่ง นี้คือนำเอาค่าตัวแปร (ที่มีการกำหนดไว้ในเว็บเซิร์ฟเวอร์) มาแทรกลงในเอกสาร HTML

คำสั่งนี้ต้องการพารามิเตอร์คำว่า var และบอกชื่อตัวแปรเป็นค่าของพารามิเตอร์เท่านั้น ชื่อตัวแปรพื้นฐานของเว็บเซิร์ฟเวอร์ที่สามารถเขียนเป็นค่าของพารามิเตอร์ var ได้ แสดงไว้ในตารางข้างล่าง
 
ชื่อตัวแปร ใช้แสดงข้อมูล
DATE_LOCAL วัน-เวลา ปัจจุบันของเซิร์ฟเวอร์ในรูปแบบท้องถิ่น
DATE_GMT วัน-เวลา ปัจจุบันของเซิร์ฟเวอร์ในรูปแบบ GMT
DOCUMENT_NAME ชื่อไฟล์เอกสาร HTML ปัจจุบัน
DOCUMENT_URI URI ของเอกสาร HTML ปัจจุบัน
LAST_MODIFIED วัน-เวลา ครั้งสุดท้ายที่เอกสาร HTML ถูกแก้ไข
QUERY_STRING_UNESCAPED ข้อมูลที่ไคลเอ็นต์ส่งมาให้เอกสาร HTML แบบ GET และถอดรหัส escape ออกแล้ว
ตัวอย่างต่อไปเป็นการเขียนโค้ด SSI เรียกค่าตัวแปรต่าง ๆ ตามตาราง มาแสดงในเว็บเพจ ดูผลได้ ที่นี่

env.shtml
 <html>
<head><title>echo command</title></head>
<body>
    <pre>
    Date Local : <!--#echo var="DATE_LOCAL" -->
    Date GMT :  <!--#echo var="DATE_GMT" -->
    Document Name : <!--#echo var="DOCUMENT_NAME" -->
    Document URI :  <!--#echo var="DOCUMENT_URI" -->
    Last Modified : <!--#echo var="LAST_MODIFIED" -->
    Query String : <!--#echo var="QUERY_STRING_UNESCAPED" -->
</body>
</html>
-------------------------------------------------------------------------------------------------------

ตัวแปรที่สามารถเรียกให้มาแสดงด้วยคำสั่ง echo ของ SSI นั้นไม่ได้จำกัดเพียงแค่ชื่อตัวแปรดังตารางข้างบนเท่านั้น แต่ยังสามารถเอาชื่อตัวแปรแวดล้อมของเว็บเซิร์ฟเวอร์มาแสดงได้อีกด้วย ตัวแปรแวดล้อมที่นิยมแสดงผ่าน SSI มีดังตารางข้างล่าง
 

ชื่อตัวแปร ใช้แสดงข้อมูล
SERVER_SOFTWARE ชื่อโปรแกรมเว็บเซิร์ฟเวอร์
SERVER_NAME ชื่อของเซิร์ฟเวอร์
HTTP_USER_AGENT ชื่อโปรแกรมที่ผู้ใช้เรียกเปิดเอกสาร HTML
REMOET_ADDRESS หมายเลข IP Address ของเครื่องผู้ใช้
REMOTE_HOST ชื่อโฮสต์และโดเมนของเครื่องผู้ใช้
ตัวอย่างต่อไปนี้ (exam2.shtml) เป็นการนำค่าตัวแปรแวดล้อมไปแสดงในเว็บเพจ  ดูผลได้ ที่นี่

exam2.shtml
--------------------------------------------------------------------------------------------------------------------
<html>
<head><title>Welcome to SSI</title></head>
<body>
     <center>
     <h2>ยินดีต้อนรับสู่ SSI</h2>
     คุณมาจาก IP Address : <b><!--#echo var="REMOTE_ADDR" --></b><br>
     ชื่อโดเมน                         : <b><!--#echo var="REMOTE_HOST" --></b><br>
     เว็บเบราเซอร์                   : <b><!--#echo var="HTTP_USER_AGENT" --></b><br>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------

คำสั่ง
include ใช้ดึงเนื้อหาไฟล์อื่นมาแทรก

คำสั่ง include ถือว่าเป็นคำสั่งที่มีประโยชน์อีกคำสั่งหนึ่งของ SSI เพราะทำให้เราสามารถดึงเอาเนื้อหาของไฟล์อื่นมาแทรกลงในเอกสาร HTML ได้อย่างง่าย ๆ เว็บที่มีการอัพเดตข้อมูลบ่อย ๆ นิยมใช้วิธีนี้เพราะสะดวกมากกว่าการเข้าไปแก้เอกสาร HTML ทุกครั้งที่ข้อมูลมีการเปลี่ยนแปลง

พารามิเตอร์ของคำสั่ง
include มี 2 อย่างคือ file กับ virtual เพื่อให้เห็นตัวอย่างชัดเจนลองดูตัวอย่างสคริปต์ exam3.shtml ต่อไปนี้ และดูผลได้ ที่นี่

exam3.shtml
----------------------------------------------------------------------------------------
<html>
<head><title>Welcome to My page</title></head>
<body>
        <!--#include file="body.txt" -->
        <!--#include file="bottom.txt" -->
</body>
</html>
---------------------------------------------------------------------------------------

ในตัวอย่าง exam3.shtml มีการใช้คำสั่ง include เพื่อเรียกเอาเนื้อหาของไฟล์ body.txt และ bottom.txt เข้ามาแทรกในสคริปต์ exam3.shtml สมมุติว่าเนื้อหาของไฟล์ body.txt และ bottom.txt เป็นดังนี้

เนื้อหาของไฟล์
body.txt
--------------------------------------------------------------------------------------
<center>
<h1>ยินดีต้อนรับสู่โฮมเพจของเรา</h1>
<a href="http://www.southcat.net"<h1>Enter hear</h1></a>
</center>
-------------------------------------------------------------------------------------

เนื้อหาของไฟล์ bottom.txt
-------------------------------------------------------------------------------------
<center>
Copyright (C) 2000 , 2001 by SouthCat<br>
Contact <a href="mailto:ksorn@yahoo.com">
webmaster</a>
</center>

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

file กับ virtual ต่างกันอย่างไร
file จะใช้กับเมื่อไฟล์นั้นอยู่ในไดเร็กทอรี่ปัจจุบันหรือไดเร็กทอรี่ย่อยที่ซ่อนอยู่ในไดเร็กทอรี่ปัจจุบัน  เช่น <!--#include file="body.txt." --> เป็นการระบุไปยังตำแหน่งไฟล์ตรง ๆ

virtual จะใช้เมื่อระบุถึงไฟล์ที่อ้างอิงจากไดเร็กทอรี่เอกสาร HTML หรือ document directory ของเว็บเซิร์ฟเวอร์เช่น <!--#include virtual="/project/2000/body.txt" -->

เรียกสคริปต์ CGI ให้ทำงานด้วยคำสั่ง exec

อย่างที่บอกตั้งแต่ต้นแล้วว่า SSI สามารถเรียกให้สคริปต์ CGI ทำงานแล้วนำผลการทำงานนั้นมาแทรกลงในเอกสาร HTML ได้ คำสั่งที่ใช้เขียนโค้ด SSI คือ exec คำสั่งนี้ทำให้ CGI ทำงานร่วมกับ SSI ได้ และดึงความสามารถของทั้งสองมาใช้ประโยชน์อย่างเต็มที่

พารามิเตอร์ของคำสั่ง
exec มี 2 อย่าง คือ cmd และ cgi
  • cmd ใช้เมื่อต้องการรันคำสั่งหรือเชลล์สคริปต์ของ Unix และ Windows (กรีของ Windows ขึ้นอยู่กับ Webserver ด้วยเพรา Webserver บางชนิดอาจไม่อนุญาตให้ใช้พารามิเตอร์ cmd) โดยบอกคำสั่งที่ต้องการรันเป็นค่าพารามิเตอร์เช่น <!--#exec cmd="ls" --> คือ การเรียกใช้คำสั่ง ls เพื่อให้แสดงรายชื่อไฟล์ทั้งหมดในไดเร็กทอรี่ปัจจุบันออกมา
  • cgi ใช้เมื่อต้องการให้ CGI ทำงาน ซึ่ง CGI นั้นอาจเป็น Script หรือโปรแกรมก็ได้ เช่น <!--#exec cgi="/cgi-bin/test.cgi" --> ในกรณีที่ใช้เว็บเซิร์ฟเวอร์ที่รันใน windows ก็อาจะเรียกไปยังไฟล์ CGI ที่มีนามสกุลเป็น .exe เลย เช่น <!--#exec cgi="/cgi-bin/counter.exe" --> ข้อสำคัญคือ CGI ที่ถูกเรียกให้ทำงานผ่าน SSI ด้วยคำสั่ง exec นี้จะต้องส่งผลการทำงานออกมาเป็น text/html เท่านั้น จะเป็นรูปกราฟฟิกหรือไฟล์รูปแบบอื่นไม่ได้
การเรียกสคริปต์ที่เป็นภาษา Perl
ตัวอย่างต่อไปนี้ เป็นการเรียกสคริปต์
CGI ให้ทำงาน ซึ่งสคริปต์นี้เขียนด้วยภาษา perl ครับ (ใช้นามสกุลเป็นเป็น .cgi ซึ่งคุณอาจจะใช้เป็นนามสกุลอื่นก็ได้เช่น .pl ขึ้นอยู่กับการเซ็ตเว็บเซิร์ฟเวอร์ของคุณครับ) นั่นคือถ้าเราต้องการเขียน CGI สำหรับนับจำนวนครั้งของการเปิดเว็บเพจ หรือที่เรียกว่า เคาน์เตอร์ ให้แสดงตัวเลขออกมาเป็นข้อความ ก็จะได้สคริปต์ counter.cgi ต่อไปนี้

counter.cgi
----------------------------------------------------------------------------------------------------------
#!C:/perl/bin/perl.exe

$filecounter = "/southcat/ssi/counter.dat";
open (FILE, "$filecounter") || die "Error open $filecounter [$!]\n";
$count = <FILE>;
close( FILE);

$count++;
open( FILE, ">$filecounter") || die "Error create file $filecounter [$!]\n";
print FILE $count;
close( FILE);

$count = sprintf("%05d", $count);
print "Content-type:text/html\n\n";
print $count;
---------------------------------------------------------------------------------------------------------

ก่อนที่จะรันสคริปต์นี้ให้ทำงาน ต้องสร้างไฟล์ว่างที่ชื่อว่า counter.dat  ก่อนนะครับ และสามารถดูผลการทำงานของไฟล์ได้ ที่นี่ และเมื่อต้องการเรียกไฟล์นี้ผ่านโค้ด SSI เราก็ต้องสร้างไฟล์ที่สามารถเรียกโค้ด SSI ได้ ดังนี้ครับ

exam4.shtml
-----------------------------------------------------------------------------------
<html>
<head><title>Welcome to My Page</title></head>
<body>
    <center>
    <h2>ยินดีต้อนรับสู่โฮมเพจของเรา</h2>
     คุณเป็นคนเยี่ยมชมลำดับที่ # <!--#exec cgi="/ssi/counter.cgi" -->
    </center>
</body>
</html>
--------------------------------------------------------------------------------------------------------

ดูผลการทำงานของ exam4.shtml ได้ ที่นี่

การเรียกสคริปต์ที่เป็นภาษา
PHP
ตัวอย่างต่อไปนี้ เป็นการเรียกสคริปต์ CGI ที่เขียนด้วยภาษา php (ex_php_script.php) ให้ทำงาน ซึ่งเป็นตัวอย่างของการแสดงค่าของสูตรคูณของเลข 2 และสามารถดูผลการทำงานของไฟล์ ex_php_script.php ได้ ที่นี่

ex_php_script.php
------------------------------------------------------
<?
$a=1;
for ($a=1; $a<13 ; $a++)
  {
    echo "2 x $a = ",2 * $a, "<br>";
  }

?>
------------------------------------------------------

และเมื่อนำไฟล์นี้ไปเรียกใช้งานผ่าน SSI จากไฟล์ exam5.shtml ดังตัวอย่างข้างล่าง ก็จะได้ผลลัพท์ ดังนี้ ครับ

exam5.shtml
---------------------------------------------------------------------------------------------------------
<html>
<head><title>Welcome to My Page</title></head>
<body>
    <center>
    <h2>ยินดีต้อนรับสู่โฮมเพจของเรา</h2>
     สูตรคูณเม่ 2<hr><!--#exec cgi="/ssi/ex_php_script.php" --><hr>
    </center>
</body>
</html>
--------------------------------------------------------------------------------------------------------

การเรียกสคริปต์ที่เป็นภาษา ASP
เรามักไม่เคยเห็นการเรียก ASP Script ผ่าน SSI กันนะครับ และจริง ๆ แล้วก็ไม่รู้เขานิยมใช้กันหรือเปล่า แต่ผมลองทดลองเรียก ASP Script ผ่าน SSI Code จากไฟล์ที่มีสกุลเป็น .sthml ได้เช่นกันครับ

ตัวอย่างต่อไปนี้เป็น ASP Script (testasp.asp) ครับ ซึ่งเป็นตัวอย่าง ASP แบบง่าย ๆ คือเป็นการวนลูปของคำสั่ง for และให้พิมพ์คำว่า "Hello" จำนวน 5 ครั้ง ดูผลการทำงานได้ ที่นี่

testasp.asp
----------------------------------------------------------------------------------------------
<%
response.write "<br>"
for n=1 to 5
response.write("Hello " & n & "<br>")
next
%>
---------------------------------------------------------------------------------------------

เราสามารถเรียกไฟล์นี้ให้ทำงาน ผ่าน SSI code จากไฟล์ exam6.shtml ได้ดังตัวอย่างต่อไปนี้ :

exam6.shtml
--------------------------------------------------------------------------------------------
<html>
<head><title>Welcome to My Page</title></head>
<body>
<center>
<h2>ยินดีต้อนรับสู่โฮมเพจของเรา</h2>
ทดสอบ <hr> <!--#exec cgi="/ssi/testasp.asp" --><hr>
</center>
</body>
</html>
-------------------------------------------------------------------------------------------

ดูผลการรันได้ ที่นี่ จากผลการรันจะเห็นว่ามีข้อความที่ไม่ต้องการเกิดขึ้นด้วย คือข้อความต่อไปนี้ "
HTTP/1.1 200 OK Server: Microsoft-IIS/5.0 Date: Fri, 08 Feb 2002 03:14:30 GMT Connection: close Content-Type: text/html" ใครรู้วิธีลบข้อความนี้ออกช่วยบอกด้วยก็แล้วกันนะครับ

การเรียกสคริปต์หลายภาษาจากไฟล์ ๆ เดียว
ไฟล์ที่สามารถใช้ SSI Code ได้ เช่นไฟล์ที่มีนามสกุลเป็น .shtml สามารถจะเรียก CGI script ของหลายภาษาจากไฟล์ ๆ เดียวได้ ตัวอย่างต่อไปนี้ (exam7.shtml) มีการใส่ SSI Code ให้เรียก CGI script ทั้ง 3 ภาษาคือ perl,php และ asp ดูผลการรัน ที่นี่

exam7.shtml
------------------------------------------------------------------------------------------

<html>
<head><title>Welcome to My Page</title></head>
<body>
<center>
<h2>ยินดีต้อนรับสู่โฮมเพจของเรา</h2><hr>
ส่วนนี้เป็นผลจากการเรียกสคริปต์ภาษา Perl<br>
คุณเป็นคนเยี่ยมชมลำดับที่ # <!--#exec cgi="/ssi/counter.cgi" --><hr>
ส่วนนี้เป็นผลจากการเรียกสคริปต์ภาษา php<br>
<!--#exec cgi="/ssi/ex_php_script.php" --><hr>
ส่วนนี้เป็นผลจากการเรียกสคริปต์ภาษา asp<br>
<!--#exec cgi="/ssi/testasp.asp" --><hr>
</center>
</body>
</html>

Local debugging PERL CGI program

Platform in its own debugging PERL CGI program, the Chinese Win98, for example, need to do the following:

1. To install, set up TCP / IP protocol

2. To install, set up Web server

3. Install PERL for Win32

Detail the various steps:

1, the installation, set TCP / IP protocol

If you have the right to connect via a dialup Internet, it shows your TCP / IP has been installed correctly, but you have to patience to watch yo! Into the "Control Panel", double-click "Network" icon, a "network" dialog box, where have installed TCP / IP. Suppose TCP / IP does not exist, to add it, just click "Add" button, select the "Agreement" and click "Add" button, select "Microsoft" - "TCP / IP", click "OK", the system Tips to Win98 disk, then restart, according as it wants. But wait, this step is not over, but also on the TCP / IP for a number of settings. Select "TCP / IP", click "Properties." In the "DNS Configuration", select "Disable DNS"; in the "IP address" option, select "Specify IP Address" in the "IP Address" enter any valid IP address, such as "10.10.10.10". Thus, installing the Web service, you can in your browser using "http://10.10.10.10" This address local browsing! Be explained that, if you choose the "automatic IP address", then you can only use "http:// to your machine name" the. How to set the machine name? Click the "identity", where "hh" is the machine name, and to emphasize that the Chinese do not use the machine name, otherwise can not be correctly identified and displayed, do remember that Oh!

You might ask, just select "Disable DNS", and "the specified IP address", then dial the Internet when in reality do not go wrong? Yes, in fact, to solve this problem is very simple, you just in the "My Computer", double-click "dialing network", select the connection you created, right-open the "Properties" dialog box, select the "Server Type", click "TCP / IP Settings", where you can select "IP address of the server has been assigned" in the "Specify name server address" enter your ISP provided in the "master DNS" and "secondary DNS" address can, and here I filled the local 163 of the DNS address.

Second, installation, set the Web server

1, Chinese Win98 CD treasure hunt

To install the Web server, but the Web server program come from? Win98 CD in to see what, here is a "add-ons" directory, go see another one "pws", in fact treasures have found that the "pws", which is short for Personal Web Server is "Personal Web Server," We use it to install the Web server on it (in fact, there are many Web server program, here it is more convenient.)!

2, the installation Web server

Before you install the PWS, the system must be IE4.0 or later browser has integrated Chinese Win98

IE4.0, this step can be omitted. Chinese Win98 CD into the "add-onspws" directory, double-click "setup.exe", select "Next", with "typical" installation of PWS to the default directory, but you can use "custom" installation of the components you need to choose the installation is complete, it will build a C under the root directory called "inetpub", the directory, this folder is very important to the future operation of all in here, you are prompted to restart, when the re-start again, you will find more than a small status bar icon, it is the "Personal Web Manager." Hey, I restarted, how will it automatically pop-up dial dialog box?

3, set the Web server

First of all, to solve the above problems. When you install PWS, it's a number of settings have been written to the registry, in every time you start Win98 will automatically run PWS, it will automatically detect network server with a browser, IE and your connection is dial the call network, so the dialog box will pop-up dial.

Right-click the desktop IE, open the "Properties" dialog box, select "Connect", select "LAN to connect to the Internet", this problem will be solved at the same time it can be used browser is an important step local information.

Double-click "PWS" System icon, click the "Start" button, Web publishing has started, quickly open the IE, in the address bar enter "http://hh", if you can open this address, then congratulations, your Web server has been installed successful.

Click the "Advanced" to open the "Advanced Options" settings box, select the "Allow Directory Browsing", double-click the "virtual directory" under the "cgi-bin" directory (if not in this directory, you can advance in the "c: inetpub "directory create a" cgi-bin "subdirectory, and then click click" ", select" Add ", enter in the directory" c: inetpubcgi-bin ", the alias type" cgi-bin " This would be in the "virtual directory" "" under a "cgi-bin" directory), pop-up "Edit List" dialog box, you can see, it's true path is "C: Inetpubcgi-bin "Please set it to" implement. " In this way, you want to debug the CGI program copied to "C: Inetpubcgi-bin", you can use "http://hh/cig-bin/ program name. Cig" test your CGI program has. What, your CGI program can not be correctly now? Almost forgot an important event, that is, you have to install a PERL CGI interpreter ─ ─ PERL for Win32!

Third, install PERL for Win32

1, Download PERL For Win32 program: Pw32i316.exe, 1.47M.

It is a version of PERL For Win32, you can go to the "http://www.activestate.com" to download, there are many versions of PERL For Win32, you can go to download to my home, my home address : "http://comenet.yeah.net".

2, install PERL For Win32, this operation is relatively simple.

Executive Pw32i316.exe, it is a self-extracting compressed file, extract to "c: perl" automatically after installation, the system questions were:

(1). Do you wish to proceed? Enter "Y"

It will be added in the registry path information related to the PERL.

(2). Modify search path? Enter "Y"

It is set in the Autoexec.bat in the PERL path. After the restart the machine, according to my practice, should be able to run a CGI program.


3, compiled a simple PERL CGI program a try.

Create a new file with Notepad, named test.cgi (also named test.pl), then Copy the following code into, then save it to "c: inetpubcgi-bin" directory. The following example ,"#!/ usr / bin / perl "and unnecessary for this case, because the system to install Perl for Win32 Perl interpreter has joined the association, in actual use, if this program runs on UNIX hosts This phrase has specified Perl interpreter position, is essential.

#! / Usr / bin / perl


print "Content-type: text / htmlnn";


# Top of this statement is to specify the server protocol between the browser, this example specifies the output data type


# To text / html, is a HTML file


print "";


print "";


print "";


print "";


print " OK, a success! ";


print "";


exit; # exit


Open a browser, enter "http://hh/cig-bin/test.cgi", a success! .

Do not tell me you did not succeed, Well, if this is true, unfortunately, you have to adapt Win98 registry, and in fact this is no big deal, the two sentences.

Method is to run the registry editor "regedit", then click Open:

HKEY_LOCAL_MACHINESystemCurrentContrlSet ServicesW3SVCParametersScript Map, by adding ". Cgi'C: PERLBINperl.exe% s% s '" and ". Pl'C: PERLBINperl.exe% s% s'". Please note, "C: PERLBINperl.exe" after the installation Pw32i316.exe, perl.exe path. Quit Registry Editor, restart your computer, enter "c: inetpubcgi-bin" directory from select just created "test.cgi" file, double-click it, then the system will see a list of window asking what program to open it using kinds of files, found in the file list "perl (if you can not find the" other "item specified by path, which is" c: perlbinperl.exe "), and select" Always use the software to open this type The file "item, click" OK "button.

Note that in the case of the browser "OK, a success!" There will be a line "Content-type: text / html", it does not matter, when the CGI program to run on other operating systems, this sentence will disappear ! In addition, Win98/95 system does not support the flock PERL statement, encountered when you use the "#" it commented, the actual use and then recovered.

Fourth, and finally added

After the some operations, you will find, to dial the Internet has been inaccessible now. Do not worry, your property as long as IE browser's "connection" to "use a modem to connect to the Internet", you can also surf the Internet! At this point, you're done! As for Chinese Win95, operating similar.

Now you can, easily prepared in the local PERL CGI debugging their own programs, I feel really great!

เปิดใช้งาน execCGI

 httpd.conf


 


 ไฟล์นี้ถ้าเป็น วินโดร จะอยู่ใน apache


 


ถ้าเป็น linux จะอยู่ในอืมจำไม่ค่อยได้ 


 /usr/local/apache2 


/etc/lib/apache/xxxxdefult  


เหอ ๆ เดาๆเอานะ




 


<Directory รูทที่ต้องการรัน >
  Options +ExecCGI
  Order Allow,Deny
  Allow from all           
  AllowOverride none
</Directory>

เทคนิคการใช้ SSI สั่งให้ CGI ทำงานและ Include File

       SSI ย่อมมาจาก ( Server Side Includes) เทคนิคนี้นิยมใข้กันมาก โดยเว็บเราเองก็ใช้ โดยปกติการใช้ SSI นั้นจะต้องอยู่บน File นามสกุล .shtml หรือ .asp แต่สำหรับ .asp นั้นจะใช้บางคำสั่งไม่ได้ อย่าพึงอ่านแล้วงง กำลังจะอธิบายให้ฟังครับ SSI เป็นเหมือนการเขียน Code แทรกไปในคำสั่ง HTML โดย Code นั้นไม่จำเป็นต้อง Execute (ประมวลผล) โปรแกรม จะคล้ายๆการทำงานของ ASP (Active Server Pages) คือการแทรก Script ลงใน HTML เรียกกันว่า Server Side Scripts มาดูตัวอย่างกันดีกว่า
แสดงวันที่ปัจจุบันของ Server <!–#echo var=”DATE_LOCAL” –>
ผลลัพธ์
แสดงวันที่ปัจจุบันของ Server
อย่าพึงงงนะทีวันที่ไม่ตรง พอดีเราใช้ Host ที่อยู่เมืองนอกเวลาเลยเป็นของเมืองนอกนะก็จะเป็นแบบนี้ หรือนิยมใช้มากคือการใช้ Include นั้นคือ สมมุติว่าเว็บของท่านมีส่วนของด้านบนของเว็บเหมือนกันหมดในทุกๆหน้า ท่านก็สามารถเก็บ Code HTML ในส่วนของด้านบนไว้ใน File นึงและใช้ SSI เรียกขึ้นมา ก็จะทำให้ท่านไม่จำเป็นของเขียน Code นั้นซ้ำๆกันในทุกๆหน้าเพียงแต่เรียก File ที่เก็บไว้มา อย่าพึงงงดูตัวอย่างกัน
<a href=”/menu1.shtml”>เมนูที่ 1</a><br>
<a href=”/menu2.shtml”>เมนูที่ 2</a><br>
<a href=”/menu3.shtml”>เมนูที่ 3</a><br>
<a href=”/menu4.shtml”>เมนูที่ 4</a><br>
<a href=”/menu5.shtml”>เมนูที่ 5</a><br>
สมมุติว่าเว็บท่านมีเมนูด้านข้างเหมือนกันหมดคืออย่างข้างบน เราก็เอา Code ดังกล่าวเก็บไว้ใน File นึงสมมุติว่าเป็น menu.inc และในทุกหน้าของเว็บเมื่อต้องการเขียน Code ส่วนของตรงนี้ก็ให้ใส่ใช้
<!–#include virtual=”/menu.inc” –> แทนจะเห็นว่าทำให้ท่านสะดวกในการ update ด้วยเวลาแก้ท่านก็แก้ที่ File menu.inc เท่านั้นก็ทำให้เมนูแก้หมดในทุกหน้า โดยทั่วไปก็อาจจะไม่น้อยอย่างนี้นะก็จะเยอะกว่านี้ก็ทำให้ประหยัดเนื้อที่ เก็บเว็บไซต์ได้อีก เพียง File เดียวเห็นไหมครับประโยชน์มหาศาล
อีกส่วนหนึ่งที่นักพัฒนามักใช้กันมากนั้นคือการรัน CGI โดยใช้ SSI (ใช้กับ .asp ไม่ได้นะ) ปกติแล้วเวลามีการเรียกใช้ Perl นั้นท่านต้องเรียก .cgi หรือ .pl โดยตรงใช่ไหม แต่ถ้าใช้ SSI ท่านไม่จำเป็นต้องทำอย่างนั้นเพียงใช้คำสั่งด้านล่างนี้
<!–#exec cgi=”/cgi-bin/ชื่อ file.cgi” –>
ก็จะทำให้ CGI ตัวนั้นทำงาน โดย CGI ที่ท่านเขียนไว้อาจจะเป็นแสดงวันที่ หรือเป็น Webboard ก็ได้แต่ต้องดูด้วยนะครับว่า Web Hosting ที่ท่านใช้ลองรับการทำงานหรือเปล่า เพราะบางทีเขาจะไม่ให้ใช้ในส่วนของ exec เพราะมันจะทำอะไรได้หลายๆอย่าง อันตรายต่อ Server เขาว่างั้น
ขอบคุณบทความดีๆจาก http://www2.twebmaster.com

ขอเสริมนิดหนึ่งคือ ที่เขาไม่ค่อยนิยมให้รันเพราะว่ามันเป็นการเปิดให้สามารถ รันคำสั้ง บนระบบปฏิบัติการได้ จนสามารถคบคุม การทำงานได้ จึงไม่ค่อยมีโฮล์พวกนี้ ด้วย