3
votes

We have an ASP intranet web application that have been developed over years which is running on IIS6. Nowadays, we would like to add some new functionalities using PHP language instead. PHP is running fine on the same server. Sessions variables need to be shared between both ASP and 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)? 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. 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.

So,

-- ASP --

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

gives:

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

-- 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 :

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

in a loop, it gives me bunch list of key/values session cookies stored.

But on PHP side, I can't find a way to get these key/value list. May be it is the way to go and find more? A real existing implementation would help more but any answers are welcome.

2
Why do you want to share same session using 2 languages? Use token-based approach is suggested - Raptor
You can implement a custom PHP session handler. Then you need to figure out how to read/decode/encode/write ASP-classic sessions (from wherever they are stored) - David-SkyMesh
To clarify, you want the ASP side to write to PHP sessions, and vice versa? I should think that's quite a tricky problem. It might be easier to implement your own session handler on each side, as @David says, so you understand the format. - halfer
Doing a bit of googling, it doesn't look easy to do it by only modifying the PHP side. This MSDN article talks about shared sessions between ASP.Net and ASP classic: msdn.microsoft.com/en-us/library/aa479313.aspx It doesn't look like you can alter how ASP classic does sessions, only get in before and after it to copy that session data to/from somewhere else. If you can just add the COM object instantiation suggested to some common include in your ASP classic application that approach could still work. What do you want to use instead of a database? Say something like redis? - David-SkyMesh
I've faced this problem exchanging session variables between Classic ASP and ASP.net pages. The simplest way is to hit a page in the other language using a zero size iframe, and pass your session variables in a querystring - John

2 Answers

1
votes

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.

<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

<%
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

0
votes

You can do this by calling session.asp from PHP script.

PHP part:

$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:

<% @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
%>