我有一个登录保护的后台网站,用 ASP classic 编写,在 Windows 上运行。登录状态存储在 session 变量中。我还有一个 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()
中,通过指定用 $ 填充的
必须包含 ASP session 的标识符,以便 ASP 可以识别用户并且响应会因用户而异。Cookie
header 对 sessionConnector.asp
发出 HTTP 请求_SERVER["HTTP_COOKIE"]
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
}
关于php - 如何从 PHP 访问 ASP 经典 session 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37052135/