如何从 PHP 访问 ASP 经典会话变量?
How to access ASP classic session variable from PHP?
我在 Windows 上有一个用 ASP 经典 运行 编写的受登录保护的后台网站。登录状态存储在会话变量中。我还有一个 PHP 页面,应该只有登录用户才能访问。如何检查 PHP 客户端是否已登录此网站?
P.S。可能有多个用户同时访问该页面。
假设 PHP 和 ASP 应用程序共享相同的域名,这里有一个分步指南。
1 - 创建一个名为 sessionConnector.asp
的 asp 文件。
2 - 在 sessionConnector.asp
中,将 Session.Contents
object 序列化为 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
header 为 sessionConnector.asp
发出 HTTP 请求,其中必须包含ASP Session,因此 ASP 可以识别用户,响应会因用户而异。
5 - 获取响应(JSON 的字符串)后,使用 json_decode 反序列化并查找 ASP session 变量。
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">
这将在本地和跨域工作。
我在 Windows 上有一个用 ASP 经典 运行 编写的受登录保护的后台网站。登录状态存储在会话变量中。我还有一个 PHP 页面,应该只有登录用户才能访问。如何检查 PHP 客户端是否已登录此网站?
P.S。可能有多个用户同时访问该页面。
假设 PHP 和 ASP 应用程序共享相同的域名,这里有一个分步指南。
1 - 创建一个名为 sessionConnector.asp
的 asp 文件。
2 - 在 sessionConnector.asp
中,将 Session.Contents
object 序列化为 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
header 为 sessionConnector.asp
发出 HTTP 请求,其中必须包含ASP Session,因此 ASP 可以识别用户,响应会因用户而异。
5 - 获取响应(JSON 的字符串)后,使用 json_decode 反序列化并查找 ASP session 变量。
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">
这将在本地和跨域工作。