关于防止文件下载盗链之用js来防止文件地址盗链
如果是页面防盗链的话,通常的方法都是用ServerVariables(“Server_Name“)结合ServerVariables(“Http_Referer”)来防止。如下:
DIM servUrl, fromUrl
servUrl = Request.ServerVariables("SERVER_NAME")
fromUrl = Request.ServerVariables("HTTP_REFERER")
If (mid(fromUrl,8,len(servUrl))) <> servUrl then
Response.Write("<script>alert('请从正确的页面登入!')</script>")
Response.End
else
......
End If
还有就是可以结合Session来防止页面盗链和文件盗链。
再有就是在服务器端来设置。
以上方法,我会在以后的文章写出来,下面让我们来看看用javascript(纯静态文件)如何来实现防止文件盗链呢?如下源码只起一个抛砖引玉的作用,希望大家多提意见。
---------------------------------------------------------------------
**********************************************
首先,比如我们有一音乐视听页面。源码如下:
先建一文件夹为:MusicMP3,在此文件夹下面放一首MP3,MP3重命名为921239.mp3
---------------------------------------------------------------------
**********************************************
Music.htm
-----------------
<html>
<head>
<title>Music</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<script language="JavaScript">
function Play(URL){
play_Win=window.open( 'Play.htm?url='+URL,'bluedestiny', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=470,height=290,top=210,left=158');
}
</script>
<body>
<p> <a href="#" onclick="javascript:Play('XPGJFUESRYVIESRSXNJFUXQDKWD&江南')">Music1</a></p>
</body>
</html>
怎么会有一长串的字符?请注意这一长串参数的格式,等会儿有用的。
---------------------------------------------------------------------------------
**************************************************
Music.htm
-----------------
<html>
<head>
<title>PlayMusic</title>
<script language=jscript src=Play_url.js></script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head>
<body bgcolor="#999999">
<TABLE cellSpacing=0 cellPadding=0 width=468 align=center border=0>
<!--DWLayoutTable-->
<TBODY>
<TR>
<TD width="468" height=60 align=middle valign="top"><font size="2">您现在收听的是:</font><font color="#FF0000" size=2>
<script language="javascript">
if(wor==undefined)
{
document.write("无曲目")
}
else
document.write(wor)
</script>
</font></TD>
</TR>
</TBODY>
</TABLE>
<TABLE height=60 cellSpacing=0 cellPadding=0 width=468 align=center border=0>
<TBODY>
<TR>
<TD class=box bgColor=#eeeeee height=60 align="center">
<div align="center">
<script language="javascript" src="MP.js"></script>
</div>
</TD>
</TR>
</TBODY>
</TABLE>
<div align="center">
</body>
</html>
上面有一段引入了两段js的代码Play_url.js,MP.js这里就是关键了。接下来我们就来看看这段代码。
-------------------------------------------------------------------------------------------
****************************************************************************
MP.jps
---------------------------------
document.write ("<OBJECT id=Player2 name=Player classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 border=\"0\" width=100% height=68 type=application/x-oleobject standby=\"Loading Windows Media Player components...\">\n")
document.write ("<param name=\"AudioStream\" value=\"-1\">\n")
document.write ("<param name=\"AutoSize\" value=\"0\">\n")
document.write ("<param name=\"AutoStart\" value=\"-1\">\n")
document.write ("<param name=\"AnimationAtStart\" value=\"-1\">\n")
document.write ("<param name=\"AllowScan\" value=\"-1\">\n")
document.write ("<param name=\"AllowChangeDisplaySize\" value=\"-1\">\n")
document.write ("<param name=\"AutoRewind\" value=\"0\">\n")
document.write ("<param name=\"Balance\" value=\"10\">\n")
document.write ("<param name=\"BaseURL\" value>\n")
document.write ("<param name=\"BufferingTime\" value=\"5\">\n")
document.write ("<param name=\"CaptioningID\" value>\n")
document.write ("<param name=\"ClickToPlay\" value=\"-1\">\n")
document.write ("<param name=\"CursorType\" value=\"0\">\n")
document.write ("<param name=\"CurrentPosition\" value=\"-1\">\n")
document.write ("<param name=\"CurrentMarker\" value=\"0\">\n")
document.write ("<param name=\"DefaultFrame\" value>\n")
document.write ("<param name=\"DisplayBackColor\" value=\"0\">\n")
document.write ("<param name=\"DisplayForeColor\" value=\"16777215\">\n")
document.write ("<param name=\"DisplayMode\" value=\"0\">\n")
document.write ("<param name=\"DisplaySize\" value=\"4\">\n")
document.write ("<param name=\"Enabled\" value=\"-1\">\n")
document.write ("<param name=\"EnableContextMenu\" value=\"0\">\n")
document.write ("<param name=\"EnablePositionControls\" value=\"-1\">\n")
document.write ("<param name=\"EnableFullScreenControls\" value=\"0\">\n")
document.write ("<param name=\"EnableTracker\" value=\"-1\">\n")
document.write ("<param name=\"Filename\" value='" + str + "'>\n")
document.write ("<param name=\"InvokeURLs\" value=\"-1\">\n")
document.write ("<param name=\"Language\" value=\"-1\">\n")
document.write ("<param name=\"Mute\" value=\"0\">\n")
document.write ("<param name=\"PlayCount\" value=\"0\">\n")
document.write ("<param name=\"PreviewMode\" value=\"0\">\n")
document.write ("<param name=\"Rate\" value=\"1\">\n")
document.write ("<param name=\"SAMILang\" value>\n")
document.write ("<param name=\"SAMIStyle\" value>\n")
document.write ("<param name=\"SAMIFileName\" value>\n")
document.write ("<param name=\"SelectionStart\" value=\"0\">\n")
document.write ("<param name=\"SelectionEnd\" value=\"0\">\n")
document.write ("<param name=\"SendOpenStateChangeEvents\" value=\"-1\">\n")
document.write ("<param name=\"SendWarningEvents\" value=\"-1\">\n")
document.write ("<param name=\"SendErrorEvents\" value=\"-1\">\n")
document.write ("<param name=\"SendKeyboardEvents\" value=\"0\">\n")
document.write ("<param name=\"SendMouseClickEvents\" value=\"0\">\n")
document.write ("<param name=\"SendMouseMoveEvents\" value=\"0\">\n")
document.write ("<param name=\"SendPlayStateChangeEvents\" value=\"-1\">\n")
document.write ("<param name=\"ShowCaptioning\" value=\"0\">\n")
document.write ("<param name=\"ShowControls\" value=\"-1\">\n")
document.write ("<param name=\"ShowAudioControls\" value=\"-1\">\n")
document.write ("<param name=\"ShowDisplay\" value=\"0\">\n")
document.write ("<param name=\"ShowGotoBar\" value=\"0\">\n")
document.write ("<param name=\"ShowPositionControls\" value=\"-1\">\n")
document.write ("<param name=\"ShowStatusBar\" value=\"-1\">\n")
document.write ("<param name=\"ShowTracker\" value=\"-1\">\n")
document.write ("<param name=\"TransparentAtStart\" value=\"0\">\n")
document.write ("<param name=\"VideoBorderWidth\" value=\"0\">\n")
document.write ("<param name=\"VideoBorderColor\" value=\"0\">\n")
document.write ("<param name=\"VideoBorder3D\" value=\"0\">\n")
document.write ("<param name=\"Volume\" value=\"0\">\n")
document.write ("<param name=\"WindowlessVideo\" value=\"0\">\n")
document.write ("<embed type=\"application/x-mplayer2\" pluginspage=\"http://www.microsoft.com/windows/mediaplayer/download/default.asp\" Name=\"Player\" width=\"300\" height=\"66\" border=\"0\" SHOWSTATUSBAR=\"-1\" SHOWCONTROLS=\"0\" SHOWGOTOBAR=\"0\" SHOWDISPLAY=\"-1\" INVOKEURLS=\"-1\" AUTOSTART=\"1\" CLICKTOPLAY=\"0\" DisplayForeColor=\"12945678\">\n")
document.write ("</OBJECT>\n")
----------------------------------------------
************************************
Play_url.js
----------------------
<script language="JavaScript" type="text/JavaScript">
var searchString=location.search // <---------取出传递过来的参数
var pos=searchString.indexOf("?url=")
if (pos==0){
var str,str1,arr,wor;
arr=searchString.split('&'); //<----------把地址和歌名分离
if (arr.length > 1){
wor=arr[1];
}
else{
wor="";
}
// 下面这段就是这里的关键,刚才的那一段字符串就在这里解码,明白了吗?
str=arr[0].substr(5,searchString.length);
str=str.replace(/YVI/g,"1")
str=str.replace(/ESR/g,"2")
str=str.replace(/SXN/g,"3")
str=str.replace(/STI/g,"4")
str=str.replace(/WFU/g,"5")
str=str.replace(/LQU/g,"6")
str=str.replace(/QGI/g,"7")
str=str.replace(/BLA/g,"8")
str=str.replace(/JFU/g,"9")
str=str.replace(/LSN/g,"0")
str=str.replace(/XPG/g,"/")
str=str.replace(/XQD/g,".")
str=str.replace(/ /g,"")
str=str.replace(/%20/g,"")
str=str.replace(/KWD/g,"MP3")
str="127.0.0.1//MusicMP3/" + str
document.write(str)
}
</script>