经典ASP + Javascript - 试图避免会话

时间:2014-11-03 09:19:37

标签: javascript asp-classic session-variables

请耐心等待我的英语......

我开始研究其他人开发的经典ASP应用程序。有很多页面,组织得不好:当你来到现有网站时,你必须面对的典型情况。

我们现在遇到一个大问题:由于会话变量丢失导致大量注销。 所以我试着写一个脚本来避免会话。

幸运的是,应用程序的每个页面都有一个公共页面。 我的第一步将是从现在到未来的过渡:应用程序已经在生产中,所以首先我将保持会话变量不变,并将我的脚本添加到包含页面的顶部。

ASP脚本定义关键会话变量的名称,在request.form集合上检索它们的值,并在session.contents集合中添加值。 然后Javascript脚本创建包含页面加载值的隐藏输入,以便将它们发布到下一页(它将填充现有表单并在用户单击链接时创建新表单)。为确保没有冲突,每个名称都使用一个特定的单词:name =“specific_myName”

如果这是一个好方法,有人可以告诉我吗?如果是这样,脚本将不得不改进...... 谢谢

脚本下方:

<%
Class ConnexionState
    ' #### Connexion
    Private m_dict
    Private m_keys 
    Private m_Javascript
    Private m_specific
    Private m_deconnection
    Public Property Get GetDict()
        Set GetDict = m_dict
    End Property
    Public Sub Class_Initialize()
        ' Test if connexion is allowed
        TestConnexion
        ' Initilize datas + retrieve "session" variables
        m_specific = "Connexion_"
        Init
    End Sub
    Public Sub Class_Terminate()
        Set m_dict = Nothing
    End Sub
    Private Sub TestConnexion()
        ' One test may be : do we come from a page of the same application ?
        If Instr(1,Request.ServerVariables("HTTP_REFERER"),Request.ServerVariables("HTTP_HOST"),1) = 0 And Request.ServerVariables("HTTP_REFERER") <> "" Then
            EndConnexion
        End If
    End Sub
    Private Sub Validate(key_)
        ' Test if variables are well formatted : for example check if we get an integer if we expect an integer
        m_deconnection = false
        On Error Resume Next
        Select Case key_
            ' Expecting an integer
            'Case "key_integer_1","key_integer_2"...
            '    m_dict(key_) = CStr(CInt(m_dict(key_)))
                ' Expecting a string which lenght is 6
            Case "key_string_1"
                If m_dict(key_) <> "" And Len(m_dict(key_)) <> 6 Then m_deconnection = True
            Case Else
                ' Avoid values which length is too high 
                If Len(m_dict(key_)) > 25 Then m_deconnection = True
        End Select
        ' Avoid ' character
        If InStr(m_dict(key_),"'") > 0 Then m_deconnection = True
        ' If we got an error or one variable is not well formatted
        If Err.Number > 0 Or m_deconnection = True Then
            EndConnexion
        End If
        On Error Goto 0
    End Sub
    Private Sub EndConnexion()
        response.end
    End Sub
    Private Sub Init()
        Dim i
        Set m_dict = Server.CreateObject("Scripting.Dictionary")
        ' Names of the "session" variables
        ' m_keys = Array(    names_ )
        ' We do some stuff on each key
        For Each i In m_keys
            SetValue(i)
        Next
    End Sub
    Public Function ToString()
        ' Pass a string (will be used in the javascript below)
        Dim i
        ToString = ""
        For Each i In m_dict.keys
            If InStr(m_dict(i),"=") = 0 then
                ToString = ToString & i & "=" & m_dict(i) & "&"
            End if
        Next
        ToString = ToString & "specific=" & m_specific & "&"
        If ToString <> "" then
            ToString = Left(ToString,Len(ToString)-1)
        End If
    End Function
    Private Sub SetValue(key_)
        ' Retrieve values from request.form collection
        m_dict(key_) = request.form(m_specific & key_)
        ' If not in request.form, we try in session.contents
        If m_dict(key_) = "" Then
            m_dict(key_) = Session(key_)
        End If
        ' Test if value is well formatted
        Validate(key_)
        ' Update session
        session(key_) = m_dict(key_)
    End Sub
End Class

Dim Connexion
Set Connexion = New ConnexionState
%>
<script type="text/javascript">
    (function() {
        var Connexion = (function() {
            function init(args_) {
                // Translate "session" variables from a string passed in argument to object properties
                var params_ = args_[0],
                    p;
                for (var i in params_.split("&")) {
                    try {
                        this[params_.split("&")[i].split("=")[0]] = params_.split("&")[i].split("=")[1];    
                    }
                    catch (e) {
                        // do something
                    }        
                }
                // Load click event listener
                load.call(this);
                return;
            } 
            function load() {
                // What happens on page load
                var that = this;
                window.onload = function() {
                    document.onclick = function(event) {
                        event = event || window.event;
                        var t = event.target || event.srcElement,
                            p,
                            input;
                        // Click on a link -> we create a form and post values to the next page
                        if (t.tagName && t.tagName.toLowerCase() === "a" && (typeof t.onclick).toLowerCase() !== "function") {
                            send.call(that,t.href,t.target);
                            return false;    
                        } 
                        // Click on an input button -> we get the form containing th input and add hidden inputs containing connexion parameters inside it
                        if (t.tagName && t.tagName.toLowerCase() === "input") {
                            p = t;
                            while (p != null) {
                                if (p.tagName && p.tagName.toLowerCase() === "form") {
                                    appendInputs.call(that,p,true);
                                    return;
                                }
                                p = p.parentNode;
                            }
                            return;    
                        } 
                        return;
                    }        
                    // If there is any form inside th page we add hidden inputs containing connexion parameters inside it
                    var formsInDocument = document.getElementsByTagName("form");
                    for (var i=0;i<formsInDocument.length ;i++ ) {
                        appendInputs.call(that,formsInDocument[i],true);
                    }                    
                }
            }
            function send(action_,target_) {
                // Create a form and post connexion parameters to the next page
                var form = document.createElement("form"),
                    body;
                form.name = "Connexion";
                if (action_) { form.action = action_; }
                if (target_) { form.target = target_; }
                form.method = "post";
                // Add hidden inputs containing connexion parameters
                appendInputs.call(this,form);
                // If body tag does not exist we create it
                if (!document.getElementsByTagName("body")[0]) {
                    body = document.createElement("body");
                    document.documentElement.appendChild(body);
                    body.appendChild(form);
                } else {
                    document.getElementsByTagName("body")[0].appendChild(form);
                }
                form.submit();
                return false;
            }
            function appendInputs(form_,testExists_) {
                // Add hidden inputs containing connexion parameters inside a form
                var input;
                for (var p in this) {
                    if (this.hasOwnProperty(p) && (typeof this[p]).toLowerCase() != "function" && p.toLowerCase() != "specific") {
                        if ((testExists_ && !document.getElementsByName(p)[0]) || !testExists_) {
                            input = document.createElement("input");
                            input.type = "hidden";
                            input.name = this["specific"] + p;
                            input.value = this[p];
                            form_.appendChild(input);
                            console.log("    " + input.name + " - " + input.value);
                        }
                    }        
                }
                return;
            }
            return {
                init: init
            }
        })();
        Connexion.init(arguments);
    })("<%=Connexion.ToString()%>");
</script>

0 个答案:

没有答案