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"%>
<%Option Explicit%>
<html>
<head>
</head>
<body>
<%
Dim strURL, arrURL, x 'Uncomment to use referring page
'strURL = Request.ServerVariables("HTTP_REFERRER")

'Simulated example
strURL = "http://localhost/mysite/mypage.asp?name=value&this=that"
'Split example into array based on / char
arrURL = Split(strURL, "/")

' Loop through array and show URL parts
For x = 1 to UBound(arrURL) If Trim(arrURL(x)) > "" Then Response.Write arrURL(x) & "<br />" End If Next %> </body> </html>

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)

'--commented out 'If Trim(arrURL(x)) > "" Then 'Response.Write arrURL(x) & "<br />" 'End If
If x = uPage Then Response.Write "Page: " & arrURL(x) End If
Next %> </body> </html>

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 />"

'-- Show URL parts For x = 1 to UBound(arrURL) If Trim(arrURL(x)) > "" Then Response.Write arrURL(x) & "<br />" End If
'-- If we are at the upper bound, display the exact page name..
'-- ..and split off parameters if there are any. If x = uPage Then qMarkPos = InStr(1, arrURL(uPage), "?", 1) 'Question mark position (params) If qMarkPos Then Response.Write "<p><strong>Page</strong><br />" _
& Left(arrURL(uPage), qMarkPos - 1) & "</p>" Else ' No parameters in URL Response.Write "<p><strong>Page</strong><br />" & arrURL(uPage) & "</p>" End If End If Next
'-- IF WE HAVE AN AMPERSAND IN THE URL, DISPLAY EACH PARAMETER PASSED.
If InStr(1, strURL, "&", 1) Then
x = 0 '-- Set counter back to zero qMarkPos = InStr(1, strURL, "?", 1) '-- Question Mark position

'-- Array split at the &, Starting from the question mark position + 1 arrURLAmp = split(Mid(strURL, qMarkPos + 1), "&")
Response.Write "<p><strong>Parameters</strong><br />"

'-- Loop and show our parameters! For x = 0 to UBound(arrURLAmp) If Trim(arrURLAmp(x)) > "" Then Response.Write arrURLAmp(x) & "<br />" End If Next
Response.Write "</p>"
End If %> </body> </html>

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
Select Case Left(arrURL(uPage), qMarkPos - 1) ' Params Case "mypage.asp" Response.Write "Thankyou for visiting my page! I hope you liked my page" Case "myotherpage.asp" Response.Write "You preferred my other page huh?" Case "formfill.asp" Response.Write "Thankyou for filling out my form" Case Else Response.Write "Generic message!" End Select
Else ' No params
Select Case arrURL(uPage) Case "mypage.asp" Response.Write "Thankyou for visiting my page! I hope you liked my page" Case "myotherpage.asp" Response.Write "You preferred my other page huh?" Case "formfill.asp" Response.Write "Thankyou for filling out my form" Case Else Response.Write "Generic message!" End Select End If End If Next %> </body> </html>

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!

TemplateMonster - Affordable & Professional website templates!
Partners Partners