Jump to content

Get cookie of a website using pfsockopen();


Recommended Posts

Hello,

          I am trying to get a browser cookie(not a text file cookie) value of a remote website.

 

I've installed "Live HTTP headers" plugin for firefox, to trace all header information.

 

I am using pfsockopen(); for reading a remote website...

 

In header.. Two kinds of cookie values are there.

1. Set-Cookie:

&

2. Cookie:

 

I need Cookie value alone.

 

Any help would be really appreciated.

 

Link to comment
https://forums.phpfreaks.com/topic/52959-get-cookie-of-a-website-using-pfsockopen/
Share on other sites

stream_get_meta_data() + preg_match().

 

Here's something I was using recently:

<?php

$source = fopen($url['SignIn'],'r',false);
$contents = stream_get_contents($source);
$metadata = stream_get_meta_data($source);
fclose($source);

foreach ($metadata['wrapper_data'] as $value) {
if (preg_match('/Set-Cookie:\s*([^;,\s]*)=([^;,\s]*)/',$value,$match)) {
	echo "Cookie: $match[1]=$match[2]\r\n";
}
}
?>

 

I know that's looking for Set-Cookie, but you can change "Set-Cookie" to "^Cookie", and it should suit you fine.

https://www.google.com/accounts/LoginAuth?service=blogger

 

POST /accounts/LoginAuth?service=blogger HTTP/1.1

Host: www.google.com

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3

Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive: 300

Connection: keep-alive

Referer: https://www.google.com/accounts/Login?service=blogger&naui=8&skipvpage=true&alwf=true

Cookie: __utmz=173272373.1178976993.22.5.utmccn=(referral)|utmcsr=freakitude.com|utmcct=/2007/02/08/gmail-open-for-all/|utmcmd=referral; __utma=173272373.1617290277.1173753241.1179329475.1179808574.29; GoogleAccountsLocale_session=en; __utma=173272373.1617290277.1173753241.1179808574.1180096851.30; __utmz=173272373.1180062305.29.6.utmccn=(referral)|utmcsr=72.249.25.177|utmcct=/~albaybi/new/|utmcmd=referral; __utmb=173272373; __utmc=173272373; rememberme=true; PREF=ID=cc03b169fb414456:FF=4:TB=2:LD=en:NR=10:TM=1172328518:LM=1177868161:DV=AA:GM=1:S=8V2P_LBiblPSVDDz; adsenseReferralSourceId=; adsenseReferralClickId=; WebmastersLocale=en; adsenseReferralSubId=

Content-Type: application/x-www-form-urlencoded

Content-Length: 105

service=blogger&naui=8&skipvpage=true&alwf=true&Email=ednremeterqg&Passwd=e028bc&rmShown=1&signIn=Sign+in

HTTP/1.x 302 Moved Temporarily

Content-Type: text/html; charset=UTF-8

Set-Cookie: rememberme=false;Domain=.google.com;Path=/;Expires=Mon, 22-May-2017 12:45:49 GMT

Set-Cookie: SID=DQAAAGwAAACyNQrtVn4FOI0Da5WdH3aQfbGocYAnTlpKZTJPp5LyTAWLJRirIrbl4UnHt6kSLCLr6C5tvKBa4Fnxj9Vfis6YvH93sijKjAWZH0ZAiFU4iqNP6dpWWLMo1G2MdpNuIVfnYyg7L5iwzcnhCRduXi6_;Domain=.google.com;Path=/

Set-Cookie: LSID=EXPIRED;Domain=.google.com;Path=/;Expires=Mon, 01-Jan-1990 00:00:00 GMT

Set-Cookie: LSID=EXPIRED;Path=/;Expires=Mon, 01-Jan-1990 00:00:00 GMT

Set-Cookie: LSID=EXPIRED;Domain=www.google.com;Path=/accounts;Expires=Mon, 01-Jan-1990 00:00:00 GMT

Set-Cookie: LSID=DQAAAG8AAADj279ak7iMxiwz0DhWSiHUGRpN_Nmbr6eT1PJJAKoNmdPk7rkOIpo3z4Dvz4pHrWJmZO79Oa18V42X2JQeN-mAaCcdVVtDY7-IjpHrrMv39e2UdNipyI46pJC3HmnUpsBRD4_hv4rWZkquIl_E-5Rf;Path=/accounts;Secure

Location: https://www.google.com/accounts/CheckCookie?service=blogger&chtml=LoginDoneHtml&skipvpage=true&naui=8

Cache-Control: private

Transfer-Encoding: chunked

Content-Encoding: gzip

Date: Fri, 25 May 2007 12:45:49 GMT

Server: GFE/1.3

----------------------------------------------------------

https://www.google.com/accounts/CheckCookie?service=blogger&chtml=LoginDoneHtml&skipvpage=true&naui=8

 

GET /accounts/CheckCookie?service=blogger&chtml=LoginDoneHtml&skipvpage=true&naui=8 HTTP/1.1

Host: www.google.com

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3

Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive: 300

Connection: keep-alive

Referer: https://www.google.com/accounts/Login?service=blogger&naui=8&skipvpage=true&alwf=true

Cookie: __utmz=173272373.1178976993.22.5.utmccn=(referral)|utmcsr=freakitude.com|utmcct=/2007/02/08/gmail-open-for-all/|utmcmd=referral; __utma=173272373.1617290277.1173753241.1179329475.1179808574.29; GoogleAccountsLocale_session=en; __utma=173272373.1617290277.1173753241.1179808574.1180096851.30; __utmz=173272373.1180062305.29.6.utmccn=(referral)|utmcsr=72.249.25.177|utmcct=/~albaybi/new/|utmcmd=referral; __utmb=173272373; __utmc=173272373; LSID=DQAAAG8AAADj279ak7iMxiwz0DhWSiHUGRpN_Nmbr6eT1PJJAKoNmdPk7rkOIpo3z4Dvz4pHrWJmZO79Oa18V42X2JQeN-mAaCcdVVtDY7-IjpHrrMv39e2UdNipyI46pJC3HmnUpsBRD4_hv4rWZkquIl_E-5Rf; rememberme=false; PREF=ID=cc03b169fb414456:FF=4:TB=2:LD=en:NR=10:TM=1172328518:LM=1177868161:DV=AA:GM=1:S=8V2P_LBiblPSVDDz; adsenseReferralSourceId=; adsenseReferralClickId=; WebmastersLocale=en; adsenseReferralSubId=; SID=DQAAAGwAAACyNQrtVn4FOI0Da5WdH3aQfbGocYAnTlpKZTJPp5LyTAWLJRirIrbl4UnHt6kSLCLr6C5tvKBa4Fnxj9Vfis6YvH93sijKjAWZH0ZAiFU4iqNP6dpWWLMo1G2MdpNuIVfnYyg7L5iwzcnhCRduXi6_

 

HTTP/1.x 200 OK

Content-Type: text/html; charset=UTF-8

Cache-Control: no-cache

Pragma: no-cache

Transfer-Encoding: chunked

Content-Encoding: gzip

Date: Fri, 25 May 2007 12:45:50 GMT

Server: GFE/1.3

----------------------------------------------------------

You can see two "Cookie:" in first and second secions...

 

First Cookie: value is having low data...

 

Second section's "Cookie:" is having more data... But it is having first secion's "Set-Cookie:" value as a part...

 

I dont know how to get these values from those pages...

 

Thanks for the help with regex.

 

I got above Header informations ONLY FROM "live http headers" plugin for firefox.

 

My only one question is.. How can i get Header information like this from google.com page?

 

#1: I need to get these information from google.com/blablab

#2: Store it in a $PHP_Variable then do "String Findings" using Reg Ex

 

You friends helping for #2.. I need your support for #1 :)

You said you were using pfsockopen() for reading, right?  In the example I posted first, I used fopen(), but you can use stream_get_meta_data() with either of them.  That's how you'll get the header data.  It'll be in the 'wrapper_data' element of the array returned by the meta data function.  See the PHP man page for more details, if needed.

 

Some bare code to illustrate:

$sock = pfsockopen(/*...URL...*/);  // Whatever you've used to open

$metadata = stream_get_meta_data($sock);
foreach ($metadata['wrapper_data'] as $value) {
if (preg_match('/^Cookie:\s*(.*)$/is',$value,$match)) {
	echo "Cookie=$match[1]<br>\n";
}
}

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.