14-2 Ū�����A�������ܼ�

Request ª«¥ó¤º§t Request.ServerVariables ¶°¦X¡A¦¹¶°¦X¥]§t¤F¦øªA¾¹Àô¹ÒÅܼƪº­È¡A³o¨ÇÀô¹ÒÅܼƲ[»\¤F¥Î¤áºÝ©M¦øªA¾¹ºÝªº¦UºØ¸ê°T¡C

¨Ò¦p¡A­Y­n±oª¾¥Î¤áºÝ¤Î¦øªA¾¹ºÝªº IP¡A¥i¥Î¤U¦C¤è¦¡¨ú±o¡G

½d¨Ò¦p¤U¡G

Example¡]requestIp01.asp¡^¡G

¤W­z½d¨Òªº­ì©lÀɦp¤U¡G

­ì©lÀÉ¡]requestIp01.asp¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<%@language=jscript%>
<%title="¨Ï¥Î Request.ServerVariables ¨Ó§ì¨ú IP"%>
<!--#include file="head.inc"-->
<hr>

<li>HTTP request ¨Ó·½ IP: Request.ServerVariables("REMOTE_ADDR") = <font color=red><%=Request.ServerVariables("REMOTE_ADDR")%></font>
<li>HTTP request ¦øªA¾¹ IP: Request.ServerVariables("LOCAL_ADDR") = <font color=red><%=Request.ServerVariables("LOCAL_ADDR")%></font>
<li>HTTP request ¥N²z¦øªA¾¹: Request.ServerVariables("HTTP_VIA") = <font color=red><%=Request.ServerVariables("HTTP_VIA")%></font>
<li>HTTP request ­ì©l¨Ó·½ IP: Request.ServerVariables("HTTP_X_FORWARDED_FOR") = <font color=red><%=Request.ServerVariables("HTTP_X_FORWARDED_FOR")%></font>

<hr>
<!--#include file="foot.inc"-->

¦pªG§A±o¨ìªº«È¤áºÝ IP ¨Ã¤£µ¥©ó§Aªº¹q¸£ªº IP¡A­þ»ò¥i¯à¬O§AªºÂsÄý¾¹³Q³]©w¦¨¸g¥Ñ¥N²z¦øªA¾¹¡]Proxy Server¡^¨Ó¨ú±oºô­¶¡A¦]¦¹ Request.ServerVariables("REMOTE_ADDR") ´NÅܦ¨¥N²z¦øªA¾¹ªº IP ¤F¡C¦b³oºØ±¡ªp¤U¡A§Ú­Ì¥i¥Ñ Request.ServerVariables("HTTP_VIA") ¨ÓÅã¥Ü©Ò¥Î¨ìªº¥N²z¦øªA¾¹¡A¨Ã¥Ñ Request.ServerVariables("HTTP_X_FORWARDED_FOR") ¨ú±o¯u¥¿«È¤áºÝªº IP¡C·íµM¡A¦pªG§AªºÂsÄý¾¹¨Ã¨S¦³³]©w¨Ï¥Î¥N²z¦øªA¾¹¡A¨º»ò¸g¥Ñ Request ª«¥ó©Ò§ì¨ìªºÅÜ¼Æ Request.ServerVariables("HTTP_VIA") ©M Request.ServerVariables("HTTP_X_FORWARDED_FOR") ³£¥u¬OªÅ¦r¦ê¦Ó¤w¡C

Hint
§A¥i¥H³]©w©Î¨ú®ø¥N²z¦øªA¾¹«á¡A¦A¬Ý¬Ý¤W­z½d¨Òªºµ²ªG¬O§_¦³ÅܤơC

¤@­Ó²³æªºÀ³¥Î¡A´N¬OÀˬd¨Ó·½ IP¡A¦Ó¨M©w¬O§_´£¨Ñºô­¶ÂsÄý¡C¨Ò¦p¡A¦pªG§Ú­Ìªº½u¤W±Ð§÷¥u¶}©ñµ¹²M¤jªº¦P¾Ç¬Ý¡A¨º§Ú­Ì´N¥i¥Hª½±µÀˬd­ì©l¨Ó·½ IP ¬O§_¬° 140.114.xxx.xxx¡A­Y¤£¬O¡A«h¤£¶Ç°eºô­¶¡A½d¨Ò¦p¤U¡G

Example¡]requestIp02.asp¡^¡G

¤W­z½d¨Òªº­ì©lÀɦp¤U¡G

­ì©lÀÉ¡]requestIp02.asp¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<%@language=jscript%>
<%title="­­¨î¯à°÷ÂsÄý¦¹ºô­¶ªº IP"%>
<!--#include file="head.inc"-->
<hr>

<%
ip=Request.ServerVariables("REMOTE_ADDR")+"";
proxy=Request.ServerVariables("HTTP_VIA")+"";
if (proxy!="undefined")		// ­Y¦³¨Ï¥Î¥N²z¦øªA¾¹¡A«h§ì¨ú­ì©l¥Î¤áºÝ IP
	ip=Request.ServerVariables("HTTP_X_FORWARDED_FOR")+"";
Response.write("­ì©l¥Î¤áºÝ IP = " + ip + "<br>");
Response.write("Proxy = " + proxy + "<br>");
domain="140.112.";
if (ip.indexOf(domain)!=0){
	Response.write("This page is not allowed!");
	Response.end;	// °±¤îºô­¶¶Ç°e¡I
}
%>

³o¬O¥x¤j IP ¯à°÷¬Ý¨ìªº¥¿±`ºô­¶¡I

<hr>
<!--#include file="foot.inc"-->

Hint
¦b¨Ï¥Î ASP ª«¥ó®É¡A­Y­n±Nª«¥ó¤ºªº¦r¦ê³]©w¨ì¥t¤@­Ó¦r¦êÅܼƮɡA§Ú­Ì³q±`¨Ï¥Î¡uip=Request.ServerVariables("REMOTE_ADDR")+""¡vªº¤è¦¡¡A¦p¦¹±j¨îÂ૬¡A¤~¯àÁ×§K¦]¬° Request.ServerVariables("REMOTE_ADDR") ¥i¯à¤£¬O¤@­Ó¦r¦êª«¥ó©Ò±a¨Óªº§xÂZ¡C

»P¦øªA¾¹¬ÛÃöªº¸ê°T¡A¥i¥H¦C¥X¦p¤U¡G

½d¨Ò¦p¤U¡G

Example¡]requestServer01.asp¡^¡G

¤W­z½d¨Òªº­ì©lÀɦp¤U¡G

­ì©lÀÉ¡]requestServer01.asp¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<%@language=jscript%>
<%title="¨Ï¥Î Request.ServerVariables ¨Ó¦C¥X»P¦øªA¾¹¬ÛÃöªº¸ê°T"%>
<!--#include file="head.inc"-->
<hr>

<ul>
<li>¦øªA¾¹ºô°ì¦WºÙ¡GRequest.ServerVariables("SERVER_NAME") = <font color=red><%=Request.ServerVariables("SERVER_NAME")%></font>
<li>¦øªA¾¹°ð¸¹¡GRequest.ServerVariables("SERVER_PORT") = <font color=red><%=Request.ServerVariables("SERVER_PORT")%></font>
<li>¦øªA¾¹¨ó©w¡GRequest.ServerVariables("SERVER_PROTOCOL") = <font color=red><%=Request.ServerVariables("SERVER_PROTOCOL")%></font>
<li>ºô­¶¦øªA¾¹³nÅé¦WºÙ¡GRequest.ServerVariables("SERVER_SOFTWARE") = <font color=red><%=Request.ServerVariables("SERVER_SOFTWARE")%></font>
<li>¦øªA¾¹¥[±K¡GRequest.ServerVariables("SERVER_PORT_SECURE") = <font color=red><%=Request.ServerVariables("SERVER_PORT_SECURE")%></font>
</ul>

<hr>
<!--#include file="foot.inc"-->

»Pºô­¶¸ô®|¬ÛÃöªº¸ê°T¡A¥i¥H¦C¥X¦p¤U¡G

½d¨Ò¦p¤U¡G

Example¡]requestPath01.asp¡^¡G

¤W­z½d¨Òªº­ì©lÀɦp¤U¡G

­ì©lÀÉ¡]requestPath01.asp¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<%@language=jscript%>
<%title="¨Ï¥Î Request.ServerVariables ¨Ó¦C¥X»Pºô­¶¸ô®|¬ÛÃöªº¸ê°T"%>
<!--#include file="head.inc"-->
<hr>

<ul>
<li>¦øªA¾¹®Ú¥Ø¿ýªº¹êÅéµwºÐ¸ô®|¡GRequest.ServerVariables("APPL_PHYSICAL_PATH") = <font color=red><%=Request.ServerVariables("APPL_PHYSICAL_PATH")%></font>
<li>ºô­¶¦b¹êÅéµwºÐªº¸ô®|¡GRequest.ServerVariables("PATH_TRANSLATED") = <font color=red><%=Request.ServerVariables("PATH_TRANSLATED")%></font>
<li>ºô­¶¬Û¹ïÀ³©ó¦øªA¾¹®Ú¥Ø¿ýªº¸ô®|¡GRequest.ServerVariables("PATH_INFO") = <font color=red><%=Request.ServerVariables("PATH_INFO")%></font>
<li>ºô­¶¬Û¹ïÀ³©ó¦øªA¾¹®Ú¥Ø¿ýªº¸ô®|¡GRequest.ServerVariables("SCRIPT_NAME") = <font color=red><%=Request.ServerVariables("SCRIPT_NAME")%></font>
<li>ºô­¶¬Û¹ïÀ³©ó¦øªA¾¹®Ú¥Ø¿ýªº¸ô®|¡GRequest.ServerVariables("URL") = <font color=red><%=Request.ServerVariables("URL")%></font>
</ul>

<hr>
<!--#include file="foot.inc"-->

¦b¤W­z½d¨Ò¤¤¡ARequest.ServerVariables("PATH_INFO")¡BRequest.ServerVariables("SCRIPT_NAME") ©M Request.ServerVariables("URL") ³£·|¦^¶Ç¤@¼Ëªºµ²ªG¡C

¨ä¥L¦³¥Îªº¸ê°T¤]³£¥i¥H¥Ñ Request.ServerVariables ¨ú±o¡A¨Ò¦p¡G

½d¨Ò¦p¤U¡G

Example¡]requestHttpReferer01.asp¡^¡G

¤W­z½d¨Òªº­ì©lÀɦp¤U¡G

­ì©lÀÉ¡]requestHttpReferer01.asp¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<%@language=jscript%>
<%title="¨Ï¥Î Request.ServerVariables ¨Ó§ì¨ú¡u³sµ²¦Ü¥Ø«eºô­¶ªº«e¤@­Óºô­¶¡v"%>
<!--#include file="head.inc"-->
<hr>

<ul>
<li>³sµ²¦Ü¥Ø«eºô­¶ªº«e¤@­Óºô­¶¡GRequest.ServerVariables("HTTP_REFERER") = <font color=red><%=Request.ServerVariables("HTTP_REFERER")%></font>
<li>¥Î¤áºÝ©Ò¥ÎªºÂsÄý¾¹¡GRequest.ServerVariables("HTTP_USER_AGENT") = <font color=red><%=Request.ServerVariables("HTTP_USER_AGENT")%></font>
<li>¥Î¤áºÝµn¿ý¦Üºô­¶ªº±b¸¹¡GRequest.ServerVariables("LOGON_USER") = <font color=red><%=Request.ServerVariables("LOGON_USER")%></font>
</ul>

<hr>
<!--#include file="foot.inc"-->

¨Ï¥Î Request.ServerVariables("HTTP_REFERER")¡A§A¥i¥H±oª¾¤U¦C¨Æ¶µ¡G

­Y±ý±N Request.ServerVariables ©Ò¦³¬ÛÃöÅܼƤ@¦¸¦L¥X¡A¥i¨£¤U¦C½d¨Ò¡G

Example¡]request/serverVariables.asp¡^¡G

¤W­z½d¨Òªº­ì©lÀɦp¤U¡G

­ì©lÀÉ¡]request/serverVariables.asp¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<%@ language="jscript" %>
<% title="¦C¥X Request.ServerVariables" %>
<!--#include file="../head.inc"-->
<hr>

<!--#include file="../listdict.inc"-->
<% listdict(Request.ServerVariables, "Request.ServerVariables"); %>

<hr>
<!--#include file="../foot.inc"-->

¦¹­ì©lÀÉ¥]§t¤F listdict.inc¡A¨ä­ì©lÀɮצp¤U¡G

­ì©lÀÉ¡]listdict.inc¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<script runat=server language=jscript>
function listdict(dict, dictname){
	Response.Write("<table border=1 align=center>\n");
	Response.Write("<tr><th colspan=2 bgcolor=cyan>" + dictname + "\n");
	Response.Write("<tr><th>Names<th>Values\n");
	var Enum=new Enumerator(dict);
	for (Enum.moveFirst(); !Enum.atEnd(); Enum.moveNext()){
		Response.Write("<tr><td>");
		Response.Write(dictname+"(\""+Enum.item()+"\")\n");
		Response.Write("<td>");
		Response.Write(dict(Enum.item())+"&nbsp;");
	}
	Response.write("</table>\n");
}
</script>

<script runat=server language=vbscript>
function listdict(dict, dictname)
	dim key
	response.write("<table border=1 align=center>" & vbcrlf)
	response.write("<tr><th colspan=2 bgcolor=cyan>" & dictname & vbcrlf)
	response.write("<tr><th>Names<th>Values" & vbcrlf)
	For Each key in dict
		response.write("<tr>" & newline)
		response.write("<td>" & dictname & "(""" & key & """)</td>")
		response.write("<td>&nbsp;" & dict(key) & "</td>")
		response.write("</tr>" & vbcrlf)
	next
	response.write("</table>" & vbcrlf)
end function
</script>

listdict.inc ¥]§t¨â­Ó¨ç¼Æ¡A¤À§O¥Î©ó¹ï¥I JScript ©M VBScript¡A¥i¦C¦L¥X¥ô¦ó Dictionary ÅܼơA¨Ò¦p¤W­z½d¨Òªº Request.ServerVariables¡C¦b JScript ªº listdict() ¨ç¼Æ¤¤¡A§Ú­Ì¥Î¨ì¤F Enumerator ªºª«¥ó¡A¦¹ª«¥ó¬OÃþ¦ü©ó VBScript ªº Dictionary ÅܼơA¥i¥Î¦r¦ê¨Ó¯Á¤Þ¥t¤@­Ó¦r¦ê¡C

­Y­nª¾¹D Request.ServerVariables ©Ò¥]§tÅܼƪº·N¸q¡A¥i¥Hª½±µ¬d¬Ý¤U¦Cºô­¶¡G

http://www.devguru.com/index.asp?page=/technologies/asp/quickref/request_servervariables.html
ÁA¸Ñ¨Ãµ½¥Î³o¨Ç¦øªA¾¹Àô¹ÒÅܼơA¥i¨Ï©pªº Web µ{¦¡³]­p¨Æ¥b¥\­¿¡C

¤W­z¦L©Ò¦³ Request.ServerVariables ªº¤èªk¡A¬O¥Î JScript ¨Ó¹F¦¨¡A§Ú­Ì¤]¥i¥H¨Ï¥Î VBScript ¨Ó¹F¨ì¬Û¦P¥\¯à¡A½Ð¨£½d¨Òµ{¦¡½X¡uexample/request/serverVariables_vbs.asp¡v¡A¦¹µ{¦¡½X¤]¥]§t¤F¬Û¦Pªº listdict.inc¡A¥H«K¦C¥X©Ò¦³ªº¦øªA¾¹ÅܼơC


JScript µ{¦¡³]­p»PÀ³¥Î¡G¥Î©ó¦øªA¾¹ºÝªº ASP Àô¹Ò