用于显示页面的会话变量包括

Session variable for displaying page includes

我有一个 .asp 页面,它使用会话变量来显示特定于管理员和非管理员的项目。这在我的网站上无处不在,但以下情况除外:

我有一个表单页面,它只允许 SESSION("adminrole") = "admin" 使用表单和记录的每个部分的各个字段更新记录。

如果 SESSION("adminrole") = "nonadmin" 则页面上包含不同的 VB 脚本

这是我在 ASP 页面顶部的示例代码

<% 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 %>

我已经验证 SESSION("adminrole") 是当用户登录我的位置 Response.write 时声明的内容,以便我可以直观地看到用户的会话角色名称。

问题是无论谁登录,包含的包含页面都是针对非管理员角色的 - 并且绝不是第一个包含文件

您遇到的问题是 IIS 中的处理顺序。服务器端包括在处理 VBScript 之前执行。使用不同的方法在您的页面中包含首选脚本 -

参见此 link 中的示例: http://www.4guysfromrolla.com/webtech/022504-1.shtml

示例代码:

<%
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