简体   繁体   中英

PHP code for retrieving ASP.Net Session ID

I am looking for a php function to get session id from a asp.net page. I used PHPSESSID to get php session id and it's giving the correct result as shown below:

$strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/'; 
session_write_close();
echo $strCookie;
result: PHPSESSID=h7h1si1gqge0opqe7hedk46aa7; path=/

Now I want to get ASP.Net Session ID, and I don't know the php command to be frank. I tried ASPSESSIONID, ASPSESSID, SessionID etc unsuccessfully. Each time the result is blank as shown below, whereas I can see the ASP.Net_SessionId using firebug. Plese somebody help me to get the ASP.net session id. I tried searching through the forum but didn't get anything.

$cookie="ASP.NET_SessionId=". $_COOKIE["ASPSESSIONID"] ;
session_write_close();
echo $cookie;
result: ASP.NET_SessionId=

Tried this code as well, but not getting anything:

if(stripos($data,"ASP.NET_SessionId"))
{
$retData = explode("ASP.NET_SessionId=", $data);
$sessionTempValue = explode(";",$retData[1]);

 //setup the cookie with the recd data here..

echo $sendCookie1 = "ASP.NET_SessionId=".$sessionTempValue[0].'; path=/';
setcookie("ASP.NET_SessionId",$sessionTempValue[0]);
}

EDIT: Thanks TVK, Icarus and Mike Brant for guiding me into right direction. I modified my script little bit as shown below and now ASP.Net SessionId is being saved in the file cookiedk.txt, though, still it's not appearing in $_COOKIE.

define('COOKIE_FILE', 'cookiedk.txt');
curl_setopt($ch, CURLOPT_COOKIESESSION, 1); 
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);

Below is cookiedk.txt content:

# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

#HttpOnly_xyz.com   FALSE   /   FALSE   0   ASP.NET_SessionId   jq0jeb45lldjlo45wreqsl45

One more help, saving the session ID in a file may be problematic where multiple users will be interacting with the system at the same time. Is there a way to get it through the script and set it there itself instead of saving in file?

If the ASP session ID is in fact available, then it should be in the $_COOKIE variable. To see all the contents of $_COOKIE , you can use print_r($_COOKIE); . If the session ID is in there, you can now see it and select the correct array key.

Keep in mind though, that cookies are only being sent if the cookie's domain restriction is being satisfied.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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