note 61957 added to function.fsockopen
| From: | ittasks at gmail dot com | Date: | Thu, 16 Feb 2006 00:02:53 +0000 |
| Subject: | note 61957 added to function.fsockopen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-103819@lists.php.net to get a copy of this message | ||
login to the site prior to downloading page:
In some wierd situations site security is based on
ASPSESSION ID and where could be a
login asp script in one place, and the actual page with
information in another place.
for such cases you have to submit ( POST ) you login and
password first, when grab ASP session (and also some
cookies from response, and when use that ASP SESSION in
second request to the actual page: (i took some parts of
codes from other ppl)
<?php
//submit login form: (url, post data, extra headers (optional))
//do not put http into URL, just domain name
$mycookies = GetCookies("www.yourdomain.com/login.login.asp",
"password=12345&username=your_username&submit=LOGIN&set=Y","");
//some extra params if you need them
// echo "Cookies:<br><pre>\n".$mycookies."\n</pre>";
//$body =PostPage("www.yourdomain.com/coolpage.asp",
//"action=zzz",$mycookies);
//echo "<br>Body:<br>\n".$body."\n";
//im using get page - so it goes like this:
$opts = array('http'=>array('method'=>"GET",
'header'=>"Accept-language: en\r\nCookie: ".$mycookies."\r\n" ));
$context = stream_context_create($opts);
$fp = fopen('http://www.yourdomain.com/coolpage.asp?p1=1&p2=23',
'r', false, $context);
fpassthru($fp);
$html = fread($fp, 1000000);
fclose($fp);
echo $html;
function PostPage($host,$query,$others=''){
$path=explode('/',$host);
$host=$path[0];
unset($path[0]);
$path='/'.(implode('/',$path));
$post="POST $path HTTP/1.1\r\nHost: $host\r\n";
$post.="Content-type: application/x-www-form-";
$post.="urlencoded\r\n${others}";
$post.="User-Agent: Mozilla 4.0\r\nContent-length: ";
$post.=strlen($query)."\r\nConnection: close\r\n\r\n$query";
$h=fsockopen($host,80);
fwrite($h,$post);
for($a=0,$r='';!$a;){
$b=fread($h,8192);
$r.=$b;
$a=(($b=='')?1:0);
}
fclose($h);
return $r;
}
function GetCookies($host,$query,$others=''){
$path=explode('/',$host);
$host=$path[0];
unset($path[0]);
$crlf = "\r\n";
$path='/'.(implode('/',$path));
$post="POST $path HTTP/1.1\r\nHost: $host\r\n";
$post.="Content-type: application/x-www-form-urlencoded\r\n${others}";
$post.="User-Agent: Mozilla 4.0\r\nContent-length: ";
$post.=strlen($query)."\r\nConnection: close\r\n\r\n$query";
$h=fsockopen($host,80);
fwrite($h,$post);
$r="";
for($a=0;!$a;){
$b=fread($h,512);
echo $b;
$r.=$b;
$gotSession=strpos($r,"ASPSESSION");
if($gotSession)
if(strpos($r, $crlf . $crlf,$gotSession)>0) break;
$a=(($b=='')?1:0);
}
fclose($h);
$arr = split("Set-Cookie:",$r);
$AllCookies="";$count=1;
while ($count < count($arr)) {
$AllCookies.=substr($arr[$count].";",
0,strpos($arr[$count].";",";")+1);
$count++;}
return $AllCookies;
}
?>
It's not optimized , but i hope someone might find it usefull.
Best Regards
DRY_GIN
----
Server IP: 216.235.15.211
Probable Submitter: 24.42.38.210
----
Manual Page -- http://www.php.net/manual/en/function.fsockopen.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+61957
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61957&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61957&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61957&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61957&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61957&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61957&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61957&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+61957&report=yes
Search -- http://master.php.net/manage/user-notes.php