ASP   «Prev  Next»

Lesson 10Response.Redirect
ObjectiveRedirect a user to a different Web page.

ASP Response.Redirect

Response.Redirect allows you to easily redirect a user to a different URL.
This is often very useful when customizing your site for different types of users. As an example, you might be using frames in your ASP script, but a certain browser version may not support frames. In a case like this, you want to display a non-frames version of the same page to the user. This can be easily accomplished by using Response.Redirect and the Browser Capabilities component or third-party component that allows you to interpret the header information to determine the type and version of browser that is communicating with your Web site.
Here is a code segment that makes the decision and directs the user to one of two alternate Web pages:

 Set objBCap = Server.CreateObject("MSWC.BrowserType")
 If objBCap.frames then
      Response.Redirect("frames.asp")
 Else
      Response.Redirect("noframes.asp")
 End if

Response.Redirect statement must be inserted in your ASP script before the tag. Otherwise you will receive an error stating that the HTTP header information has already been written out.

Redirect

Response.Redirect strURL: Redirects the client's request to another URL.
Parameters
strURL: The Universal Resource Locator string for the new location to which you wish to redirect the client
Example
<%
' The following code determines whether the client has
' security clearance for a certain page. If not, it
' is redirected to another URL.
[...Code to determine user's clearance for the current page...]
If Not(strUserSecurity = "ADMIN" or "SUPERADMIN") Then
Response.Redirect "/security/noclearance.asp?usrid=09563"
End If
%>
Notes:
The strURL value you use when calling the Redirect method can be an exact URL with DNS or a virtual directory and filename. It also can be the name of a file that resides in the same folder as the requested page. If your script has written any content to the HTTP response body, that content is ignored by the script once the call to the Redirect method is executed.
Calling the Redirect method is conceptually the same as setting the Status property to "302 Object Moved" and sending the user to a new location using the Location HTTP header. Note that upon redirection, some older (HTTP 1.0) client browsers will mistakenly change POST requests to GET requests when the new URL is called. This is an important consideration when the client's POSTed information contains more data than the GET method can handle. It is assumed that new browsers supporting the HTTP 1.1 protocol have fixed this problem.
In the next lesson, you will use the Request and Response objects in your course project.