如何从 PHP 访问 ASP 经典会话变量? [英] How to access ASP classic session variable from PHP?

查看:24
本文介绍了如何从 PHP 访问 ASP 经典会话变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a login protected back office website written in ASP classic running on Windows. Login status is stored in a session variable. I also have a PHP page that should be accessible only to logged in users. How do I check in PHP that the client is logged in to this website?

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

P.S. There may be multiple users accessing the page at the same time.

推荐答案

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

By assuming both PHP and ASP applications share the same domain name, here's a step by step guide.

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

1 - Create an asp file named sessionConnector.asp.

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

2 - In sessionConnector.asp, serialize the Session.Contents object into a format that PHP can deserialize, JSON for example. You can use JSON.asp from aspjson.

<%@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 函数.

3 - Create a PHP function named GetASPSessionState().

4 - 在 GetASPSessionState() 中,通过指定用 $ 填充的 Cookie 标头为 sessionConnector.asp 发出 HTTP 请求_SERVER["HTTP_COOKIE"] 必须包含 ASP Session 的标识符,这样 ASP 才能识别用户,响应因用户而异.

4 - In GetASPSessionState(), make an HTTP request for sessionConnector.asp by specifying the Cookie header filled with $_SERVER["HTTP_COOKIE"] which must contains identifier of the ASP Session, so ASP can identify the user and the response will vary by user.

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

5 - After fetching the response (string of JSON), deserialize by using json_decode and look for the ASP session variable.

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 访问 ASP 经典会话变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆