2016-03-23 53 views
0

我有一個使用vbscript的經典asp網站。如何檢查用戶是否來自我的網站上的某個目錄?我有這個代碼asp classic中提供了通配符?

<%Response.Write(Request.ServerVariables("http_referer"))%> 

其中寫道:http://example.com/mobile/REFER.asp。我想寫一個if/else語句,首先檢查引用的URI是否在目錄http://example.com/mobile/中。

所以我的代碼應該像下面的東西。但我不確定語法。是否有這樣的通配符字符是asp?

<% Request.ServerVariables("http_referer") == "http://example.com/mobile/*" 

最後,我想用它來寫一個if/else語句

<% if Request.ServerVariables("http_referer") != "http://example.com/mobile/*" then 
null; elseif (screen.width <= 699) { 
    document.location = "/mobile/mobile_home.asp"; 
    } %> 

===

截至加速編輯@ mikeyq6的JavaScript樣本這裏面工作:

<script type="text/javascript"> 
if(document.referrer.indexOf('/mobile') > -1 && 
    screen.width <= 699) { 
    document.location = "/mobile/mobile_home.asp"; 
} 
</script> 
+0

記住,使用硬編碼的URL ISN」的任何方法特別是從維護的角度來看,這是最好的想法。如果您確實需要這樣做,請考慮在URL中存儲您希望在[應用程序](https://msdn.microsoft.com/zh-cn/library/ms525360(v = vs.90).aspx)對象級別變量,這樣你就可以在'global.asa'中定義它,並在需要時調用它。 – Lankymart

回答

0

基本上你正在處理的是一個字符串,所以你不能以這種方式使用通配符(除非你是使用正則表達式,就像在這種情況下用大錘擊打釘子一樣)

使用InStr函數檢查值是否包含您正在查找的字符串會容易得多。例如:在這裏InStr

<% if InStr(Request.ServerVariables("http_referer"), "http://example.com/mobile/") = 0 then 
    null 
elseif (screen.width <= 699) { 
    document.location = "/mobile/mobile_home.asp"; 
} %> 

更多信息: http://www.w3schools.com/asp/func_instr.asp

你可以在JavaScript類似的還有東西:

if(window.location.href.indexOf("http://example.com/mobile/") > -1 && 
    screen.width <= 699) { 

    document.location = "/mobile/mobile_home.asp"; 
} 

請注意,我簡化了if...else爲好。你不需要這兩種情況。

+0

糟糕。所以顯然我混合了asp和javascript。 InStr(Request.ServerVariables(「http_referer」))是否有一個JavaScript等價物? – testing123

+0

@ user5753132我在答案底部的javascript中添加了一個示例 – mikeyq6

0

你知道你正在尋找固定的URL的長度,所以只是看看引薦比賽的第n個字符是:

const BASE_DIR = "http://example.com/mobile/" 

dim referer: referer = lcase(Request.ServerVariables("http_referer")) 

if left(referer, len(BASE_DIR)) = BASE_DIR then 
    ... 
else 
    ... 
end if