Advertisement
ASP_Volume3 Miscellaneous #53193

Import request variables

PHP offers a feature called Autoglobals and the importrequestvariables function. ASP doesn't. This article shows how we can do a simular thing, and address variables directly without having to do a lot of bulk and useless work.

AI

Shrnutí AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Zdrojový kód
original-source
<FONT SIZE=-1>
I can imagine those not familiar with PHP don't have a clue what I'm talking about. Well, say you access a page like "somepage.php?somefield=somevalue", you would have to use Request.QueryString("Somefield") to get the value. This very often results in pages starting with 5, 10, 20 or more lines looking like this:
<PRE>
SomeField = Request.QueryString("Somefield")
SomeOtherField = Request.QueryString("SomeOtherField")
... and so on
</PRE>
To be honest, it's quite ugly, and it's quite annoying for the programmer, always having to write a simular block of code, not doing any actual coding.
<p>This script however, mimics a part of the PHP autoglobals behaviour by automatically assinging the value of a field to it's fieldname variable in VB. It does this by using the Execute statement. Execute allows us to execute a string. That string we can build up in the ASP page with whatever we want. What interests us the most is assigning values to variables
Take this for example:
<PRE>
Execute "X = ""This is the value of X"""
Response.Write X
</PRE>
Running the above code will show the text "This is the value of X". If we now do a simular thing with the QueryString and the Form collections (which we can itterate), we can automatically import all the variables.
<PRE>
For Each Item In Request.QueryString
 ItemValue = Request.QueryString(Item)
 Execute Item & " = """ & ItemValue & """"
Next
</PRE>
<BR>
Something that might happen is that you come across an reserved name (public, sub, function, ...). To prevent this, you can add a prefix to the variable name. You will have to address the variable with the prefix, but you won't run the risk (or at least a lot less) to run into errors.
<P>
I've attached a completely worked out version of the process I described above. To use it, just import the file at the top of the ASP page you want to use it in.
<BR>
<PRE><!-- #include file="importrequest.asp" --></PRE>
<P>
I hope it is to any use, it sure has been to me. Rating and feedback appreciated.
<P>
<B>Note: someone found security issue, to fix that, you need to replace the execute line with following line:
<PRE>
Execute Item & " = """ & Replace(ItemValue,"""","""""") & """"
</PRE>
</FONT>
Původní komentáře (3)
Obnoveno z Wayback Machine