PDA

View Full Version : basic: setting variable


code blue
05-18-2006, 08:45 AM
Hi all, sorry, i'm a dimwit and know bugger all about asp but i need your help!

I have managed to implement an asp/css style sheet switcher on my site, but i'm just stuck on one thing...

the external .asp file that does the switch, then redirects the browser to a specified url.

what i need to do so i dont have to include this code on every page, is set the url of each page as a variable that is then used by this script.

After some searching i came up with this to set the variable (and this would be repeated on each page, but setting it to a different value each time):


<% Dim myVar
strCurrPage = "http://mysite.com.au/index_style_change.asp"
%>


then when the "change style" button is clicked it goes to the seperate asp file which has at the end:


<%
Dim strNewStyle
strNewStyle = Request.QueryString("style")
If Len(strNewStyle) > 1 Then
Response.Cookies("stylesheet") = strNewStyle
Response.Cookies("stylesheet").Expires = Date + 30
End If
Response.Redirect ("page.asp")
%>


here i'm only worried about the last line where "page.asp" is, i need it to add in the variable instead. I tried (and variations of):


Response.Redirect ("<%= strCurrPage %>")


but no luck.

can someone help me do this?

Spudhead
05-18-2006, 12:38 PM
I'm confused :confused:

You want a button or link or something on each page that, when clicked, sets a cookie with the filename of a stylesheet?

It sounds like you need to look at server-side includes; an easy way of repeating chunks of code across pages.

I'd also have a look at the ServerVariables collection - there are a couple of variables in there that you can use to get the name of the current page without having to hardcode it yourself.

degsy
05-18-2006, 04:20 PM
<% Dim myVar
strCurrPage = "http://mysite.com.au/index_style_change.asp"
Session("currPage") = strCurrPage
%>


Response.Redirect Session("currPage")

code blue
05-19-2006, 02:31 AM
It sounds like you need to look at server-side includes; an easy way of repeating chunks of code across pages.

I'd also have a look at the ServerVariables collection - there are a couple of variables in there that you can use to get the name of the current page without having to hardcode it yourself.

spud - hiya - if i knew the first thing about server-side includes i'd consider it, but i dont... i'm normally just a static page programmer, and i'm struggling terribly with asp as it is! also, the client only runs asp pages for some reason, and I just have to try make this work.

what is the serverVariables collection?? where can i find out more about this?

degsy - thanks - that worked perfectly! :thumbsup:

ghell
05-20-2006, 11:02 PM
server side includes you can imagine just joining together text of pages before the asp interprets it. they need to be in the non asp section (outside the <% %> and are done like this
<!--#include file="somefile.ext" -->
or
<!--#include virtual="scripts/someotherfile.inc" -->

something along those lines.
Request.ServerVariables collection contains the page's headers. all the http headers start with HTTP_ in their name.

this loop will show you all servervariables:<%
For Each sv In Request.ServerVariables
Response.Write sv & ": " & Request.ServerVariables(sv) & "<br />"
Next
%>

Sessions (as somone suggested their use) are similar to cookies but use of them is not advised as they are server resource hogs (cookies are stored on the client instead, the more data you have stored in sessions the worse your performance will be so if you are storing a lot of data (like a few strings of text per user) or have lots of users they should definately not be used unless absolutely neccessary.

I'm not really sure what you are trying to do with your page however. If you are trying to theme your page you can store a user's theme name in a cookie or database (if they are logged in users) you can then use the name of the theme/skin to just point the user at a different stylesheet.

<link rel="stylesheet" type="text/css" src="themes/<%=strTheme%>/style.css" />

for example would do it if you had strTheme populated beforehand. Setting a default value for if populating it returns "" (no cookie or db entry or whatever) is also advised.

code blue
05-23-2006, 05:38 AM
thanks ghell (i think!)

I've given up on trying to use asp for this (it was a css style changer basically), and have instead reverted to a basic javascript function which does the same thing (although much more easily).

However: i just used that little snippet of code you supplied and it output this on the page:

ALL_HTTP: HTTP_ACCEPT:text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 HTTP_ACCEPT_LANGUAGE:en-us,en;q=0.5 HTTP_CONNECTION:keep-alive HTTP_HOST:localhost HTTP_USER_AGENT:Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3 HTTP_COOKIE:style=; stylesheet=base%2Ecss; ASPSESSIONIDSQSDCTTT=LMJNHJCBMCOAOOODIOAHGBOF HTTP_ACCEPT_ENCODING:gzip,deflate HTTP_ACCEPT_CHARSET:ISO-8859-1,utf-8;q=0.7,*;q=0.7 HTTP_KEEP_ALIVE:300 HTTP_CACHE_CONTROL:max-age=0
ALL_RAW: Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Language: en-us,en;q=0.5 Connection: keep-alive Host: localhost User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3 Cookie: style=; stylesheet=base%2Ecss; ASPSESSIONIDSQSDCTTT=LMJNHJCBMCOAOOODIOAHGBOF Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Cache-Control: max-age=0
APPL_MD_PATH: /LM/W3SVC/1/ROOT
APPL_PHYSICAL_PATH: c:\inetpub\wwwroot\
AUTH_PASSWORD:
AUTH_TYPE:
AUTH_USER:
CERT_COOKIE:
CERT_FLAGS:
CERT_ISSUER:
CERT_KEYSIZE:
CERT_SECRETKEYSIZE:
CERT_SERIALNUMBER:
CERT_SERVER_ISSUER:
CERT_SERVER_SUBJECT:
CERT_SUBJECT:
CONTENT_LENGTH: 0
CONTENT_TYPE:
GATEWAY_INTERFACE: CGI/1.1
HTTPS: off
HTTPS_KEYSIZE:
HTTPS_SECRETKEYSIZE:
HTTPS_SERVER_ISSUER:
HTTPS_SERVER_SUBJECT:
INSTANCE_ID: 1
INSTANCE_META_PATH: /LM/W3SVC/1
LOCAL_ADDR: 1.0.0.1
LOGON_USER:
PATH_INFO: /depend/about/living_incontinence.asp
PATH_TRANSLATED: c:\inetpub\wwwroot\depend\about\living_incontinence.asp
QUERY_STRING:
REMOTE_ADDR: 1.0.0.1
REMOTE_HOST: 1.0.0.1
REMOTE_USER:
REQUEST_METHOD: GET
SCRIPT_NAME: /depend/about/living_incontinence.asp
SERVER_NAME: localhost
SERVER_PORT: 80
SERVER_PORT_SECURE: 0
SERVER_PROTOCOL: HTTP/1.1
SERVER_SOFTWARE: Microsoft-IIS/5.1
URL: /depend/about/living_incontinence.asp
HTTP_ACCEPT: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
HTTP_ACCEPT_LANGUAGE: en-us,en;q=0.5
HTTP_CONNECTION: keep-alive
HTTP_HOST: localhost
HTTP_USER_AGENT: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3
HTTP_COOKIE: style=; stylesheet=base%2Ecss; ASPSESSIONIDSQSDCTTT=LMJNHJCBMCOAOOODIOAHGBOF
HTTP_ACCEPT_ENCODING: gzip,deflate
HTTP_ACCEPT_CHARSET: ISO-8859-1,utf-8;q=0.7,*;q=0.7
HTTP_KEEP_ALIVE: 300
HTTP_CACHE_CONTROL: max-age=0


so form that info, the current URL would be "PATH_TRANSLATED: c:\inetpub\wwwroot\depend\about\living_incontinence.asp"?? is that right?

If i wanted to include the url in the page, how would i output it?

"<%=PATH_TRANSLATED%>" ???

or am i waaaay off?

ghell
05-23-2006, 12:30 PM
You would need to use Request.ServerVariables("PATH_TRANSLATED") but that gets the physical path on the server in a similar way to what you would get if you did Server.MapPath(Request.ServerVariables("PATH_INFO"))

if you have the thing on the same site you dont need the full http://blahblah/somedirectories/style.css you can just use a relative path.

to get the current url you can concatenate "http:// " SERVER_NAME and URL i think (as well as Request.QueryString without any parenthesis if you want that part of the url as well)

all of these just return strings from a collection so normal string operations work on them etc.

from your HTTP_COOKIE server variable id say just usestrStyle = Request.Cookies("stylesheet")
If strStyle = "" Then strStyle = "base.css"
Response.Write "<link rel=""stylesheet"" type=""text/css"" src=""" & strStyle & """ />"

code blue
05-24-2006, 02:04 AM
ok... thanks, but lost me a bit there...

how would i actually get the page to display it's own URL within it's text?


<%
Request.ServerVariables("PATH_TRANSLATED")
Response.Write("PATH_TRANSLATED")
%>

??

all this prints to the page is "PATH_TRANSLATED", shouldn't it display the variable??

also - is there a serverVariable for the title of the page?

Spudhead
05-24-2006, 11:06 AM
Close, but not quite :)


dim sPath
sPath = Request.ServerVariables("PATH_TRANSLATED")
Response.Write(sPath)


When using response.write() - if you put stuff in quotes, it'll treat it as a string and write, literally, whatever occurs inside those quotes. If you don't put it in quotes, it assumes it's a variable.

ghell
05-24-2006, 12:47 PM
Request.Form Request.QueryString and Request.ServerVariables are all collections/dictionaries/hashmaps whatever. In these you put a string in (key) and it returns the value as a string.Response.Write "http://" & Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL")something like that should print it out as I said earlier.

code blue
05-25-2006, 12:32 AM
I will be playing with this a bit more and see if i can up my level of understanding (experimentation is the best way to learn sometimes!:) ), at the moment it's all waaay over my head, but the result is good - so thanks!:thumbsup:

code blue
05-25-2006, 01:33 AM
oh, but still - is there a serverVariable for the name of a page?

(ie the text that comes up in the top bar of the browser when you are on the page)...

i can't see it in the chunk of output i posted above..

ghell
05-25-2006, 01:13 PM
No there is no servervariable for the page title. This would be the text you put in your <title>...</title> tag anyway though so you should already know that :thumbsup:

ServerVariables are mostly the HTTP headers of the page :)

degsy
05-25-2006, 04:09 PM
You can split the URL or Script Name Server Variable.


<%
SCRIPT_NAME = Request.ServerVariables("SCRIPT_NAME")
SCRIPT_NAME = "my.file.with.periods.asp"
SCRIPT_NAME = Split(SCRIPT_NAME,"/")
Page = SCRIPT_NAME(Ubound(SCRIPT_NAME))
Page = Split(Page,".")

For x=0 To Ubound(Page)-1
Title = Title & Page(x) & "."
Next
Title = Left(Title, Len(Title)-1)
%>
<title><%=Title%></title>

The loop is just there incase you have filenames with multiple periods. If you want the extension aswell then remove the loop.

code blue
05-26-2006, 01:21 AM
thanks again guys.. but i might leave it at that.;)

I was trying to set up an email a friend type link which would have the page name as the subjects and the url of the page in the body of the email.

The url is working perfectly (using ghell's method - thanks:thumbsup: ), but i've had no luck pulling in the page title. I think i may just hard-code it...

Or i could set it as a variable i guess, and then call it into the mailto link. That atleast would mean that i only need one mailto link accross all pages.

thanks again everyone for your patience and help.:cool:

degsy
05-26-2006, 02:43 PM
Just to note you can also get the filename like this


<%
filename = "this.is.a.file.asp"
Response.Write filename & "<br>"
ext = InStrRev(filename, ".")
Response.Write ext & "<br>"
If ext > 0 Then
fn = Left(filename, ext-1)
End If
Response.Write fn

%>



If you want the actual page title from within a HTML file then you need to do some other extraction


<%
Filename = "fiel.htm"
Set fs=Server.CreateObject("Scripting.FileSystemObject")

Set f=fs.OpenTextFile(Server.MapPath(Filename), 1)
contents = f.ReadAll
f.Close

Set f=Nothing
Set fs=Nothing
'contents = Split(contents, vbCRLF)
startpos = Instr(Lcase(contents), "<title>")+ 7 ' or Len("<title>")
endpos = Instr(Lcase(contents), "</title>")
title = Mid(contents, startpos, endpos-startpos)
Response.Write title

%>

ghell
05-26-2006, 04:28 PM
Anything between the final / and any ? of a valid URI should be the file name. InStr and InStrRev are indeed very good ways of getting these (as long as you do things like check InStrRev()s result incase there was no ? in the name.

As for the title it would be easiest to simply save the title to a variable at the start, print it between the <title>..</title> tags and then have it in a variable still for if you want to use it later in the pace ;)