【问题标题】:How to access ASP classic session variable from PHP?如何从 PHP 访问 ASP 经典会话变量?
【发布时间】:2016-08-31 07:58:16
【问题描述】:

我有一个在 Windows 上运行的用 ASP 经典编写的受登录保护的后台网站。登录状态存储在会话变量中。我还有一个 PHP 页面,只有登录用户才能访问。如何在 PHP 中检查客户端是否已登录该网站?

附:可能有多个用户同时访问该页面。

【问题讨论】:

    标签: php architecture asp-classic


    【解决方案1】:

    假设 PHP 和 ASP 应用程序共享相同的域名,这里有一个分步指南。

    1 - 创建一个名为 sessionConnector.asp 的 asp 文件。

    2 - 在sessionConnector.asp 中,将Session.Contents 对象序列化为PHP 可以反序列化的格式,例如JSON。您可以使用aspjson 中的JSON.asp

    <%@Language=VBScript CodePage=65001%>
    <!--#include file="JSON.asp"-->
    <%
    Set JSONObject = jsObject()
    
    For Each Key In Session.Contents
        If Not IsObject(Session.Contents(Key)) Then 'skip the objects cannot be serialized
            JSONObject(Key) = Session.Contents(Key)
        End If
    Next
    
    JSONObject.Flush
    %>
    

    3 - 创建一个名为 GetASPSessionState() 的 PHP 函数。

    4 - 在GetASPSessionState() 中,通过指定Cookie 标头填充$_SERVER["HTTP_COOKIE"]sessionConnector.asp 发出HTTP 请求,该标头必须包含ASP 会话的标识符,以便ASP 可以识别用户并且响应会有所不同按用户。

    5 - 获取响应(JSON 字符串)后,使用 json_decode 反序列化并查找 ASP 会话变量。

    function GetASPSessionState(){
        if(stripos($_SERVER["HTTP_COOKIE"], "ASPSESSIONID") === false){
            # since ASP sessions stored in memory 
            # don't make request to get ASP session state if the cookie does not contain ASPSESSIONID
            # otherwise IIS will create new redundant sessions for each of your checks so it wouldn't be a memory-friendly way
            # returning an empty array
            return array();
        } else {
            $options = array('http' => 
                array('method'=>"GET", 'header' => "Cookie: " . $_SERVER["HTTP_COOKIE"])
            );
            $cx = stream_context_create($options);
            $response = file_get_contents("http://mywebsite.com/sessionConnector.asp", false, $cx);
            return json_decode($response, JSON_FORCE_OBJECT);
        }
    }
    
    $aspSessionState = GetASPSessionState();
    if($aspSessionState["IsLoggedIn"] == true){
        //user previously logged in with the ASP
    }
    

    【讨论】:

    • 看起来不错。但是我需要一个解决方案来解决用户从 PHP 页面开始然后被重定向到我需要获取 PHP 会话数据的 ASP 页面的反向场景。
    • 这值得自己提出问题。解决方案只是反之亦然@WilliamK。如果您对您的场景提出新问题,请告诉我,我明天会尽力提供帮助。
    • 我被禁止提出常识性问题!但是,我确实通过向 ASP 页面自动提交 PHP 表单来找到解决方案。
    • @WilliamK 很抱歉听到这个消息。如果您当前的解决方案有效且足够,这比在平台之间共享会话要好。
    【解决方案2】:

    我的解决方案是自动提交一个可以双向工作的网络表单,无论是 PDF 到 ASP 还是 ASP 到 PDF。

    在首页简单地添加一个 OnLoad 到 body 标签,像这样:

    <body onload="document.xxx.submit()">
    

    其中“xxx”是包含您要传递的隐藏字段的表单的 ID。例如:

    <form id="xxx" action="example.asp" method="post">
    

    这将在本地和跨域工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 1970-01-01
      • 2014-10-14
      • 2013-01-31
      相关资源
      最近更新 更多