|
Modify page content based on referring page.
Your average dynamic website these days contains numerous forms to fill in, large amounts of Server-Side processing and database interaction. With this, comes numerous "Thanks for filling out xxxx form", etc etc The obvious way to handle these is simple, create lots of different "Thankyou" pages for each and every form or confirmation on your website. But what if you want to make this a little more dynamic? In this tutorial we're going to look at writing content dependent on the referring page (The page the user was on previously). The referring page is held in your browsers history and is accessible in ASP using Request.ServerVariables("HTTP_REFERRER"). Here is an example of a referring URL, which is from a google search result: http://www.google.com/search?hl=en&lr=&ie=UTF-8&oe=UTF-8&q=css+div&spell=1 If we store the above URL as a string, we can split it up and then modify the content of our page accordingly. We could confirm the terms they searched for, or simply thank them for clicking our website in a list of search results! Lets get started with identifying the exact referring pagename (E.g. mypage.asp) from the referring URL. We'll accomplish this using the following code. <%@LANGUAGE="VBSCRIPT"%> The above code produces the following output
Our original string: http://localhost/mysite/mypage.asp?name=value&this=that has been split at each forward slash "/" and each 'split' part has been put into an array, which we have then looped through and displayed to the screen. If this was the genuine referring page, we could now modify the content of the page specific to people who came from "mypage.asp". We could accomplish this by creating a new variable which would hold the upper bound integer for the array (The last string in the array) The script would now look like this (Additions are indicated in bold type): <%@LANGUAGE="VBSCRIPT"%>
<%Option Explicit%>
<html>
<head>
</head>
<body>
<%
Dim strURL, arrURL, x, uPage
'strURL = Request.ServerVariables("HTTP_REFERRER")
strURL = "http://localhost/mysite/mypage.asp?name=value&this=that"
arrURL = Split(strURL, "/")
uPage = UBound(arrURL)
For x = 1 to UBound(arrURL) This will generate the following output
So now, we have the exact pagename, along with the parameters that were passed. What we want to do from here is split the pagename entirely and display the parameters individually. In order to do this we need to write a little bit more code in order to split the string up further. We'll also include the URL parts that we wrote to the screen in the first part of this tutorial. From here, things start to get a little more complex! Here is the final code which shows us information on our simulated referring URL <%@LANGUAGE="VBSCRIPT"%>
<%Option Explicit%>
<html>
<head>
</head>
<body>
<h1>Referring URL splitting</h1>
<%
Dim strURL, arrURL, x, uPage, qMarkPos, arrURLAmp
'strURL = Request.ServerVariables("HTTP_REFERRER")
strURL = "http://localhost/mysite/mypage.asp?name=value&this=that"
arrURL = Split(strURL, "/")
uPage = UBound(arrURL)
'-- LOOP AND SHOW URL PARTS. Response.Write "<strong>Full URL</strong>: " & strURL & "<br /><br />" Response.Write "<strong>URL Component parts</strong><br />" '-- IF WE HAVE AN AMPERSAND IN THE URL, DISPLAY EACH PARAMETER PASSED. If InStr(1, strURL, "&", 1) Then
We are now getting quite complex. Take a moment to look over the code above and identify what each part of it doing! The above code, produces the output shown below. Now that we have access to everything we need, we can easily modify the content of a page to accomodate the referring page. We can thank somebody for filling out a specific form, confirm or even store parameters searched for in a database and many more things. Now, we'll setup a Select Case statement which will display different messages based on the page. <%@LANGUAGE="VBSCRIPT"%>
<%Option Explicit%>
<html>
<head>
</head>
<body>
<h1>Referring URL splitting</h1>
<%
Dim strURL, arrURL, x, uPage, qMarkPos, arrURLAmp
'strURL = Request.ServerVariables("HTTP_REFERRER")
strURL = "http://localhost/mysite/mypage.asp?name=value&this=that"
arrURL = Split(strURL, "/") uPage = UBound(arrURL) '-- LOOP AND SHOW MESSAGE. For x = 1 to UBound(arrURL) If x = uPage Then qMarkPos = InStr(1, arrURL(uPage), "?", 1) If qMarkPos Then The output of the code above is shown below. This is based on the "myotherpage.asp"
URL being the selected URL. Instead of showing the split URL to the screen we are now generating some sort of dynamic message dependant on the referring URL. This could be expanded upon to load a wide range of text or images. You could also integrate the parameters into the equation by using a similar Select Case statement to generate content based on the parameters. That concludes this string-bashing tutorial. Top | Discuss this tutorial in our forum! | Home © designplace.org 2003 Remember, if you get stuck or need to ask any questions, register with the forums and tell us about it! |
|