Forum Moderators: mack

Message Too Old, No Replies

Getting the filename from the URL in ASP

         

chris_f

4:35 pm on Oct 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi All,

I am trying to find a way to get the filename from the url in ASP. For example if the url was:

w w w . domain.com/page2.asp

I want it to put page2 or page2.asp in a variable called 'PageName'.

Can anyone point me to any good resources that can give me examples of this.

Thanks,
Chris.

amoore

5:05 pm on Oct 10, 2002 (gmt 0)

10+ Year Member



The environment variable SCRIPT_FILENAME may have what you're looking for. It's the filename of the script being run. SCRIPT_NAME may also have it. That's the name of the "file" in URI space. I don't know how to get environment variables in ASP, but I'm sure you can. (Just like you get the user agent in an environment variable)

If you want to know all of the environment variables in a request, you can use a script like this perl script:


#!/usr/local/perl -w
use strict;
print "Content-type: text/html\n\n";
print join( "<br>\n", map { $_ . " => " . $ENV{$_} } keys( %ENV ) );

or an equivalent one in ASP.

chris_f

5:10 pm on Oct 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Amoore,

However, I really need an ASP resource/example.

Chris

amoore

5:18 pm on Oct 10, 2002 (gmt 0)

10+ Year Member



it's not official Self-Reliance Day, so I searched google for "environment variables in ASP" for you. The first result gives a pretty good explanation of pulling evironment variables in ASP. There's no silver platter on that page, but it still is pretty thorough. It shows you how to create a page in ASP which displays all of the environment variables and even mentions one of the ones I told you about as an example.

Good luck.

korkus2000

5:24 pm on Oct 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use the server variable PATH_INFO or SCRIPT_NAME.

You will get everything after the SERVER_NAME or domain name including the /. You just have to snip that off the front and anything else you dont want. You can just use a left to kill the slash.

<%= Request.ServerVariables("SCRIPT_NAME") %>

or

<%= Request.ServerVariables("PATH_INFO") %>

chris_f

5:30 pm on Oct 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks KK2000,

However, who do I snip the text?

Chris

korkus2000

5:40 pm on Oct 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't follow.

txbakers

5:42 pm on Oct 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Once you have the server.variable, you just convert it to a string, and manipulate it just like any other string.

chris_f

5:46 pm on Oct 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's the problem.

How would I manipulate the string?

Chris.

korkus2000

5:58 pm on Oct 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<%
dim textlength, nonslashtext
textlength = Len(Request.ServerVariables("SCRIPT_NAME"))
nonslashtext = textlength - 1
Response.write(Right(Request.ServerVariables("SCRIPT_NAME"),nonslashtext))
%>

from www.domain.com/lovinit.asp this code gives you lovinit.asp instead of /lovinit.asp

chris_f

6:01 pm on Oct 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Brilliant.

KK2000 you are a life saver. Thank you SOOOOOO much.

Chris.

txbakers

8:15 pm on Oct 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



and he makes a pretty darn good Martini from what I hear as well.