Search code examples
asp-classic

Session variable for displaying page includes


I have a .asp page that uses sessions variables to display items specific to an administrator versus non administrator. This works everywhere throughout my site with the exception of the following:

I have a form page that is only suppose to allow SESSION("adminrole") = "admin" to update records using a form and the various fields for each part of a record.

If the SESSION("adminrole") = "nonadmin" then a different VB script is included on the page

Here is my example code at the top of an ASP page

<% RESPONSE.WRITE SESSION("adminrole") %>
<% IF SESSION("adminrole") = "admin" THEN %>
<!--#include file="vb/member_details.vb" -->
<% ELSEIF SESSION("adminrole") = "nonadmin" THEN%>
<!--#include file="vb/member_details_NOUPDATE.vb" -->
<% END IF %>

I have verified that the SESSION("adminrole") is what has been declared when a user logs in my place the Response.write so that I can visually see the session role name for the user.

The issue is that no matter who logs in, the include page that gets included is for the NON Admin role - AND NEVER the first Include file


Solution

  • The issue you have here is the order of processing in IIS. Server-side includes get executed before the VBScript is processed. Use a different method to include the preferred script in your page -

    See the example in this link: https://web.archive.org/web/20211020134119/https://www.4guysfromrolla.com/webtech/022504-1.shtml

    Example Code:

    <%
    Dim strInclude
    Dim I_want_to_include_file_1
    I_want_to_include_file_1 = True
    
    If I_want_to_include_file_1 = True Then
      strInclude = getMappedFileAsString("include1.asp")
    Else
      strInclude = getMappedFileAsString("include2.asp")
    End If
    
    Execute strInclude
    %>
    

    Because this method does not use the built-in IIS include, the code will be run when the page is run, but only one file will be included. The code for the getMappedFileAsString(filepath) function is shown below. Essentially it grabs the complete contents of the specified filepath, returning the file's contents as a string.

    Function getMappedFileAsString(byVal strFilename)
      Const ForReading = 1
    
      Dim fso
      Set fso = Server.CreateObject("Scripting.FilesystemObject")
    
      Dim ts
      Set ts = fso.OpenTextFile(Server.MapPath(strFilename), ForReading)
    
      getMappedFileAsString = ts.ReadAll
      ts.close
    
      Set ts = nothing
      Set fso = Nothing
    End Function