如何从PHP访问ASP经典会话变量?

Yur*_*rov 4 php architecture asp-classic

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

PS可能有多个用户同时访问该页面.

Kul*_*gin 7

通过假设PHP和ASP应用程序共享相同的域名,这是一个循序渐进的指南.

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

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

<%@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
%>
Run Code Online (Sandbox Code Playgroud)

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

4 - 在GetASPSessionState(),sessionConnector.asp通过指定Cookie填充的头部来发出HTTP请求,该头部$_SERVER["HTTP_COOKIE"]必须包含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
}
Run Code Online (Sandbox Code Playgroud)