简体   繁体   English

在经典ASP和PHP之间共享会话而不是使用数据库

[英]share session between classic ASP and PHP over than using database

We have an ASP intranet web application that have been developed over years which is running on IIS6. 我们有一个多年来开发的ASP Intranet Web应用程序,它运行在IIS6上。 Nowadays, we would like to add some new functionalities using PHP language instead. 如今,我们希望使用PHP语言添加一些新功能。 PHP is running fine on the same server. PHP在同一台服务器上运行良好。 Sessions variables need to be shared between both ASP and PHP. 需要在ASP和PHP之间共享会话变量。

I am asking if there is other alternatives to share session between classic ASP and PHP instead of using database as gateway (too much resources consuming for us)? 我问是否有其他选择在经典ASP和PHP之间共享会话而不是使用数据库作为网关(为我们消耗太多资源)? Both side need to read/edit session variables. 双方都需要读取/编辑会话变量。

By tweaking a bit, I've noticed that a PHPSESSID and ASPSESSIONID is generated on PHP side every time a user is logged in the ASP web application. 稍微调整一下,我注意到每次用户登录ASP Web应用程序时都会在PHP端生成PHPSESSID和ASPSESSIONID。 These are also visible on the ASP side, which are stored inside the server Variable HTTP_COOKIE, so I think there may be a correlation between ASP session and PHP sessions variables at the heart of IIS. 这些也可以在ASP端看到,它们存储在服务器变量HTTP_COOKIE中,所以我认为在IIS的核心ASP会话和PHP会话变量之间可能存在关联。

So, 所以,

-- ASP -- - ASP -

<% Response.write ('HTTP_COOKIE') %> 

gives: 得到:

__utma=...; __utmz=...; computer%5Fid=AAA; lan=fre;ASPSESSIONIDXXXXXXXX=BBBBBBBBBBBBBBBBBBBBBBBB; user_login=cccc

-- PHP -- - PHP -

    echo '<pre>';
    var_dump($_COOKIE) ?>
    echo '</pre>';

gives: 得到:

Array
(
    [__utma] => ...
    [__utmz] => ...
    [computer_id] => AAA
    [lan] => fre
    [ASPSESSIONIDXXXXXXXX] => BBBBBBBBBBBBBBBBBBBBBBBB
    [user_login] => cccc
)

on ASP side, if I write : 在ASP方面,如果我写:

<% Request.Cookies(strCookie)(strKey) %>

in a loop, it gives me bunch list of key/values session cookies stored. 在循环中,它为我提供了存储的密钥/值会话cookie的一系列列表。

But on PHP side, I can't find a way to get these key/value list. 但是在PHP方面,我找不到获取这些键/值列表的方法。 May be it is the way to go and find more? 可能是它的方式去寻找更多? A real existing implementation would help more but any answers are welcome. 真正的现有实施将有所帮助,但欢迎任何答案。

I've never used session variables in PHP before, so here I'm assuming that you have already assigned $var1 and $var2 the values of the session variables you want to pass to your ASP file. 我以前从未在PHP中使用会话变量,所以在这里我假设您已经为$ var1和$ var2分配了要传递给ASP文件的会话变量的值。

<iframe height="0" width="0" scrolling="No" src="setsession.asp?var1=<?php echo $var1; ?>&var2=<?php echo $var2; ?>"></iframe>

Then your setsession.asp file would simply be 然后你的setsession.asp文件就是这样

<%
Session("var1") = Request.Querystring("var1")
Session("var2") = Request.Querystring("var2")
%>

Obviously you could do this the other way around, you just need to understand how to handle querystring and session variables in both languages 显然你可以反过来这样做,你只需要了解如何处理两种语言的查询字符串和会话变量

I have used this before for the very same purpose. 我之前使用过这个目的是为了同一个目的。 Try this link http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=9028&lngWId=4 试试这个链接http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=9028&lngWId=4

You can do this by calling session.asp from PHP script. 您可以通过从PHP脚本调用session.asp来完成此操作。

PHP part: PHP部分:

$link = "$http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url=explode("/",$link);
array_pop($url);
$urlp=implode("/",$url)."/";
//here we get the url path

$ck=array_keys($_COOKIE);
for ($i=0;$i<count($ck);++$i) {
  if (strpos($ck[$i],"ASPSESSIONID")===0) {
    $cook .=$ck[$i]."=".$_COOKIE["$ck[$i]"].";"."<br>";
  }//we need to pass ASPSESSIONID cookies to ASP script
}
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Cookie: ".$cook
  )
);

//function for reading/writing ASP session values
function aspsession() {
  global $urlp,$opts;
  $n=urlencode(func_get_arg(0));
  if (func_num_args()==2) {
    $v=urlencode(func_get_arg(1));
    return file_get_contents("$urlp../session.asp?n=$n&v=$v",NULL,stream_context_create($opts));
  } else {
    return file_get_contents("$urlp../session.asp?w=$n",NULL,stream_context_create($opts));
  }//put the right relative URL for session.asp
  //make sure it's in the same application as your other ASP scripts,
  //so it has the same session
}

//to test if it works
aspsession("a","test");
echo aspsession("a");

...and the session.asp : ...和session.asp

<% @Language = "VBScript" 
     ENABLESESSIONSTATE = True%>
<% Response.ContentType="text/plain" %>
<% Response.Expires=-1 %>
<%
n=Request.QueryString("n")
v=Request.QueryString("v")
if n<>"" then
  session(n)=v
else
  Response.Clear
  Response.Write session(Request.QueryString("w"))
end if
%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM