【发布时间】:2016-08-31 07:58:16
【问题描述】:
我有一个在 Windows 上运行的用 ASP 经典编写的受登录保护的后台网站。登录状态存储在会话变量中。我还有一个 PHP 页面,只有登录用户才能访问。如何在 PHP 中检查客户端是否已登录该网站?
附:可能有多个用户同时访问该页面。
【问题讨论】:
标签: php architecture asp-classic
我有一个在 Windows 上运行的用 ASP 经典编写的受登录保护的后台网站。登录状态存储在会话变量中。我还有一个 PHP 页面,只有登录用户才能访问。如何在 PHP 中检查客户端是否已登录该网站?
附:可能有多个用户同时访问该页面。
【问题讨论】:
标签: php architecture asp-classic
假设 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
}
【讨论】:
我的解决方案是自动提交一个可以双向工作的网络表单,无论是 PDF 到 ASP 还是 ASP 到 PDF。
在首页简单地添加一个 OnLoad 到 body 标签,像这样:
<body onload="document.xxx.submit()">
其中“xxx”是包含您要传递的隐藏字段的表单的 ID。例如:
<form id="xxx" action="example.asp" method="post">
这将在本地和跨域工作。
【讨论】: