Jump to content

Browser Detection


php.ajax.coder

Recommended Posts

I am using the following function to fetch a web pages source code but there is a problem when a page has a browser detection script is there any way to make the server think that it is a browser and not a script

function getSource($url){
//@ this removes the warning messages
$source = @file_get_contents($url); 
if (empty($source)){
	return "No Source";	
}else{
	return $source;	
}
}

 

Thanks

 

 

Link to comment
https://forums.phpfreaks.com/topic/149411-browser-detection/
Share on other sites

You need to use curl instead of file_get_contents, and add the headers like

 

curl_set_opt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.7) Gecko/2009030516 Ubuntu/9.04 (jaunty) Firefox/3.0.7');

Actually, when you think about it, that makes a lot more sense - My suggestion won't work.

Link to comment
https://forums.phpfreaks.com/topic/149411-browser-detection/#findComment-784883
Share on other sites

You don't need to use cURL, you can set the user agent with ini_set():

 

<?php
ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; da; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7');
function getSource($url){
   //@ this removes the warning messages
   $source = @file_get_contents($url);
   if (empty($source)){
      return "No Source";   
   }else{
      return $source;   
   }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/149411-browser-detection/#findComment-785142
Share on other sites

I think CURL will help you. I used below function to fetch source code of a webpage :

 

function read_web_page($url) {
   $ch = curl_init($url);
   $fp = fopen("d:/test.txt","w");
   curl_setopt($ch, CURLOPT_FILE, $fp);
   curl_setopt($ch, CURLOPT_HEADER, 0);
   curl_exec($ch);
   curl_close($ch);
   fclose($fp);
}

 

pass url of any webpage and it wil save source code to test.txt in D drive. Change it as per your requirement,

Link to comment
https://forums.phpfreaks.com/topic/149411-browser-detection/#findComment-785150
Share on other sites

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.