ASP grab parameter from referral url

2019-09-09 12:26发布

问题:

Hi I am using a code to get the referral URL as you can see below:

sRef = encode(Request.ServerVariables("HTTP_REFERER"))

This code is getting the following URL: http://www.rzammit.com/pages/linux-form.asp?adv=101&loc=349

From that url I want to grab the ADV and LOC (Request.querystring doesnt work)

Any help please on how I can do this? All this is happening in a script.

回答1:

I can't believe I have done this. ASP Classic IS my first web programming love so I had to :)

<%

URL="http://www.rzammit.com/pages/linux-form.asp?adv=101&loc=349&websync=ert4545445&put=4563"


Response.write ("adv = " + GetVarValue(URL, "adv"))
response.write("<br>")
Response.write ("loc = " + GetVarValue(URL, "loc"))
response.write("<br>")
Response.write ("adv = " + GetVarValue(URL, "websync"))
response.write("<br>")
Response.write ("gput = " + GetVarValue(URL, "gput"))
response.write("<br>")
Response.write ("put = " + GetVarValue(URL, "put"))
response.write("<br>")

%>

<br><br><br>



<%
function GetVarValue(Source, VarName)
    pos1 = instr(source, varname + "=")

'to check if the variable was not found
    if pos1=0 then
        GetVarValue="Not Found!!!"
        exit function
    end if

    pos1 = pos1 + len(varName) + 1

    pos2=instr(mid(source,pos1), "&")-1


'to check if it was the last variable
    if pos2=-1 then
        str1 = mid(source, pos1)
    else
        str1 = mid(source, pos1, pos2)
    end if
    GetVarValue=str1



End Function

%>

With this function in your page you can query any variable by passing the variable name and URL string to GetVarValue!

(PS: if this answer works, accept this answer, it will do wonders to my StackOverflow Repo :) )



回答2:

Treat the sRef as a string and use Mid to get the values. Following code should be a starting point to get where you want to reach!

<%
sRef="http://www.rzammit.com/pages/linux-form.asp?adv=101&loc=349"

a=instr(sRef, "adv")+4
b=instr(sRef, "&loc")

response.write(mid(sRef ,a,b-a))
response.write("<br>")
response.write(mid(sRef ,b+5))

%>


回答3:

Use a Regexp and a dictionary to extract structured info from a string:

>> Dim s : s = "http://www.rzammit.com/pages/linux-form.asp?adv=101&loc=349"
>> Dim d : Set d = getKVP(s)
>> WScript.Echo "loc =", d("loc")
>> WScript.Echo "adv =", d("adv")
>> Function getKVP(s)
>>   Dim r, d, m
>>   set r = New RegExp
>>   r.Global = True
>>   r.Pattern = "(\w+)=(\w+)"
>>   set d = CreateObject("Scripting.Dictionary")
>>   For Each m In r.Execute(s)
>>       d(m.SubMatches(0)) = m.SubMatches(1)
>>   Next
>>   Set getKVP = d
>> End Function
>>
loc = 349
adv = 101

You may have to tinker with the simplistic pattern, but this approach scales much better than the Instr way.