How to Find the Current URL in ASP

The full URL to a page comes in three parts: The domain name, the path to the file then the filename, and the QueryString. For example, take the URL http://www.example.com/example/page.asp?name=Bob. The three parts of this are:

1. The domain name: www.example.com
2. The path to the page: /example/page.asp
3. The QueryString: name=Bob

So how do you find it all out with your own scripts?

Well the following code should do it:

ASP:
  1. <%@language="VBScript"%>
  2. <%
  3.   Dim strDomain, strPath, strQueryString, strURL
  4.   ‘ find out the domain:
  5.   strDomain = Request.ServerVariables("HTTP_HOST")
  6.   ‘ find out the path to the current file:
  7.   strPath = Request.ServerVariables("URL")
  8.   ‘ find out the QueryString:
  9.   strQueryString = Request.ServerVariables("QUERY_STRING")
  10.   ‘ put it all together:
  11.   strURL = "http://" & strDomain & strPath & "?" & strQueryString
  12.   Response.Write "The current URL is: " & strURL
  13. %>

If you have a website, consider joining the Money4Banners advertising network. They will pay you £10 + £5 each month for displaying a small advert on three of your pages, regardless of traffic. American webmasters are welcome, and since £10 == $20, you make more!


One Response to “How to Find the Current URL in ASP”  

  1. Gravatar Icon 1 donna

    How do I capure the IP address and store it only? I am building a web-based survey and I want to pass the data into the database.

    Thanks,
    donna

Leave a Reply

You must log in to post a comment.


Categories