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

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

问题描述

我有一个受登录保护的后台办公室网站,该网站使用Windows上运行的ASP Classic编写.登录状态存储在会话变量中.我还有一个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.可能有多个用户同时访问该页面.

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()中,通过指定填充有$_SERVER["HTTP_COOKIE"]Cookie标头来向sessionConnector.asp发出HTTP请求,该标头必须包含ASP会话的标识符,以便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天全站免登陆