Jump to content

fsockopen + showing cookies recieved


xScott

Recommended Posts

I use the snoopy php class to navigate to pages for a game i play. I know its possible to show what cookies have been set while navigating through the class but i have no idea on how to get started. Any ideas? All i need to be able to do is print what cookies have been set while i was browsing that page so even if its a link to another script that can do this i will be happy.

Scott
Link to comment
https://forums.phpfreaks.com/topic/26488-fsockopen-showing-cookies-recieved/
Share on other sites

  • 3 months later...
Since the cookies are sent as HTTP headers, you can retrieve an array containing all headers like this:

[code]print_r($snoopy->headers);[/code]

Gives you: 
[quote]Array ( [0] => HTTP/1.1 200 OK [1] => Connection: close [2] => Date: Thu, 22 Feb 2007 20:02:45 GMT [3] => Server: Microsoft-IIS/6.0 [4] => X-Powered-By: ASP.NET [5] => X-AspNet-Version: 1.1.4322 [6] => Set-Cookie: ASP.NET_SessionId=e3b3g145df3lll45lkjj4lab; path=/ [7] => Set-Cookie: OnCoreWeb=AutoLoadImages=-1&ImageViewer=2&DefaultNumberOfRows=15; expires=Fri, 22-Feb-2008 20:02:45 GMT; path=/ [8] => Set-Cookie: OnCoreWebAuthenticated=Authenticated=0&AgentKey=-1&CacheKey=54165805.375&LastUrl=http://198.185.140.50/oncoreweb/settings.aspx; expires=Fri, 23-Feb-2007 06:02:45 GMT; path=/ [9] => Cache-Control: private [10] => Content-Type: text/html; charset=utf-8 [11] => Content-Length: 38389 ) [/quote]

To make the output a bit more user friendly, use this:
[code]// Output the headers
$headers=$snoopy->headers;
while(list($key,$val) = each($headers)) {
    echo $key.' ---> ' . $val.'<br>';
}[/code]

Give you this:

[quote]0 ---> HTTP/1.1 200 OK
1 ---> Connection: close
2 ---> Date: Thu, 22 Feb 2007 20:02:45 GMT
3 ---> Server: Microsoft-IIS/6.0
4 ---> X-Powered-By: ASP.NET
5 ---> X-AspNet-Version: 1.1.4322
6 ---> Set-Cookie: ASP.NET_SessionId=e3b3g145df3lll45lkjj4lab; path=/
7 ---> Set-Cookie: OnCoreWeb=AutoLoadImages=-1&ImageViewer=2&DefaultNumberOfRows=15; expires=Fri, 22-Feb-2008 20:02:45 GMT; path=/
8 ---> Set-Cookie: OnCoreWebAuthenticated=Authenticated=0&AgentKey=-1&CacheKey=54165805.375&LastUrl=http://198.185.140.50/oncoreweb/settings.aspx; expires=Fri, 23-Feb-2007 06:02:45 GMT; path=/
9 ---> Cache-Control: private
10 ---> Content-Type: text/html; charset=utf-8
11 ---> Content-Length: 38389 [/quote]

And finally, if you want to grab just the session ID from these examples, do this:

[code]// grab the headers
$headers = $snoopy->headers;
while(list($key,$val) = each($headers)) {
echo $key.' ---> ' . $val.'<br>';
// If this iteration is the SessionID cookie, grab that value for later use
if (preg_match("/ASP.NET_SessionId=(.*?)$/i", $val, $ubid)) {
// send this cookie to the next session
$sessionID=$ubid[1];
}
}[/code]

Of course, these examples are from one specific server (ASP on IIS in this case) so the headers may look a bit different for each server and application.  If you need to re-use this session variable in successive Snoopy requests, it's pretty simple.  Before you send the request ($snoopy->fetch / $snoopy->fetchform / etc) simply pass a new cookie object to the Snoopy object like so...

[code]$snoopy->cookie["ASP.NET_SessionId"] = $sessionID;[/code]

... and your new $snoopy session will pass the cookie containing your SessionId along with the request.

Hope that helps!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.