UNIX °è¿¿¡¼ »ç¿ëµÇ´Â À¥ÇÁ·Î±×·¥ ¾ð¾î·Î´Â
C, Perl, PHP µîÀÌ Àִµ¥ Àå´ÜÁ¡À» °³ÀÎÀû °ßÇØ·Î ºñ±³Çغ¸¸é ´ÙÀ½°ú
°°´Ù.
C´Â ÄÚµù¼Óµµ¿Í ¶óÀ̺귯¸® µî¿¡¼ ¿¼¼¸¦ º¸ÀÌ´Â
¹Ý¸é ½ÇÇà¼Óµµ°¡ ºü¸£´Ù´Â ÀåÁ¡ÀÌ ÀÖ´Ù.
PerlÀÇ °æ¿ì °ÅÀÇ ´ëºÎºÐÀÇ UNIX ½Ã½ºÅÛ¿¡ ±âº»ÀûÀ¸·Î ¼³Ä¡µÇ¾î
ÀÖ¾î ½Ã½ºÅÛ°£ ȣȯ¼ºÀÌ ¶Ù¾î³ª°í ÄÚµù ¼Óµµµµ ºü¸£Áö¸¸ ÀÎÅÍÇÁ¸®ÅÍ
¾ð¾î Ư¼º»ó ½ÇÇà¼Óµµ°¡ ¾à°£ ´À¸®°í ´Ù¼ö ÇÁ·Î±×·¥ÀÌ µ¿½Ã¿¡ ½ÇÇàµÉ
°æ¿ì ¼¹ö¿¡ ºÎ´ãÀÌ µÉ ¼ö ÀÖ´Ù. PHP´Â ÄÚµù ¼Óµµ¿Í ½ÇÇà¼Óµµ
¸é¿¡¼ ¿ìÀ§¸¦ º¸ÀÌÁö¸¸ À¥¼¹ö¿Í Á¶È¸¦ ÀÌ·ï¾ß ÇϹǷΠ¹ü¿ë ÇÁ·Î±×·¥À¸·Î
¹èÆ÷Çϱ⿣ ºÎÀûÀýÇÑ µ¥´Ù ºñ´ëÇÑ À¥¼¹ö°¡ µ¿ÀÛÇÏ°Ô µÇ¾î ¼¹ö¿¡
ºÎ´ãÀÌ µÉ ¼ö ÀÖ´Ù.
ÇÊÀÚ°¡ ´Ù¸¥ ¾ð¾îº¸´Ù PerlÀ» ÁÖ·Î »ç¿ëÇÏ´Â
ÀÌÀ¯´Â ÄÚµù¼Óµµ¿Í ½Ã½ºÅÛ µµ±¸³ª ¹®ÀÚ¿ Á¶ÀÛµµ±¸·Î »ç¿ëÇÒ ¼ö
Àֱ⠶§¹®ÀÌ´Ù.
¥±. CGI Using Perl
»ý¼ÒÇÒÁö ¸ð¸£Áö¸¸ ´ÙÀ½ÀÇ ¼Ò½º´Â º»½©(bash)
½ºÅ©¸³Æ®¸¦ ÀÌ¿ëÇÑ °£´ÜÇÑ CGIÀÌ´Ù.
#!/bin/bash echo Content-type: text/html
echo echo ¡°bash¸¦ ÀÌ¿ëÇÑ CGIÀÔ´Ï´Ù.<BR>¡±
echo ¡°Á¢¼ÓÁÖ¼Ò : $REMOTE_ADDR¡±
´ÙÀ½Àº C¸¦ ÀÌ¿ëÇÑ CGIÀÌ´Ù. ¼Ò½º¸¦ way.c¶ó´Â
ÆÄÀÏ¿¡ ÀúÀåÇÑ ÈÄ ÄÄÆÄÀÏ($ gcc -oway.cgi way.c)Çؼ À¥»ó¿¡¼
½ÇÇàÇغ¸¸é °£´ÜÇÑ °á°ú°¡ Ãâ·ÂµÈ´Ù.
main() { printf (¡°Content-type:
text/html¡¬n¡¬n¡±); printf (¡°C¸¦ ÀÌ¿ëÇÑ CGIÀÔ´Ï´Ù.<BR>¡±);
printf (¡°Á¢¼ÓÁÖ¼Ò : %s¡±, getenv(¡°REMOTE_ADDR¡±));
}
ÀÌ·¸µí ¸¹Àº ¾ð¾î³ª ½©½ºÅ©¸³Æ®¸¦ ÀÌ¿ëÇØ CGI¸¦
¸¸µé¾î³¾ ¼ö ÀÖ´Ù.
1. Perl CGI ¸Àº¸±â
Perl·Î À¥»ó¿¡¼ ƯÁ¤ ¹®ÀÚ¸¦ Ãâ·ÂÇϱâ À§Çؼ´Â
³»¿ëÀ» Ãâ·ÂÇϱâ Àü¿¡ ´ÙÀ½°ú °°Àº Çì´õ¸¦ Ãâ·ÂÇÏ¿© Çü½ÄÀ» ¼±¾ðÇØÁà¾ß
ÇÑ´Ù.
°£È¤ ´º½º±×·ì(han.comp.lang.perl)¿¡ ¿Ã¶ó¿À´Â
Áú¹® Áß ÀûÁö ¾Ê´Â Áú¹®ÀÌ ÀÌ·± »ç¼ÒÇÑ ¹®Á¦°¡ ¿øÀÎÀÎ °æ¿ì°¡ ¸¹´Ù.
ƯÈ÷ ¡°Internal Server Error(500)¡±´Â ´ëºÎºÐ À¥¼¹ö Error log³ª
±¸¹®Á¡°Ë ¿É¼Ç($ perl -c ÆÄÀϸí)À» ÅëÇØ ¿øÀÎÀ» ÆľÇÇÒ ¼ö ÀÖ´Ù.
´ÙÀ½ ¼Ò½º¸¦ way.cgi ¶ó´Â ÆÄÀÏ·Î ÀúÀåÇÑ ÈÄ ½ÇÇà±ÇÇÑÀ» ºÎ¿©(chmod
+x)Çϸé À¥ºê¶ó¿ìÁ®·Î ¡°Hello, Way!¡±¶ó´Â ³»¿ëÀ» Á¢ÇÒ ¼ö ÀÖ´Ù.
#!/usr/bin/perl print ¡°Content-type:
text/html¡¬n¡¬n¡±; print ¡°<H1>Hello, Way!</H1>¡¬n¡±;
ƯÁ¤ URL·Î Á¢¼ÓÀ» Àüȯ(Redirect)Çϱâ À§Çؼ´Â
´ÙÀ½°ú °°ÀÌ »ç¿ëÇÑ´Ù. º¸Åë http:// µîÀÇ ÇÁ·ÎÅäÄÝ ÁöÁ¤ ¾øÀÌ »ó´ë
URL·Î »ç¿ëÇÒ ¼öµµ ÀÖÁö¸¸ ¿øÄ¢ÀûÀ¸·Î ÇÁ·ÎÅäÄݱîÁö Æ÷ÇÔÇؼ Àû¾îÁà¾ß
ÇÑ´Ù. »ý·«ÇÒ °æ¿ì ¿À¶óŬ À¥¼¹ö, PWS ±¸¹öÀü µîÀÇ ÀϺΠÀ¥¼¹ö¿¡¼´Â
¿¡·¯°¡ ¹ß»ýÇϱ⵵ ÇÑ´Ù. ´ÙÀ½ ¼Ò½º°¡ ºê¶ó¿ìÁ®·Î ½ÇÇàµÇ¸é ÁöÁ¤µÈ
URL·Î Á¢¼ÓÀÌ ÀüȯµÈ´Ù.
2. ¾ÆÆÄÄ¡ À¥¼¹ö ¼³Á¤
¸¸¾à »ç¿ëÇÏ´Â À¥¼¹ö¿¡¼ CGI ½ÇÇàÀ» Çã¿ëÇÏÁö
¾Ê°Å³ª CGI ÆÄÀÏ¿¡ ½ÇÇà±ÇÇÑÀÌ ºÎ¿©µÇÁö ¾Ê¾ÒÀ» °æ¿ì ¡°Forbidden(403)¡±À̳ª
¡°Method Not Allowed(405)¡± ¿¡·¯°¡ ¹ß»ýÇÒ °ÍÀÌ´Ù. °æ¿ì¿¡ µû¶ó
Perl ¼Ò½º°¡ ºê¶ó¿ìÁ®¿¡ ±×´ë·Î Ãâ·ÂµÇ±âµµ ÇÑ´Ù.
¡Ø ¾ÆÆÄÄ¡ À¥¼¹ö CGI ½ÇÇà¿À·ù ÇØ°á¹æ¹ý
°¡. AddHandler cgi-script .cgi ºÎºÐÀÇ ÁÖ¼®
Á¦°Å ³ª. <Diretory> ¼³Á¤ Áß Options ¿¡ ExecCGI Ãß°¡
<Directory ¡°/usr/local/apache/htdocs¡±>
Options Indexes Includes ExecCGI AllowOverride
FileInfo AuthConfig </Directory>
´ÙÀ½Àº °èÁ¤»ç¿ëÀÚÀÇ À¥¹®¼ °æ·Î¿¡ CGI½ÇÇàÀ»
Çã¿ëÇÏ´Â ¼³Á¤ÀÌ´Ù. <Directory /home/*/public_html>
Options Indexes Includes ExecCGI AllowOverride
FileInfo AuthConfig </Directory>
|
´Ù. ScriptAlias ¼³Á¤
ScriptAlias /cgi-bin/ ¡°/usr/local/apache/cgi-bin/¡±
(VirtualHostÀÇ °æ¿ìµµ Àû¿ëÀÌ °¡´ÉÇÏ´Ù.)
¡¤ScriptAlias¶õ? 1. ½ÇÁ¦
µð·ºÅ丮¸¦ °¡»ó µð·ºÅ丮·Î ÀνÄÇϵµ·Ï ÇÑ´Ù.(Alias,
º°Äª ±â´É) 2. µð·ºÅ丮 ³»ÀÇ ÆÄÀÏÀ» ºê¶ó¿ìÀú·Î
Á¢±ÙÇÏ¸é ¹«Á¶°Ç
½ÇÇà ÇÁ·Î±×·¥(CGI)À¸·Î Ãë±ÞÇÏ¿© ½ÇÇàÇÑ´Ù.
3. º¸¾È»ó HTML ¹®¼ µîÀº ÆÛ¹Ì¼Ç ¿¡·¯³ª ¼¹ö¿¡·¯°¡
¹ß»ýÇÏ°í À̹ÌÁö´Â ³ªÅ¸³ªÁö
¾Ê´Â´Ù.
|
3. FORM ű×
HTML ¹®¼¸¦ ÀÌ¿ëÇØ CGI¿¡ Á¤º¸¸¦ Àü´ÞÇϱâ À§ÇØ
FORM űװ¡ »ç¿ëµÈ´Ù. FORM ű״ ¿©´À HTML ÅÂ±×¿Í ¸¶Âù°¡Áö·Î
<FORM>À¸·Î ½ÃÀÛÇÏ¿© </FORM>À¸·Î ³¡ÀÌ ³´Ù.
<FORM ACTION=¡±¿¬°áÇÒ CGI URL¡± METHOD=GET>
TARGETÀ» ÁöÁ¤Çϰųª NAMEÀ» ÁöÁ¤ÇÒ ¼ö ÀÖÀ¸¸ç,
´ÙÀ½°ú °°ÀÌ ÆÄÀÏ ¾÷·Îµå¸¦ À§ÇÑ ¼±¾ðµµ °¡´ÉÇÏ´Ù.
<FORM ACTION=¡±¿¬°áÇÒ CGI URL¡± METHOD=POST
ENCTYPE=¡±multipart/form-data¡±>
´ÙÀ½°ú °°ÀÌ FORM ÅÂ±× ³»ºÎ¿¡ <INPUT>,
<SELECT>, <TEXTAREA> µîÀÇ Å±׸¦ »ç¿ëÇÏ¿© ½ÇÁúÀûÀÎ
Á¤º¸¸¦ Àü´ÞÇϵµ·Ï ÇÑ´Ù.
<FORM ACTION=¡±way.cgi¡± METHOD=GET>
¼º¸í <INPUT TYPE=TEXT NAME=user><BR>
ÁÖ¼Ò <INPUT TYPE=TEXT NAME=address><BR>
¼ºº° <SELECT NAME=sex> <OPTION>³²
<OPTION>¿©
</SELECT><BR>
<INPUT TYPE=SUBMIT
VALUE=¡±Àü¼Û¡±><BR> </FORM>
¼º¸í¿¡ way¸¦ ÀÔ·ÂÇÏ°í ÁÖ¼Ò¿¡ Kwangju¸¦, ¼ºº°Àº
¡®³²¡¯À» ¼±ÅÃÇÏ¸é ºê¶ó¿ìÁ® ÁÖ¼ÒÀÔ·ÂÁÙ¿¡ ´ÙÀ½°ú °°ÀÌ ³ªÅ¸³ª¸ç
ÁöÁ¤µÈ CGI¿¡ Á¤º¸¸¦ Àü´ÞÇÏ°Ô µÈ´Ù.(GET ¹æ½ÄÀÇ ¿¹ÀÌ´Ù.)
way.cgi?user=way&address=Kwangju&sex=%B3%B2
----- ----- -------- ------- ------------- CGI
¼º¸í ÁÖ¼Ò
¼ºº°
? ±âÈ£¸¦ ±¸ºÐÀÚ·Î ÇÏ¿© ½ÇÇàµÉ CGI(ÁÂÃø)¿Í
ÀÎÀÚ(¿ìÃø)·Î ±¸¼ºµÇ¸ç °¢ ÀÎÀÚµéÀº & ±âÈ£·Î ¿¬°áµÈ´Ù. °¢
ÀÎÀÚµéÀº = ±âÈ£·Î Å°(Ç׸ñ¸í)¿Í °ªÀ¸·Î ±¸¼ºµÈ´Ù.
4. ÀÎÀÚÀÇ Àü´Þ°ú ó¸®
FORM ű׳ª GET¹æ½ÄÀÇ Á÷Á¢¿¬°áÀ» ÅëÇØ Àü´ÞµÈ
Á¤º¸´Â ¾à°£ÀÇ ÀýÂ÷¸¦ °ÅÃÄ¾ß Perl¿¡¼ È°¿ëÇÒ ¼ö ÀÖ´Ù. ƯÁ¤
±âÈ£·Î ³ª´¶ ÀÎÀÚµéÀ» ÃßÃâÇØ ³»¾ßÇÏ°í ¼ºº°°ú °°ÀÌ 16Áø¼ö ÇüÅ·Î
Àü´ÞµÈ ÇѱÛÀ̳ª Ư¼ö¹®ÀÚ¸¦ Á¤»óÀûÀÎ ÀÚ·á·Î º¯È¯ÇØ¾ß ÇÑ´Ù. À̸¦
À§ÇØ ¸¹Àº À̵éÀÌ cgi-lib.pl ¶óÀ̺귯¸®³ª CGI.pm ¸ðµâÀ» »ç¿ëÇÏ´Â
°ÍÀÌ º¸ÅëÀÌ´Ù. ÇÏÁö¸¸ ÇÊ¿äÇÑ ¹üÀ§ ÀÌ»óÀÇ ºñ´ëÇÑ ¼Ò½º¸¦
»ç¿ëÇÏ°í ½ÍÁö ¾Ê±â¿¡ ÇÊÀÚ´Â Çѹøµµ »ç¿ëÇغ» ÀûÀÌ ¾ø´Ù. Àû¾îµµ
´ÙÀ½ÀÇ ¼Ò½º Á¤µµ¸é ³Ñ°Ü¹ÞÀº ÀÎÀÚ¸¦ Perl¿¡¼ Á¤»óÀûÀÎ ÀÚ·á·Î
È°¿ëÇÒ ¼ö Àֱ⠶§¹®ÀÌ´Ù.
sub input_form { # »ç¿ëÇÒ
º¯¼ö¸¦ Áö¿ªº¯¼ö·Î ¼±¾ð local ($form, $tmp,
$key, $value); # GET, POST ¹æ½ÄÀ» ÆǺ°ÇÏ¿©
ÀÎÀÚ ½Àµæ if($ENV{¡®REQUEST_METHOD¡¯} eq
¡°GET¡±) { $form = $ENV{¡®QUERY_STRING¡¯};
} if($ENV{¡®REQUEST_METHOD¡¯} eq ¡°POST¡±)
{ read(STDIN, $form, $ENV{¡®CONTENT_LENGTH¡¯});
} # Å°¿Í °ªÀ¸·Î ±¸ºÐÇÏ°í 16Áø¼ö¸¦ ÀϹݹ®ÀÚ·Î
º¯È¯ foreach $tmp (split(/&/, $form))
{ ($key, $value) = split(/=/,
$tmp); $key =~ tr/+/
/; $value =~ tr/+/ /; $key
=~ s/%(..)/pack(¡°C¡±, hex($1))/ge;
$value =~ s/%(..)/pack(¡°C¡±, hex($1))/ge;
$input{$key} = $value; } }
|
¼Ò½º¸¦ °£·«È÷ ¼³¸íÇÏ¸é ´ÙÀ½°ú °°´Ù. 1.
ÀÎÀÚ Àü´Þ¹æ½Ä(GET ¶Ç´Â POST) ÆǺ° 2. Àüü ÀÎÀÚ ½Àµæ
3. ÀüüÀÎÀÚ¿¡¼ & ±âÈ£¸¦ ±âÁØÀ¸·Î °¢ ÀÎÀÚ ÃßÃâ 4. °¢
ÀÎÀÚ¿¡¼ = ±âÈ£¸¦ ±âÁØÀ¸·Î Å°¿Í °ªÀ¸·Î ±¸ºÐ 5. + ±âÈ£·Î
³Ñ¾î¿Â °ø¹é¹®ÀÚ¸¦ Á¤»óÀûÀÎ °ø¹éÀ¸·Î º¯È¯ 6. ÇѱÛÀ̳ª Ư¼ö¹®ÀÚÀÎ
16Áø¼ö ÀڷḦ ÀϹݹ®ÀÚ·Î º¯È¯ 7. %input Á¶Çչ迿¡ Å°¿Í
°ª ÀúÀå
ÀÌÁ¦ &input_form; À» ¸ÕÀú È£ÃâÇÏ°í $input{¡®Ç׸ñ¸í¡¯}
°ú °°ÀÌ ÀڷḦ ÃßÃâÇÏ¿© È°¿ëÇÒ ¼ö ÀÖ´Ù.
¾Õ¼ ¼³¸íÇÑ FORM űװ¡ Æ÷ÇÔµÈ HTML ¹®¼¿¡¼
CGI¸¦ È£ÃâÇϸé Àü´ÞµÈ Á¤º¸¸¦ Ãâ·ÂÇÏ´Â CGI¸¦ ¸¸µé¾î º¸ÀÚ.
´ÙÀ½Àº HTML¹®¼(FORM ÅÂ±× »ç¿ë)¿¡ À¥ºê¶ó¿ìÁ®·Î
Á¤º¸¸¦ ÀÔ·ÂÇϴ ȸé(lynx)ÀÌ´Ù.
´ÙÀ½Àº way.cgi ÆÄÀÏÀÌ´Ù. ¹Ýµå½Ã Perl°æ·Î¸¦
È®ÀÎÇÏ°í ½ÇÇà±ÇÇÑÀ» ºÎ¿©ÇØ¾ß Á¤»óÀûÀÎ °á°ú¸¦ ¾òÀ» ¼ö ÀÖÀ» °ÍÀÌ´Ù.
#!/usr/bin/perl
&input_form; &print_html;
sub input_form { ... } #
¾Õ¼ ¼³¸íÇÑ ¼Ò½º sub print_html { print
¡°Content-type: text/html¡¬n¡¬n¡±; print
¡°<HR>¡¬n¡±; print ¡°<H2>¿ø·¡
Àü´ÞµÇ¾ú´ø ÀÎÀÚ</H2>¡¬n¡±; print ¡°$ENV{¡®QUERY_STRING¡¯}<BR>¡¬n¡±;
print ¡°<HR>¡¬n¡±; print ¡°<H2>º¯È¯ÇÏ¿©
È°¿ëÀÌ °¡´ÉÇØÁø ÀÚ·á</H2>¡¬n¡±; print
¡°¼º¸í : $input{¡®user¡¯}<BR>¡¬n¡±;
print ¡°ÁÖ¼Ò : $input{¡®address¡¯}<BR>¡¬n¡±;
print ¡°¼ºº° : $input{¡®sex¡¯}<BR>¡¬n¡±;
print ¡°<HR>¡¬n¡±; exit; }
|
Á¤»óÀûÀÎ °æ¿ì °á°ú´Â ´ÙÀ½°ú °°´Ù.
___________________________________________
¿ø·¡ Àü´ÞµÇ¾ú´ø ÀÎÀÚ user=way&address=Kwangju&sex=%B3%B2
___________________________________________
º¯È¯ÇÏ¿© È°¿ëÀÌ °¡´ÉÇØÁø ÀÚ·á ¼º¸í : way
ÁÖ¼Ò : Kwangju ¼ºº° : ³² ___________________________________________
|
5. ¸ðµâ°ú ¶óÀ̺귯¸® Socket, DBI, DBD
µîÀÇ Perl ¸ðµâÀº PerlÀÇ È°¿ë¹üÀ§¸¦ È®Àå½ÃÄÑÁÖ´Â ¸Å¿ì À¯¿ëÇÑ
µµ±¸ÀÌ´Ù. Perl ¸ðµâÀ» »ç¿ëÇϱâ À§Çؼ´Â ´ÙÀ½°ú °°ÀÌ ¼±¾ðÇØÁÖ¾î¾ß
ÇÑ´Ù.
use ¸ðµâ¸í;
Socket ¸ðµâÀÇ °æ¿ì ¿ø°ÝÅë½ÅÀ» °¡´ÉÇÏ°Ô ÇØÁÖ´Â
µµ±¸·Î¼, sendmail ÀÌ Áö¿øµÇÁö ¾Ê´Â UNIX°è¿ ¼¹ö³ª WindowsNT/98/95
¿î¿µÃ¼Á¦ÀÇ ¼¹ö¿¡¼ SMTP ¼¹ö¿¡ Á¢¼ÓÇÏ¿© ¸ÞÀÏÀ» Àü¼ÛÇÏ´Â °æ¿ì°¡
ÁÁÀº ¿¹ÀÌ´Ù. °ü½É ÀÖ´Â µ¶ÀÚ´Â Way-BOARD(Ver1.2)¸¦ ´Ù¿î·Îµå
ÇÏ¿© ¾ÐÃàÀ» ÇØÁ¦ÇÑ ÈÄ lib/mail.cgi ÆÄÀÏ ÇÏ´ÜÀÇ Socket Mail ºÎºÐÀ»
Âü°íÇϱ⠹ٶõ´Ù. ¸¹Àº ¸ðµâÀº PerlÀÌ ¼³Ä¡µÇ¸é¼ ÇÔ²² ¼³Ä¡µÇÁö¸¸
±× ¹ÛÀÇ ¸ðµâÀº http://www.perl.com/CPAN/¿¡¼
±¸ÇÒ ¼ö ÀÖ´Ù.
ÀÚÁÖ »ç¿ëµÇ´Â ÇÔ¼ö¸¦ ƯÁ¤ ÆÄÀÏ¿¡ ³Ö¾îµÎ°í
ÇÊ¿ä¿¡ µû¶ó ²¨³» ¾µ ¼ö ÀÖ´Ù.
require(¡°¶óÀ̺귯¸® ÆÄÀÏ¸í¡±);
C³ª PHPÀÇ include¹®°ú °ÅÀÇ µ¿ÀÏÇÑ ±â´ÉÀ» ÇÑ´Ù.
¾ÕÀÇ ÀÎÀÚº¯È¯ ¼Ò½ºµµ ¶óÀ̺귯¸® ÆÄÀÏ¿¡ ³Ö¾îµÎ°í °¢Á¾ÀÇ
CGI¿¡¼ ÇÔ²² °øÀ¯ÇÒ °æ¿ì ÀÛ¾÷È¿À²Àº ±Ø´ëÈ µÈ´Ù. ¶óÀ̺귯¸®
ÆÄÀÏ¿¡ ³Ö¾îµÑ °æ¿ì ½ÇÇà±ÇÇÑÀ» ºÎ¿©Çϰųª Perl À§Ä¡¸¦ Àû¾îÁÙ
ÇÊ¿ä°¡ ¾øÁö¸¸ À¯ÀÇÇÒ Á¡Àº ÆÄÀÏÀÇ Á¦ÀÏ ÇÏ´Ü¿¡ ´ÙÀ½°ú °°ÀÌ return
1; À̶ó´Â ÁÙÀ» Ãß°¡ÇØÁà¾ß ¿¡·¯°¡ ¹ß»ýÇÏÁö ¾Ê´Â´Ù´Â °ÍÀÌ´Ù.
# Perl Library File
sub input_form { ... }
sub html_header { ... }
return 1;
|
6. ȯ°æº¯¼ö ¾Õ¼ ¼³¸íÇÑ ÀÎÀÚó¸® ¼Ò½º¿Í
bash, C¸¦ ÀÌ¿ëÇÑ CGI¿¡¼ °¢°¢ ȯ°æº¯¼ö¸¦ Àоîµé¿© È°¿ëÇÏ´Â ¹ýÀ»
º¼ ¼ö ÀÖ¾ú´Ù. Perl¿¡¼´Â ȯ°æº¯¼ö°¡ Á¶ÇÕ¹è¿ÀÎ %ENV¿¡
ÀúÀåµÇ¾îÀÖ´Ù. ´ÙÀ½Àº Perl, PHP, C, bash¿¡¼ Á¢¼ÓÀÚÀÇ IPÁ¤º¸¸¦
¾ò¾î³»´Â ¹æ¹ýÀÌ´Ù.
±¸ ºÐ |
ȯ°æº¯¼ö ½Àµæ¹æ¹ý |
perl |
$ENV{¡®REMOTE_ADDR¡¯} |
PHP,
C |
getenv(¡°REMOTE_ADDR¡±) |
bash |
$REMOTE_ADDR |
´ÙÀ½ÀÇ ¼Ò½º¸¦ ½ÇÇàÇغ¸¸é Perl¿¡¼ »ç¿ëµÇ´Â
°¢Á¾ÀÇ È¯°æº¯¼ö¸í°ú °ªÀ» ±¸ÇÒ ¼ö ÀÖ´Ù.
#!/usr/bin/perl print ¡°Content-type:
text/html¡¬n¡¬n¡±; foreach $env_val (keys %ENV) {
print ¡°* $env_val : $ENV{$env_val}<BR>¡¬n¡±;
}
´ÙÀ½Àº ÇÊÀÚÀÇ ¼¹ö¿¡¼ ¼Ò½º¸¦ ½ÇÇàÇÑ °á°ú´Ù.
´ëÃæ ÈȾ¸é ¾î¶² ±â´ÉÀ» ÇÏ´Â °ÍµéÀÎÁö ¾Ë ¼ö ÀÖÀ» °ÍÀÌ´Ù.
°æ¿ì¿¡ µû¶ó ȯ°æº¯¼ö Á¾·ù°¡ ´Ã¾î³ª°Å³ª ÁÙ¾îµé ¼ö ÀÖ´Ù.
* SERVER_SOFTWARE : Apache/1.3.9
(Unix) * GATEWAY_INTERFACE : CGI/1.1
* DOCUMENT_ROOT : /home/way/public_html
* REMOTE_ADDR : 210.123.123.123 * SERVER_PROTOCOL
: HTTP/1.1 * SERVER_SIGNATURE : Apache/1.3.9
Server at way.co.kr Port
80 * REQUEST_METHOD : GET * QUERY_STRING
: * HTTP_USER_AGENT : Mozilla/4.0 (compatible;
MSIE 5.0); Windows 98
* PATH : /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
* HTTP_ACCEPT : */* * HTTP_CONNECTION :
Keep-Alive * REMOTE_PORT : 1129 * SERVER_ADDR
: 210.123.254.100 * HTTP_ACCEPT_LANGUAGE
: ko * SCRIPT_NAME : /test/env.cgi *
HTTP_ACCEPT_ENCODING : gzip, deflate * SCRIPT_FILENAME
: /home/way/ public_html/test/env.cgi
* SERVER_NAME : way.co.kr * REQUEST_URI
: /test/env.cgi * SERVER_PORT : 80 *
HTTP_HOST : way.co.kr * SERVER_ADMIN : webmaster@way.co.kr
|
¸¸¾à ºê¶ó¿ìÁ®¿¡ ÀԷµÈ(ƯÈ÷ VirtualHost¿¡¼)
µµ¸ÞÀÎÀ» ¾ò±â À§Çؼ´Â $ENV{¡®HTTP_HOST¡¯}¶ó´Â º¯¼ö¸¦ ÀоîµéÀ̸é
µÈ´Ù.
¿¹½Ã¿¡ ³ª¿ÍÀÖÁö ¾ÊÁö¸¸ HTTP_COOKIE(ÄíÅ°Á¤º¸),
HTTP_REFERER(CGI ½ÇÇà URL), REMOTE_USER (ÀÎÁõ »ç¿ëÀÚ
ID) µîÀÇ È¯°æº¯¼öµµ ¸Å¿ì À¯¿ëÇÏ´Ù.
´ÙÀ½ ¼Ò½º´Â ȯ°æº¯¼ö¸¦ ÀÌ¿ëÇØ ½º½º·ÎÀÇ URLÀ»
Ç¥½ÃÇÏ´Â °£´ÜÇÑ ¼Ò½ºÀÌ´Ù.
#!/usr/bin/perl print ¡°Content-type:
text/html¡¬n¡¬n¡±; print ¡°http://$ENV{¡®HTTP_HOST¡¯}$ENV{¡®SCRIPT_NAME¡¯}¡¬n¡±;
7. ½Ã°£Á¤º¸
½Ã°£Á¤º¸¸¦ ¾ò±â À§Çؼ´Â time() ÇÔ¼ö¸¦ ÀÌ¿ëÇÑ´Ù.
¾òÀ» ¼ö ÀÖ´Â °ªÀº 1970³â 1¿ù 1ÀÏÀ» ±âÁØÀ¸·Î ÇÑ ÃÊ ´ÜÀ§ °ªÀÌ´Ù.
º¸ÅëÀÇ °æ¿ì localtime() ÇÔ¼ö¸¦ »ç¿ëÇÏ¿© ´ÜÀ§º° °ªµéÀ» ¸®½ºÆ®·Î
ÃßÃâÇس½´Ù. ´ÙÀ½Àº ½Ã°£Á¤º¸¸¦ º¸±â ÁÁ°Ô ÃßÃâÇس»´Â ¼Ò½ºÀÌ´Ù.
ÁÖÀÇÇÒ Á¡Àº ¿äÀÏ($weekday)À̳ª ¿ù($month)Àº 0ºÎÅÍ ½ÃÀ۵ȴٴÂ
°ÍÀÌ´Ù. ¶ÇÇÑ Ç×°£¿¡ ½Ã²ô·¯¿î Y2K¹®Á¦¿Í °ü·ÃÇØ °í·ÁÇÒ ºÎºÐÀº
$year°¡ ½ÇÁ¦ ¿¬µµ¿¡¼ 1900À» »« ¼öÄ¡·Î ±¸¼ºµÈ´Ù´Â °ÍÀÌ´Ù. ±×·¯´Ï±î
$year °ª¿¡ 1999³âÀº 99, 2000³âÀº 100ÀÌ ÀúÀåµÈ´Ù. ¿Ü±¹¼¹ö¸¦
»ç¿ëÇÏ´Â °æ¿ì¿Í °°ÀÌ ÇÊ¿ä¿¡ µû¶ó Áö¿ª½Ã°£´ë¿¡ µû¸¥ Á¶Á¤ÀÌ ÇÊ¿äÇϸé
time()¿¡ ÃÊ´ÜÀ§·Î Á¶Á¤À» °¡ÇÒ ¼ö ÀÖ´Ù. localtime(time()+(12*60*60))¿Í
°°ÀÌ »ç¿ëÇÒ °æ¿ì ¼¹ö½Ã°£¿¡¼ 12½Ã°£ ºü¸¥ ½Ã°£Á¤º¸°¡ ÃßÃâµÈ´Ù.
#!/usr/bin/perl print
¡°Content-type: text/html¡¬n¡¬n¡±; ($second,
$minute, $hour, $date, $month, $year, $weekday,
$yearday, $Isdst) = localtime(time());
$month ++; $year += 1900;
@dayname = (¡°ÀÏ¡±,¡±¿ù¡±,¡±È¡±,¡±¼ö¡±,¡±¸ñ¡±,¡±±Ý¡±,¡±Å䡱);
$weekday = $dayname[$weekday];
print ¡°ÀÏÀÚ : $year³â $month¿ù
$dateÀÏ $weekday¿äÀÏ<BR>¡¬n¡±; print
¡°½Ã°£ : $hour½Ã $minuteºÐ $secondÃÊ<BR>¡¬n¡±;
|
PHP¿¡¼ »ç¿ëÇÏ´Â date ÇÔ¼öó·³ Ç¥½ÃÇü½ÄÀÇ
ÀÚÀ¯·Î¿î ÁöÁ¤ÀÌ °¡´ÉÇÑ Perl ÇÔ¼ö¸¦ ¸¸µé¾îº¸´Â °Íµµ ÁÁÀº °øºÎ°¡
µÉ °ÍÀÌ´Ù.
8. ¿¬¼ÓÃâ·Â
Á¤º¸¸¦ ¿¬´Þ¾Æ Ãâ·ÂÇϱâ À§Çؼ´Â ¼ö¸¹Àº print¹®À»
»ç¿ëÇϱ⵵ ÇÏ°í print¹® Çϳª·Î ´Ù¼öÀÇ ¶óÀÎÀ» Ãâ·ÂÇϱ⵵ ÇÏÁö¸¸
¡° ±âÈ£ µîÀ» Ç¥½ÃÇÒ ¶§ ¡¬¡±¿Í °°ÀÌ Ç¥½ÃÇØ Áà¾ßÇϹǷΠ¸Å¿ì ºÒÆíÇÏ´Ù.
ÇÏÁö¸¸ ´ÙÀ½°ú °°ÀÌ print¹®À» print <<¡±±¸ºÐ¹®ÀÚ¡±;¿Í °°ÀÌ
»ç¿ëÇÏ°Ô µÇ¸é ¡°±¸ºÐ¹®ÀÚ¡±°¡ ³ªÅ¸³¯ ¶§±îÁö °è¼Ó Ãâ·ÂÇÒ ¼ö ÀÖ´Ù.
ÇØ´ç¹üÀ§ ³»¿¡¼´Â HTML ¹®¼Ã³·³ ÄÚµùÇصµ Å©°Ô ¹®Á¦°¡ ¾øÀ¸¸ç
Perl¿¡¼ »ç¿ëÁßÀÎ º¯¼ö¸¦ Á÷Á¢ »ç¿ëÇÒ ¼öµµ ÀÖ°í ¡° µîÀÇ Å«µû¿ÈÇ¥¸¦
°¡°ø ¾øÀÌ »ç¿ëÇÒ ¼öµµ ÀÖ´Ù. À¯ÀÇÇÒ Á¡Àº ³¡À» ¾Ë¸®´Â
¡°±¸ºÐ¹®ÀÚ¡±´Â ù ¹ø° ÀÚ¸´¼öºÎÅÍ ÀԷµǾî¾ß ÇÑ´Ù´Â °Í°ú ¸ÞÀÏÁÖ¼Ò¿¡
¾²ÀÌ´Â @±âÈ£´Â ¡¬@¿Í °°ÀÌ »ç¿ëÇØ¾ß ÇÑ´Ù´Â °ÍÀÌ´Ù. ¸¸¾à print
<<¡¯EOF¡¯;¿Í °°ÀÌ ÀÛÀºµû¿ÈÇ¥¸¦ ÀÌ¿ëÇØ ±¸ºÐ¹®ÀÚ¸¦ °¨½ÎÁÖ¸é
@ ±âÈ£¸¦ ±×´ë·Î »ç¿ëÇÒ ¼ö ÀÖÁö¸¸ º¯¼öÂüÁ¶°¡ ºÒ°¡´ÉÇÏ´Ù.
#!/usr/bin/perl $user
= ¡°way¡±; $address = ¡°Kwangju¡±; print
¡°Content-type: text/html¡¬n¡¬n¡±; print
<<EOF; <HTML> <HEAD>
<TITLE>¿¬¼ÓÃâ·Â ½ÃÇè</TITLE>
</HEAD> <BODY> ¼º¸í : ¡°$user¡±<BR>
ÁÖ¼Ò : ¡°$address¡±<BR> </BODY>
</HTML> EOF print ¡°Ãâ·Â¿Ï·á¡±;
|
9. ¸ÞÀÏÀü¼Û
°Ô½ÃÆÇ¿¡ »õ·Î¿î ±ÛÀÌ µî·ÏµÇ°Å³ª ¹æ¹®ÀÚ°¡ ¹æ¸í·Ï¿¡
±ÛÀ» ³²±æ °æ¿ì, ƯÁ¤¾ç½ÄÀÇ ½Åû¼¸¦ ÀԷ¹޴ °æ¿ì ¸ÞÀÏ·Î Á¤º¸°¡
Àü´ÞµÇµµ·Ï ¸ÞÀÏÀ» Àü¼ÛÇÏ´Â ¹æ¹ýÀ» ¾Ë¾Æº¸ÀÚ. ¾Õ¼ ¼³¸íÇßµíÀÌ
ÆÄÀÏÀ» Á¶ÀÛÇϱâ À§ÇØ ´ÙÀ½°ú °°ÀÌ »ç¿ëÇß¾ú´Ù.
open(ÆÄÀÏÇÚµé, ¡°[Á¶ÀÛ¹æ¹ý] ÆÄÀÏ¸í¡±);
¿©±â¿¡ Á¶ÀÛ¹æ¹ýÀ» ¡°|¡± ±âÈ£·Î ÇÏ°í ÆÄÀϸí
´ë½Å ¸í·É¾î(sendmail)³ª ÀåÄ¡(ÇÁ¸°ÅÍ µî)¸¦ Àû¾îÁÖ¸é Perl¿¡¼
Á¤º¸¸¦ Àü´ÞÇÒ ¼ö ÀÖ´Ù.
´ÙÀ½Àº ¸ÞÀÏÀ» Àü¼ÛÇÏ´Â ¼Ò½ºÀÌ´Ù. @±âÈ£´Â
¡¬@¿Í °°ÀÌ Ç¥½ÃÇϰųª Å«µû¿ÈÇ¥ ´ë½Å ÀÛÀºµû¿ÈÇ¥·Î °¨½ÎÁÖ¸é ¿¡·¯°¡
¹ß»ýÇÏÁö ¾Ê´Â´Ù.
#!/usr/bin/perl open(MAIL,¡±|/usr/sbin/sendmail
-t¡±); print MAIL ¡°To: way¡¬@abc.co.kr¡¬n¡±;
print MAIL ¡°Subject: Á¦¸ñÀÔ´Ï´Ù.¡¬n¡¬n¡±;;
print MAIL ¡°¸Þ¼¼Áö º»¹®ÀÔ´Ï´Ù.¡±;
close (MAIL);
print ¡°Content-type: text/html¡¬n¡¬n¡±;
print ¡°Àü¼Û¿Ï·á¡±;
|
¹ß¼ÛÀÚ(From)¸¦ ÁöÁ¤ÇÏ·Á¸é To ¾Æ·§ÁÙ¿¡ ´ÙÀ½°ú
°°ÀÌ »ðÀÔÇÑ´Ù.
print MAIL ¡°From: ÀÓ´ëÈ£ <lawwal¡¬@way.co.kr>¡¬n¡±;
HTMLÇü½ÄÀ¸·Î ¸ÞÀÏÀ» º¸³»·Á¸é Subject ºÎºÐÀ»
´ÙÀ½°ú °°ÀÌ ¹Ù²ãÁÖ¸é µÈ´Ù.
print MAIL ¡°Subject: Á¦¸ñÀÔ´Ï´Ù.¡¬n¡±;
print MAIL ¡°Content-type: text/html¡¬n¡¬n¡±;
10. ÄíÅ°
°Ô½ÃÆÇ¿¡ ±ÛÀ» ¾²¸é ´ÙÀ½¿¡ ±ÛÀ» ¾µ ¶§ ÀÚµ¿À¸·Î
À̸§°ú ¸ÞÀÏÁÖ¼Ò¸¦ Àû¾îÁְųª Á¢¼ÓÀÚ ÀÎÁõ¿¡ ÁÖ·Î ÄíÅ°°¡ »ç¿ëµÈ´Ù.
Perl¿¡¼µµ ÄíÅ°¸¦ Á÷Á¢ Çڵ鸵ÇÒ ¼ö ÀÖ´Ù. ´ÙÀ½ ¼Ò½º´Â
Perl¿¡¼ »ç¿ëÀÚÀÇ ºê¶ó¿ìÁ®¿¡ ÄíÅ°°ªÀ» ÀúÀåÇÏ´Â ÇÔ¼öÀÌ´Ù.
sub cookie_set { local($name,
$value, $exp) = @_; $exp
= scalar localtime(time()+(24*60*60)*$exp);
print ¡°Content-type: text/html¡¬n¡±;
print ¡°Set-Cookie: $name=;¡¬n¡±;
print ¡°Set-Cookie: $name=$value;expires=$exp;¡¬n¡±;
}
|
»ç¿ëÀÚÀÇ ºê¶ó¿ìÁ®¿¡ ÀúÀåµÈ ÄíÅ°°ªÀº ȯ°æº¯¼öÀÎ
$ENV{¡®HTTP_COOKIE¡¯}¿¡ ÀúÀåµÈ´Ù. ´ÙÀ½ ¼Ò½º´Â ÄíÅ°¸¦ Àоîµé¿©
%cookie¶ó´Â Á¶Çչ迿¡ ³Ö¾î È°¿ëÀÌ °¡´ÉÅä·Ï ÇÏ´Â ÇÔ¼öÀÌ´Ù. ÀÚ¼¼È÷
º¸¸é ¾Õ¼ ¼³¸íÇÑ input_form() ÇÔ¼ö¿Í À¯»çÇÏ´Ù´Â °ÍÀ» ¾Ë ¼ö ÀÖÀ»
°ÍÀÌ´Ù.
sub cookie_read { @strings
= split(/; /,$ENV{¡®HTTP_COOKIE¡¯}); foreach
$strs (@strings) { local
($name, $value) = split(/=/,$strs); $value
=~ tr/+/ /; $value
=~ s/%(..)/pack(¡°C¡±, hex($1))/ge; $cookie{$name}
= $value; } }
|
µÎ °³ÀÇ ÇÔ¼ö¸¦ ÀÌ¿ëÇؼ ÄíÅ°¸¦ ÀúÀåÇÏ°í Àоµµ·Ï
ÇÏ°Ú´Ù.
#!/usr/bin/perl &cookie_set(¡°name¡±,
¡°way¡±, 7); # Å°, °ª, ¸¸·á±âÀÏ print
¡°Content-type: text/html¡¬n¡¬n¡±; print
¡°ÄíÅ°¸¦ ÀúÀåÇÏ¿´½À´Ï´Ù.¡±;
#!/usr/bin/perl &cookie_read;
print ¡°Content-type: text/html¡¬n¡¬n¡±; print
¡°Àоî¿Â nameÀÇ °ªÀº $cookie{¡®name¡¯}ÀÔ´Ï´Ù.¡±;
11. ½ÇÇà±ÇÇÑ°ú º¸¾È
Perl»Ó¸¸ ¾Æ´Ï¶ó ¸ðµç CGI°¡ ÆÄÀÏÀ» Á¶ÀÛÇÒ °æ¿ì
À¥¼¹ö°¡ nobody µîÀ¸·Î ½ÇÇàµÇ¹Ç·Î nobody µîÀÇ ¼ÒÀ¯·Î ÆÄÀÏÀÌ
»ý¼ºµÇ°í Á¶À۵ǾîÁø´Ù. ÀÌ´Â ½ÇÁ¦ ÆÄÀϼÒÀ¯ÀÚ°¡ Á¢±ÙÇϱ⵵
°ï¶õÇÒ»Ó´õ·¯ µ¿ÀÏÇÑ ¼¹ö¸¦ »ç¿ëÇÏ´Â ±¸¼º¿øÀº °øÅëÀÇ ±ÇÇÑÀ» °¡Áö´Â
Çü±¹ÀÌ µÇ¹Ç·Î º¸¾È¹®Á¦°¡ ¾ß±âµÉ ¼ö ÀÖ´Ù.(¿ªÈ¿°ú¸¦ ¿ì·ÁÇØ ¼Ò½º¸¦
¼Ò°³ÇÏÁö´Â ¾Ê°Ú´Ù.) ÇØ°á¹æ¹ýÀ¸·Î´Â ´ÙÀ½ ¼¼ °¡Áö Á¤µµ·Î
¿ä¾àÇÒ ¼ö ÀÖ´Ù. ¼¼ °¡Áö ¹æ¹ý ¸ðµÎ ½ÇÁ¦ »ç¿ëÀÚÀÇ ±ÇÇÑÀ¸·Î ½ÇÇàµÇ¹Ç·Î
Áß¿äÇÑ Á¤º¸¸¦ Áö´Ñ ÀÚ·áÆÄÀÏÀ̳ª µð·ºÅ丮´Â chmod go-rwx ¿Í °°ÀÌ
Çصξ ¹«¹æÇÏ´Ù.
°¡. ¾ÆÆÄÄ¡ suexec Àû¿ë
¾ÆÆÄÄ¡ ¼³Ä¡½Ã ´ÙÀ½°ú °°ÀÌ suexec¸¦ Àû¿ëÇÏ°í
ÄÄÆÄÀÏÇÑ´Ù. °¡»ó µµ¸ÞÀÎÀÇ °æ¿ì ¾ÆÆÄÄ¡ ¼³Á¤½Ã <VirtualHost>
ºÎºÐ¿¡ User¿Í GroupÀ» ½ÇÁ¦ °èÁ¤ »ç¿ëÀÚ·Î ÁöÁ¤ÇØÁÖ¸é µÈ´Ù.
# ./configure --prefix=/usr/local/apache
--enable-suexec --suexec-caller=nobody
--suexec-docroot=/home --suexec-logfile=/usr/local/apache/logs/suexec_log
--suexec-userdir=public_html --suexec-uidmin=500
--suexec-gidmin=500 --suexec-safepath=/usr/local/bin:/usr/bin:/bin
# make # make install
* ¾ÆÆÄÄ¡ VirtualHost ¼³Á¤ <VirtualHost
210.123.254.100> User way
Group user
ServerAdmin webmaster@way.co.kr
DocumentRoot /home/way/public_html
ScriptAlias /cgi-bin/ /home/way/public_html/cgi-bin/
ServerName www.way.co.kr
ServerAlias way.co.kr www.way.co.kr
TransferLog /home/way/www_log/today.log
ErrorLog /home/way/www_log/error.log
</VirtualHost>
³ª. cgiwrap Àû¿ë
ftp://ftp.umr.edu/pub/cgi/cgiwrap/cgiwrap-Version.tar.gz
# tar xvfz cgiwrap-Version.tar.gz # ./configure --help(¿É¼Ç
È®ÀÎ) # ./configure --with-httpd-user=nobody # make
# make install # cp cgiwrap /usr/local/apache/cgi-bin
# cd /usr/local/apache/cgi-bin # chown root cgiwrap
# chmod 4755 cgiwrap # ln -s cgiwrap cgiwrapd # ln -s
cgiwrap nph-cgiwrap # ln -s cgiwrap nph-cgiwrapd
ÇÊÀÚÀÇ °æ¿ì À§¿Í °°ÀÌ ¼³Ä¡ÇÑ ÈÄ cgi-binÀº
ÀÌ¹Ì /home/way/public_html/cgi-bin À¸·Î ScriptAlias µÇ¾îÀÖÀ¸¹Ç·Î
/usr/local/apache/cgi-bin µð·ºÅ丮¸¦ cgi-sys·Î ScriptAliasÇؼ
´ÙÀ½°ú °°ÀÌ ½ÇÇàÇÏ¿´´Ù.
* ½Ã½ºÅÛ °æ·Î : /home/way/public_html/cgi-bin/file.cgi
* ½ÇÇàURL : http://way.co.kr/cgi-sys/cgiwrap/way/file.cgi
(¿ìÃøÀÇ way´Â »ç¿ëÀÚ ID¸¦ ¶æÇÑ´Ù.)
´Ù. Set User ID
Perl °æ·Î¸¦ Àû´Â ¼Ò½º ù ¹ø°ÁÙ¿¡ -U ¿É¼ÇÀ»
ÇÔ²² Àû¾îÁØ ÈÄ chmod +s way.cgi ¿Í °°Àº ¹æ¹ýÀ¸·Î ÆÄÀÏ¿¡ ½ÇÇà±ÇÇÑÀ»
ºÎ¿©ÇÑ´Ù. #!/usr/bin/perl -U
12. ¹æ¸í·Ï ¸¸µé±â
ÀÌÁ¦ CGI ¿¹Á¦·Î °£´ÜÇÑ ¹æ¸í·ÏÀ» Çѹø ¸¸µé¾î
º¸ÀÚ. Áö¸é°ü°è»ó ¸ÞÀÏÀ̳ª ÄíÅ°, ÆäÀÌÁö ³ª´® µîÀº ¹èÁ¦Çϵµ·Ï
ÇÏ°Ú´Ù. Âü°í·Î ¹æ¸í·ÏÆÄÀÏ(guestbook.dat)Àº nobody µîÀÇ
À¥»ç¿ëÀÚ°¡ ¾µ ¼ö ÀÖ´Â ±ÇÇÑÀÌ ºÎ¿©µÇ¾î¾ß ÇÑ´Ù.
#!/usr/bin/perl # ¸ÕÀú ÀÎÀÚÁ¤º¸¸¦ ½ÀµæÇÑ´Ù.
&input_form;
# Àü´ÞµÈ °ªÁß ¡®ÀÛ¾÷¡¯ Ç׸ñÀÇ °ªÀÌ ¾øÀ» °æ¿ì
# ÀÌ¹Ì µî·ÏµÈ ±ÛÀ» Ãâ·Â(¹æ¸í·Ï º¸±â)ÇÏ°í Á¾·áÇÑ´Ù. if(!$input{¡®ÀÛ¾÷¡¯})
{ &print_guestbook; exit; }
# Àü´ÞµÈ °ªÁß ¡®ÀÛ¾÷¡¯ Ç׸ñÀÇ °ªÀÌ ¡®¾²±â¡¯ÀÏ
°æ¿ì # ¾²±âȸéÀ» Ãâ·ÂÇÑ´Ù. elsif($input{¡®ÀÛ¾÷¡¯} eq
¡°¾²±â¡±) { &guestbook_write;
exit; }
# Àü´ÞµÈ °ªÁß ¡®ÀÛ¾÷¡¯ Ç׸ñÀÇ °ªÀÌ ¡®Ã³¸®¡¯ÀÏ
°æ¿ì # ±ÛÀ» µî·ÏÇÏ°í µî·ÏµÈ ±ÛÀ» Ãâ·ÂÇÑ´Ù. elsif($input{¡®ÀÛ¾÷¡¯}
eq ¡°Ã³¸®¡±) { &guestbook_add;
&print_guestbook; exit; }
# ¾î´À°Íµµ ¾Æ´Ò °æ¿ì ±×³É Á¾·áÇÑ´Ù. else
{ exit; }
sub input_form {
# ¾Õ¼ ¼³¸íÇß´ø ÀÎÀÚó¸®ÇÔ¼öÀÌ´Ù. local
($form, $tmp, $key, $value); if($ENV{¡®REQUEST_METHOD¡¯}
eq ¡°GET¡±) { $form = $ENV{¡®QUERY_STRING¡¯};
} if($ENV{¡®REQUEST_METHOD¡¯} eq ¡°POST¡±) { read(STDIN,
$form, $ENV{¡®CONTENT_LENGTH¡¯}); } foreach $tmp (split(/&/,
$form)) { ($key, $value) = split(/=/, $tmp);
$key =~ tr/+/ /; $value
=~ tr/+/ /; $key =~ s/%(..)/pack(¡°C¡±,
hex($1))/ge; $value =~ s/%(..)/pack(¡°C¡±, hex($1))/ge;
$input{$key} = $value; } }
sub print_guestbook {
# HTML ¸Ó¸´¸» Ãâ·Â &html_start(¡°¹æ¸í·Ï
º¸±â¡±);
print ¡°<TABLE BORDER=1 WIDTH=500>¡¬n¡±;
# ¹æ¸í·Ï ÆÄÀÏ·Î »ç¿ëÇÒ ÆÄÀϸíÀ» ÁöÁ¤ÇÑ´Ù.
local $file = ¡°guestbook.dat¡±;
# ÆÄÀÏÀÌ Á¸ÀçÇÏÁö ¾Ê°Å³ª Å©±â°¡ 0byte¸é ÀÛ¼ºµÈ
# ÀÚ·á°¡ ¾ø´Ù°í Ãâ·ÂÇÏ°í Á¾·áÇÑ´Ù. if(!-e $file || -s $file
eq 0) { print ¡°<TR><TD>ÀÛ¼ºµÈ ±ÛÀÌ
¾ø½À´Ï´Ù.</TD></TR>¡¬n¡±; &guestbook_write_link;
print ¡°</TABLE>¡¬n¡±; &html_end;
exit; }
local $print_msg;
# ¹æ¸í·Ï ÆÄÀÏÀ» ¿¾î Ãâ·ÂÇÑ´Ù. # ÆÄÀÏÀ»
¿ ¼ö ¾øÀ» °æ¿ì Á¾·áÇÑ´Ù. open(FILE, ¡°$file¡±) || die ¡°ÆÄÀÏÀ»
¿ ¼ö ¾ø½À´Ï´Ù.($file)¡±; while(<FILE>)
{ # ÇÑÁÙÀ» Àоî¿Í¼ ³¡¿¡ÀÖ´Â °³Ç๮ÀÚ(¡¬n)¸¦
Á¦°ÅÇÑ´Ù. chomp $_; #
Àоî¿Â ÁÙÀÇ ÀڷḦ | ±âÈ£¸¦ ±âÁØÀ¸·Î ÃßÃâÇÏ¿© #
@guest ¶ó´Â ¹è¿¿¡ Áý¾î ³Ö´Â´Ù. local
@guest = split(/¡¬|/, $_); # Ç׸ñº°·Î
ÃßÃâÇÑ´Ù. local $name = $guest[0];
local $mail = $guest[1]; local
$home = $guest[2]; local $date =
$guest[3]; local $usip = $guest[4];
local $mesg = $guest[5]; #
ÀڷḦ °¡°ø(¸µÅ© »ðÀÔ ¹× ½Ã°£ Ãâ·Â)ÇÑ´Ù. if($mail)
{ $mail = ¡°<A HREF=mailto:$mail>$mail</A>¡±;
} if($home) { $home = ¡°<A
HREF=$home>$home</A>¡±; } $date
= &time_conv($date); # HTML
Çü½ÄÀ¸·Î ÀڷḦ º¯¼ö¿¡ ÀúÀåÇÑ´Ù. $print_msg
.= ¡°<TR>¡¬n¡±;
$print_msg .= ¡°<TD><FONT
SIZE=2>¡¬n¡±; $print_msg .= ¡°¢Â
¼º¸í : $name($mail)<BR>¡¬n¡±; $print_msg
.= ¡°¢Â ȨÆäÀÌÁö : $home<BR>¡¬n¡±; $print_msg
.= ¡°¢Â ÀÏÀÚ : $date (IP:$usip)<BR>¡¬n¡±; $print_msg
.= ¡°¢Â $mesg¡±; $print_msg .= ¡°</FONT></TD>¡¬n¡±;
$print_msg .= ¡°</TR>¡¬n¡±;
} # -- while ·çÇÁ Á¾´ÜÁ¡ close(FILE);
# ¸ðµç ±ÛÀ» Ãâ·ÂÇÑ´Ù. print $print_msg;
# ±Û¾²±â ºÎºÐÀÇ ¸µÅ©¸¦ Ãâ·ÂÇÑ´Ù. &guestbook_write_link;
print ¡°</TABLE>¡¬n¡±; # HTMLÀ»
¸¶¹«¸®ÇÑ´Ù. &html_end; }
sub guestbook_write {
&html_start(¡°¹æ¸í·Ï ¾²±â¡±);
# µî·Ï¾ç½ÄÀ» Ãâ·ÂÇÑ´Ù. # ÁÖÀÇÇÒ »çÇ×Àº
FORM ACTION ºÎºÐ°ú # ÇÏ´ÜÀÇ ¡®ÀÛ¾÷¡¯ ºÎºÐÀÌ´Ù. print
<<¡±EOF¡±; <FORM ACTION=$ENV{¡®SCRIPT_NAME¡¯}
METHOD=POST>
<TABLE BORDER=1 WIDTH=500> <TR> <TD>¼º¸í</TD>
<TD><INPUT TYPE=TEXT NAME=name SIZE=20></TD>
</TR> <TR> <TD>E-mail</TD>
<TD><INPUT TYPE=TEXT NAME=mail SIZE=35></TD>
</TR> <TR> <TD>ȨÆäÀÌÁö</TD>
<TD><INPUT TYPE=TEXT NAME=home SIZE=35 VALUE=¡±http://¡±></TD>
</TR> <TR> <TD>³²±â½Ç ³»¿ë</TD>
<TD><TEXTAREA NAME=mesg ROWS=5
COLS=35></TEXTAREA> </TR> <TR>
<TD COLSPAN=2> <INPUT TYPE=SUBMIT VALUE=¡±±ÛÀ»
µî·ÏÇÕ´Ï´Ù.¡±> </TD> <INPUT TYPE=HIDDEN
NAME=¡±ÀÛ¾÷¡± VALUE=¡±Ã³¸®¡±> </FORM> </TR>
</TABLE> EOF &html_end;
}
sub guestbook_add {
# Àü´Þ¹ÞÀº °¢ Ç׸ñº° Á¤º¸¸¦ ÀúÀåÇϱâ À§ÇØ
ÁغñÇÑ´Ù. # µî·ÏµÈ ½Ã°£°ú ±Û¾´ÀÌÀÇ IPÁÖ¼Òµµ ½ÀµæÇÑ´Ù.
local $name = $input{¡®name¡¯}; local $mail = $input{¡®mail¡¯};
local $home = $input{¡®home¡¯}; local $date = time();
local $usip = $ENV{¡®REMOTE_ADDR¡¯}; local $mesg = $input{¡®mesg¡¯};
# $mesg °ª¿¡¼ °³Ç๮ÀÚ¸¦ °ø¹é¹®ÀÚ·Î º¯È¯ÇÑ´Ù.
$mesg =~ s/¡¬cM//g; $mesg =~ s/¡¬r¡¬n/¡¬n/g; $mesg =~
s/¡¬n/ /g;
# °¡±ÞÀû ÀÚ·á ±¸ºÐÀÚ·Î »ç¿ëÇÒ | ±âÈ£°¡ ÀԷµÈ
# ÀڷḦ ´Ù¸¥ ±âÈ£·Î º¯È¯ÇÑ´Ù. $mesg =~ s/|/¡¬:/g; local
$file = ¡°guestbook.dat¡±; local $original;
# ÃÖ±Ù¿¡ µî·ÏµÈ ±ÛÀ» ¸ÕÀú Ãâ·ÂÇϱâÀ§ÇØ
# Á¾Àü¿¡ µî·ÏµÈ ±ÛÀÌ ÀÖÀ» °æ¿ì Àоî¿Â´Ù. if(-e $file) {
open(FILE, ¡°$file¡±) || die ¡°ÆÄÀÏÀ» ¿¼ö ¾ø½À´Ï´Ù.($file)¡±;
while(<FILE>) { $original .= $_; } close(FILE);
}
# »õ·Î¿î ±ÛÀ» ÀúÀåÇÏ°í Á¾ÀüÀÇ ±Ûµµ µÚÀ̾î
ÀúÀåÇÑ´Ù. open(NEW, ¡°>$file¡±) || die ¡°ÆÄÀÏÀ»
¿¼ö ¾ø½À´Ï´Ù.($file)¡±; print NEW ¡°$name|$mail|$home|$date|$usip|$mesg|¡¬n¡±;
print NEW $original; close(NEW); }
sub html_start {
# TITLE·Î ÀμâÇÒ Á¤º¸¸¦ ³Ñ°Ü¹Þ´Â´Ù. local($title)
= @_;
# HTML ¹®¼Çü½ÄÀ¸·Î Ãâ·ÂÇÏ°Ú´Ù°í ¼±¾ðÇÑ´Ù.
print ¡°Content-type: text/html¡¬n¡¬n¡±;
# HTML ÅÂ±× ¿¬¼Ó Ãâ·Â(TITLE Æ÷ÇÔ) print
<<¡±EOF¡±; <HTML> <HEAD> <TITLE>$title</TITLE>
</HEAD> <BODY> <HR SIZE=1> <H2>$title</H2>
<HR SIZE=1> EOF }
sub html_end {
# HTML Ãâ·ÂÀ» ¸¶¹«¸®ÇÑ´Ù. print <<¡±EOF¡±;
<HR SIZE=1> </BODY> </HTML> EOF
}
sub guestbook_write_link {
# ¡®¾²±â¡¯ ¸µÅ©¸¦ Ãâ·ÂÇÑ´Ù. print <<¡±EOF¡±;
<TR> <TD>[ <A HREF=¡±$ENV{¡®SCRIPT_NAME¡¯}?ÀÛ¾÷=¾²±â¡±>±Û¾²±â</A>
]</TD> </TR> EOF }
sub time_conv {
# ½Ã°£º¯È¯ ÇÔ¼öÀÌ´Ù. # time() °ªÀ¸·Î ÀúÀåµÈ
ÀڷḦ Ãâ·ÂÇü½Ä¿¡ ¸Â°Ô # º¯È¯ÇÑ´Ù. local ($data) = @_;
local ($second, $minute, $hour, $date, $month, $year, $weekday,
$yearday, $Isdst) = localtime($data); $month ++; $year
+= 1900; $month < 10 && ($month = ¡°0$month¡±);
$date < 10 && ($date = ¡°0$date¡±); $hour <
10 && ($hour = ¡°0$hour¡±); $minute < 10 &&
($minute = ¡°0$minute¡±); $second < 10 && ($second
= ¡°0$second¡±); return ¡°$year/$month/$date $hour:$minute:$second¡±;
}
ÇÊÀÚ´Â Çѹø ÄÄÇ»ÅÍ¿¡ ÀÔ·ÂÇÑ
ÀڷḦ ¶Ç´Ù½Ã ÀÔ·ÂÇϱ⸦ ¸Å¿ì ½È¾îÇÑ´Ù. º» °Á´Â
http://way.co.kr ¿¡µµ µ¿½Ã¿¡ ¿Ã·ÁÁø´Ù. Áö¸é¿¡ ¼Ò°³µÈ ¼Ò½º¸¦
Å°º¸µå·Î ÀÔ·ÂÇÏÁö ¸»°í ÇÊÀÚÀÇ È¨ÆäÀÌÁö¸¦ ã¾Æ º¹»çÇؼ »ç¿ëÇϱâ
¹Ù¶õ´Ù.
¡ã top
|