加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 17:50:17 所属栏目:PHP教程 来源:网络整理
导读:我有一个在 Windows上运行的ASP经典编写的登录保护后台网站.登录状态存储在会话变量中.我还有一个 PHP页面,只有登录用户才能访问.如何在PHP中检查客户端是否已登录此网站? 附:可能有多个用户同时访问该页面. 解决方法 通过假设PHP和ASP应用程序共享相同的
我有一个在 Windows上运行的ASP经典编写的登录保护后台网站.登录状态存储在会话变量中.我还有一个 PHP页面,只有登录用户才能访问.如何在PHP中检查客户端是否已登录此网站?

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

解决方法

通过假设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()中,通过指定填充了$_SERVER [“HTTP_COOKIE”]的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
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读