PDA

View Full Version : جلوگیری از خواندن محتویات وبسایت (حل شد)



ingenuous67
شنبه 17 خرداد 1393, 08:38 صبح
سلام دوستان خسته نباشید.
مشکلی که من دارم با دستور file_get_content هست.
تقریبا درصد بالایی از سایت ها رو که با این دستور فچ میکنم مشکلی نداره.
اما چند تا سایت مثل eforosh.com و avval.ir هستند که فکر کنم اجازه خوندن اطلاعاتشون رو نمیدن.
در واقع توی هاستشون تنظیماتی اعمال کردن که نمیشه محتویات سایت رو با دستور مذکور خوند.
میخوام بپرسم کسی روشی سراغ نداره که بتونم محتویات این وبسایت ها رو هم بخونم.؟
در ضمن من curl رو هم تست کردم جواب نداد.
یه نفر پیشنهاد داد از هاست خارجی استفاده کنم که سرورش توی ایران نباشه چون دستور file_get_content توی برخی سرورهای ایران فیلتر شده. اما میخوام بدونم بدون تعویض هاست میشه این کار رو انجام داد یا نه؟

eshpilen
شنبه 17 خرداد 1393, 08:56 صبح
هرچی که میاد توی مرورگر شما، پس میشه گرفتش!
فقط این هست که این کار در بعضی موارد پیچیده تره و کار بیشتری میبره.
بعضی وقتا این پیچیدگی بصورت غیرعمدی بخاطر روش و فناوری استفاده شده توسط برنامه نویس اون سایت یا پارامترهای فنی دیگر ایجاد شده، و بعضی وقتا هم عمدا سعی کردن از این دسترسی های خارج از مرورگر جلوگیری کنن.
مثلا یک برنامه ای ممکنه user agent و referrer و این حرفا رو چک کنه، یا حتی پارامترهای دیگری رو، تا مطمئن بشه که درخواست از یک مرورگر اومده. ولی موضوع اینه که همهء این پارامترها رو هم میشه در سمت کلاینت جعل کرد (به راحتی). بهرحال درنظر بگیرید که مرورگر هم یک برنامه است مثل بقیهء برنامه ها، منجمله برنامهء شما، و سرورها در نهایت راهی ندارن که بین دوتا برنامه یکی رو تشخیص بدن (اگر یک برنامه سعی کنه رفتار دیگری رو تقلید کنه). البته در مواردی که مثلا از کپچا استفاده میشه، خودکار کردن این عملیات دشوار یا غیرممکن میشه، چون کامپیوترها نمیتونن به تنهایی کپچا رو بخونن یا حداقل نمیتونن این کار رو به سادگی و با الگوریتمی که هرکسی بتونه بنویسه انجام بدن.

طبیعتا برای موارد پیچیده و اوقاتی که میخواید برنامه خودش رو جای مرورگر جا بزنه، باید از ابزارهایی مثل curl استفاده کنید.
بهرحال قبلش باید ساختار سایت و کدهای سمت کلاینت و سرور برنامهء هدف رو مورد بررسی قرار بدید تا بفهمید چطور کار میکنه و از چه پارامترها یا احتمالا هوشمند بازی های پشت پرده ای استفاده میکنه، که بعد شما بخواید گولش بزنید! طبیعتا این کار میتونه در بعضی موارد بیش از حد پیچیده و دشوار باشه و صرف نکنه از دید طرف یا اینکه سواد و توانایی مبتدی ها بهش نرسه. ولی اونایی که وارد باشن بالاخره چپش میکنن!!
یک ابزاری که میتونید در این راه استفاده کنید، ابزارها و امکاناتی هست که مرورگرهایی مثل فایرفاکس دارن. مثلا در فایرفاکس میشه به راحتی درخواستهای ایجکس و پارامترهایی رو که همراهشون به سرور ارسال میشن، و همچنین پاسخ های دریافت شده رو، مشاهده کرد.

البته اینم درنظر داشته باشید که این قبیل کارهایی که اکثرا میخوان بکنن و از سایتهای دیگه سرقت اطلاعات و کپی برداری و سوء استفاده بکنن به این شکل، کارهای چیپی است و از نظر اخلاقی و همچنین قانونی مشکل دارن یا حداقل مشکوک هستن. در بعضی کشورها میتونن به این خاطر از شما شکایت و در دادگاه محکومتون کنن.
سعی کنید دنبال چیزهای مفیدتر و غیرمبتذل باشید.

MMSHFE
شنبه 17 خرداد 1393, 11:16 صبح
ضمن تأیید صحبتهای دوست خوبمون eshpilen این نکته رو هم اضافه کنم که بعضی از سایتها هم بعد از Load شدن، محتوا رو با JS تولید یا با AJAX درخواست میدن که بیاد و اینجور وقتها شما باید سورس اصلی داده ها رو پیدا کنید و از اون، اطلاعات رو بخونید. برای مثال سایت بورس به این شکل کار میکنه.

aalmair
شنبه 17 خرداد 1393, 12:00 عصر
<?php

if(!function_exists('http_chunked_decode')) {

function http_chunked_decode($chunk) {
$pos = 0;
$len = strlen($chunk);
$dechunk = null;

while(($pos < $len)
&& ($chunkLenHex = substr($chunk,$pos, ($newlineAt = strpos($chunk,"\n",$pos+1))-$pos)))
{
if (! is_hex($chunkLenHex)) {
trigger_error('Value is not properly chunk encoded', E_USER_WARNING);
return $chunk;
}

$pos = $newlineAt + 1;
$chunkLen = hexdec(rtrim($chunkLenHex,"\r\n"));
$dechunk .= substr($chunk, $pos, $chunkLen);
$pos = strpos($chunk, "\n", $pos + $chunkLen) + 1;
}
return $dechunk;
}
}



function is_hex($hex) {
// regex is for weenies
$hex = strtolower(trim(ltrim($hex,"0")));
if (empty($hex)) { $hex = 0; };
$dec = hexdec($hex);
return ($hex == dechex($dec));
}



class http{

private $gived_headers=array();
private $gived_headers_str='';

function __construct(){

}
private function gived_header_init($arr_header=array()){


$res=array();
$size=count($arr_header) ;

if($size)
{
$res[]=$arr_header[0];
}

for($i=0;$i<$size;$i++)
{
if(is_string($arr_header[$i]))
{


$pos=strpos($arr_header[$i],':');
if($pos!==false)
{
($key=strtolower(substr($arr_header[$i],0,$pos))) ;
($value=strtolower(substr($arr_header[$i],$pos+1))) ;
$t_i=0;
while( $t_i < strlen($value) && ord($value[$t_i]) <= 32)
{
$t_i++;
}
$value=substr($value,$t_i);
$values=(explode(';',$value));

for($k=0;$k<count($values);$k++)
{
if(is_string($values[$k]))
{
$explod_by_comma=explode(',',$values[$k]);
if(count($explod_by_comma)>1)
{
for($j=0;$j<count($explod_by_comma);$j++)
{
$pos_equal_comma=strpos($explod_by_comma[$j],'=');
if($pos_equal_comma!==false)
{
($key2=(trim(substr($explod_by_comma[$j],0,$pos_equal_comma))));
($value2=(trim(substr($explod_by_comma[$j],$pos_equal_comma+1))));
@$res[$key]['value'][$key2]=$value2;


}else
{
@$res[$key]['value'][$key][]=trim($explod_by_comma[$j]);
}
}

}else

{
$pos_equal=strpos($values[$k],'=');
if($pos_equal!==false)
{
($key2=(trim(substr($values[$k],0,$pos_equal))));
($value2=(trim(substr($values[$k],$pos_equal+1))));
@$res[$key]['value'][$key2]=$value2;

}else
{
@$res[$key]['value'][$key][]=$values[$k] ;
}



}



}

}

$res[$key]['values']=$value;

}else
{
$res[]=$arr_header[$i];
}
}
}

return $res;

}
public function get_gived_headers(){

return $this->gived_headers;
}
public function get_gived_headers_str(){

return $this->gived_headers_str;
}
public function http_request_(
$verb = 'GET', /* HTTP Request Method (GET and POST supported) */
$ip, /* Target IP/Hostname */
$port = 80, /* Target TCP port */
$uri = '/', /* Target URI */
$getdata = array(), /* HTTP GET Data ie. array('var1' => 'val1', 'var2' => 'val2') */
$postdata = array(), /* HTTP POST Data ie. array('var1' => 'val1', 'var2' => 'val2') */
$cookie = array(), /* HTTP Cookie Data ie. array('var1' => 'val1', 'var2' => 'val2') */
$custom_headers = array(), /* Custom HTTP headers ie. array('Referer: http://localhost/ */
$timeout = 1, /* Socket timeout in seconds */
$req_hdr = false, /* Include HTTP request headers */
$res_hdr = false, /* Include HTTP response headers */
&$error_str=''
)
{


$this->gived_headers_str='';
$this->gived_headers= array();
$error_str='';


$max_header_size=10*1024 ; // 10 KB
$max_document_size=2*1024*1024 ; // 10 MB


$content_lenght=-1;

$http_ver='HTTP/1.1';

$ret = '';
$verb = strtoupper($verb);
$cookie_str = '';
$getdata_str = count($getdata) ? '?' : '';
$postdata_str = '';
$give_header_array=array();
$user_agent='aalmabot 1.0 (http://aalma.ir ; http://aalma.ir/bot/ ; arash_shikhlu@yahoo.com ; info@aalma.ir ;) ';

foreach ($getdata as $k => $v)
$getdata_str .= urlencode($k) .'='. urlencode($v) . '&';

foreach ($postdata as $k => $v)
$postdata_str .= urlencode($k) .'='. urlencode($v) .'&';

foreach ($cookie as $k => $v)
$cookie_str .= urlencode($k) .'='. urlencode($v) .'; ';

$crlf = "\r\n";
$req = $verb .' '. $uri . $getdata_str .' '.$http_ver . $crlf;
$req .= 'Host: '. $ip . $crlf;
$req .= 'User-Agent: '.$user_agent . $crlf;
$req .= 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' . $crlf;
$req .= 'Accept-Language: en-us,en;q=0.5' . $crlf;
//$req .= 'Accept-Encoding: deflate' . $crlf;
$req .= 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7' . $crlf;
foreach ($custom_headers as $k => $v)
$req .= $k .': '. $v . $crlf;

if (!empty($cookie_str))
$req .= 'Cookie: '. substr($cookie_str, 0, -2) . $crlf;

if ($verb == 'POST' && !empty($postdata_str))
{
$postdata_str = substr($postdata_str, 0, -1);

$req .= 'Content-Type: application/x-www-form-urlencoded' . $crlf;
$req .= 'Content-Length: '. strlen($postdata_str) . $crlf;
$req .= "Connection: Close".$crlf.$crlf;
$req .= $postdata_str.$crlf;
}
else $req .= "Connection: Close".$crlf.$crlf;

if ($req_hdr)
$ret .= $req;

if ((($fp = @fsockopen($ip, $port, $errno, $errstr)) == false) || !is_resource($fp) )
{

$error_str="Error $errno: $errstr\n";
return 0;
}


// stream_set_timeout($fp, 0, $timeout * 1000);

fputs($fp, $req);



if ($line = fread($fp,(1024*20))){ $ret .= $line; }






if (!$res_hdr) {

$pos=strpos($ret, "\r\n\r\n");
if($pos!==false)
{
$give_header_str=substr($ret , 0 , $pos);

$this->gived_headers_str=$give_header_str;

$this->gived_headers= $this->gived_header_init(explode($crlf,$give_header_str)) ;
// var_dump($this->gived_headers);
if(isset($this->gived_headers['content-length']['values']) && is_numeric($this->gived_headers['content-length']['values']))
{
$content_lenght=(int)$this->gived_headers['content-length']['values'];



if($content_lenght>($max_document_size) || $content_lenght < 0)
{
$error_str='net::ERR_RESPONSE_DOCUMENT_TOO_BIG';
return 0;

}
}

// $error_str='net::ERR_RESPONSE_content_length not exist';



$ret = substr($ret, $pos+ 4);
}else
{

$error_str='net::ERR_RESPONSE_HEADERS_TOO_BIG';
return 0;

}
}



while (($line = fread($fp,($max_header_size))))
{
if(strlen($ret)> $max_document_size)
{
$error_str='net::ERR_RESPONSE_DOCUMENT_TOO_BIG';
return 0;
}

$ret .= $line;
}
if($content_lenght!=-1)
{

if(strlen($ret)!=$content_lenght)
{
$error_str='net::ERR_RESPONSE_DOCUMENT_TOO_BIG';
return 0;
}

}



fclose($fp);


if($http_ver=='HTTP/1.1'){
if(isset($this->gived_headers['transfer-encoding']['values'])&&strtolower(trim($this->gived_headers['transfer-encoding']['values']))=='chunked')
{
// if(is_hex(substr($ret,0,4)))
// {
return http_chunked_decode(($ret));
// }
}

}
return $ret;
}


}




class browser{


private $queue_links=array();
private $document_text_content='';



function __construct(){




}


public function feach($address='',&$document_content='',&$content_lenght=0,&$content_type='',&$response_headers=array(),&$error_str='') {


$document_content='';
$content_lenght=0;
$content_type='';
$response_headers=array();
$error_str='';


$redirect_limit=5;

$document_content_type='';
$document_content_length=0;
$http_status_code=0;
$http_version='';
$http_status='' ;
$http_response='' ;
$http_status_codes= array(
// Informational 1xx
100 => 'Continue',
101 => 'Switching Protocols',

// Success 2xx
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',

// Redirection 3xx
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found', // 1.1
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
// 306 is deprecated but reserved
307 => 'Temporary Redirect',

// Client Error 4xx
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',

// Server Error 5xx
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
509 => 'Bandwidth Limit Exceeded'
);
$document_content_temp='';

while($redirect_limit>=0)
{
$redirect_limit--;


$path='/';
$host='';
$port=80;
$get_method_data=array();
$protocol='';


if($parse_address=parse_url($address))
{

if( !isset($parse_address['scheme'])) //// protcol not exist
{
$error_str=" host not exist on url (".$address.") ";
return 0;

} else
{
$protocol=$parse_address['scheme'];
}




if( ! isset($parse_address['host'])) //// protcol not exist
{

$error_str=" host not exist on url (".$address.") ";

return 0;


}else
{
$host=$parse_address['host'];
}



if(isset($parse_address['path']))
{
$path=$parse_address['path'];
}

if(isset($parse_address['port']))
{
$port=$parse_address['port'];
}



if(isset($parse_address['query']))
{
$explode_query=explode('&',$parse_address['query']);
for($i=0;$i<count($explode_query);$i++)
{

$method_get_key_value=explode('=',$explode_query[$i]);
if(count($method_get_key_value)==2)
{
if(is_string($method_get_key_value[0])&&is_string($method_get_key_value[1]))
{
$get_method_data[$method_get_key_value[0]] =$method_get_key_value[1];
}


}elseif(count($method_get_key_value)==1)
{
if(is_string($method_get_key_value[0]))
{
$get_method_data[$method_get_key_value[0]] ='';
}

}

}

}



if($protocol== 'http')
{

$http=new http();
$http_error_str='';



$document_content_temp=$http->http_request_('get',$host,$port,$path,$get_method_ data,array(),array(),array('Transfer-Encoding: chunked','Accept-Encoding: deflate, gzip'),5,false,false,$http_error_str);

if($document_content_temp===0)
{
if(strlen($http_error_str))
{
$error_str=$http_error_str;
return 0;
}

}




$get_gived_headers=$http->get_gived_headers();


}





// var_dump($get_gived_headers);exit;

if(isset($get_gived_headers[0]))
{
$http_version=substr($get_gived_headers[0],0,8) ;
$http_status_code=(int)substr($get_gived_headers[0],9,3) ;
$http_status=substr($get_gived_headers[0],13) ;
$http_response= $get_gived_headers[0] ;


//var_dump($get_gived_headers,$document_content);
if(!($http_version=='HTTP/1.1' || $http_version=='HTTP/1.0'))
{
$error_str=" HTTP version not 'HTTP/1.1' or 'HTTP/1.0' ";
return 0;

}


if($http_status_code==301 || $http_status_code==302)
// HTTP/1.1 301 Moved Permanently
// HTTP/1.1 302 Moved Temporarily
{

if(isset($get_gived_headers['location']['values']))
{
if($new_address=relative_to_absolute_link($get_giv ed_headers['location']['values'],$address))
{

$address=$new_address;
}
}



}
elseif($http_status_code==200) // HTTP/1.1 200 OK
{



if(isset($get_gived_headers['content-type']['value']['content-type'][0]))
{
$document_content_type=strtolower(trim($get_gived_ headers['content-type']['value']['content-type'][0]));
}else
{
$error_str=" can not detect document content-type ";
return 0;
}


if(isset($get_gived_headers['content-length']['values']))
{
$document_content_length=(int)(trim($get_gived_hea ders['content-length']['values']));
}



if(isset($get_gived_headers['content-encoding']['values']))
{
$document_content_encoding=strtolower(trim($get_gi ved_headers['content-encoding']['values']));

if($document_content_encoding == 'deflate')
{

$document_content=gzinflate(($document_content));

}
}




if(isset($get_gived_headers['transfer-encoding']['values']))
{
$document_content_encoding=strtolower(trim($get_gi ved_headers['transfer-encoding']['values']));

if($document_content_encoding == 'chunked')
{

// if(is_hex(substr($document_content,0,4)))


// $document_content=http_chunked_decode(($document_c ontent));



}
}











$redirect_limit=0;
break;


}
elseif(isset($http_status_codes[$http_status_code]))
{

$error_str=" can not open url'".$address."' because server send '".$http_response."' ";


return 0;


break;
$redirect_limit=0;
}
else{

$error_str=" can not open url'".$address."' because unknoew HTTP status code '".$http_status_code."' ";
return 0;


break;
$redirect_limit=0;
}

}



//var_dump($http->get_gived_headers_str(),$http->get_gived_headers()); exit;

}else
{
$error_str=" protocol '".$protocol."' not reconize ";

return 0;
}







} // end while redirect loop

if($http_status_code==200)
{
$document_content=$document_content_temp;
$content_lenght=$document_content_length;
$content_type=$document_content_type;
$response_headers=$get_gived_headers;

return 1;

}else
{
if(isset($http_status_codes[$http_status_code]))
{

$error_str='http error '.$http_status_codes[$http_status_code];
}else
{

$error_str='http error '.$http_status_code;
}

return 0;
}


}








} // end class browser


$b=new browser();



$b->feach('http://aalma.ir',$document_content,$content_len,$content_ type,$response_header,$error_feach);
if(strlen($error_feach))
{

die($error_feach );
}

echo ($document_content);

ingenuous67
شنبه 17 خرداد 1393, 14:57 عصر
دوستان دوباره سلام
پیدا کردم مشکل حل شد.
فقط کافیه این یه خط کد رو به ابتدای فایل اضافه کنیم.
ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');

به همین خوشمزگی
الان دیگه خیلی راحت میشه این دستور رو اجرا کرد
echo file_get_contents('http://www.eforosh.com');
بازم مرسی از راهنماییتون