11/3/10

Get Filename from Url Path - ASP Classic

The following VBScript function can be used in an ASP legacy application to extract the file name from a request.
public function GetFileName()
    dim files, url, segments

    'get then current url from the server variables
    url = Request.ServerVariables("path_info")

    segments = split(url,"/")

    'read the  last segment
    url = segments(ubound(segments))

    GetFileName = url

end function

The function returns the last segment of the path information or url. These are some examples:


Url
File Name
http://mydomain/products/default.asp
Default.asp
http://mydomain/products/
The default page in that directory(check web server settings)
http://mydomain/default.asp
Default.asp


This is useful when there is the need of an  application rule associated to the file name. For example, a new rule may need to be created on your legacy application because robots are exploiting a security hole in your application. You may want to protect some pages that can be used to update information , and you want to add a security policy to only those pages.  This function can be used to know the target page on the request and apply the security rule if the page has been configured to be protected.

Update - Function to get the server or domain name:

This function can be used to get the domain or server name where the application is hosted. This becomes useful when there is  a need to apply dynamic settings or rules based on the domain name. For example, we may want to connect to a different database when using a development server. In the world of .NET, this is easily managed by web.config settings for a build definition that targets a particular environment like Dev, Stage, Prod.

public function GetServerName()
    dim name

    'get then current url from the server variables
    name = Request.ServerVariables("server_name")  

    GetServerName = name

end function


I hope this is useful to someone.


thanks.


og-bit.com