Location: PHPKode > scripts > PHP Functions for detecting Spiders and Small Screen Devices > php-functions-for-detecting-spiders-and-small-screen-devices/index.php
<?php
/*


is_spider()
	- Function for testing if a useragent matches that of a spider
	- Returns useragent string if true

is_smallscreen()
	- Function for testing if a useragent matches that of a small screen
	  device, ie PDA or cellphone 
	- Returns useragent string if true
	- Not really dependable, I couldn't find a comprehensive list of
	  user agents or anything in common, but good enough for me.





Copyright (c) 2006, Takeshi Media

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 

*/





function is_spider()
{
	global $_SERVER;
	static $isspider = 'init';

	if( $isspider != 'init' )
	{
// its set so don't do anything 
	} else {
// don't know if this is a spider or not yet
		$spiders = array(
		"almaden.ibm.com" ,
		"gigabot" ,
		"appie 1.1" ,
		"architext" ,
		"ask jeeves",
		"asterias2.0",
		"augurfind" ,
		"baiduspider" ,
		"bannana_bot" ,
		"bdcindexer" ,
		"crawler" ,
		"hide@address.com" ,
		"docomo" ,
		"fast-webcrawler", 
		"fluffy the spider",
		"frooglebot",
		"geobot",
		"googlebot",
		"mediapartners-google",
		"gulliver",
		"henrythemiragorobot",
		"ia_archiver",
		"infoseek",
		"kit_fireball",
		"lachesis",
		"lycos_spider",
		"mantraagent",
		"mercator",
		"msnbot",
		"moget" ,
		"muscatferret" ,
		"nationaldirectory-webspider" ,
		"naverrobot" ,
		"ncsa beta" ,
		"netresearchserver" ,
		"ng/1.0" ,
		"osis-project" ,
		"polybot" ,
		"pompos" ,
		"scooter" ,
		"seventwentyfour" ,
		"sidewinder" ,
		"sleek spider" ,
		"slurp" ,
		"steeler" ,
		"szukacz" ,
		"t-h-u-n-d-e-r-s-t-o-n-e" ,
		"teoma" ,
		"turnitinbot" ,
		"ultraseek" ,
		"vagabondo" ,
		"voilabot" ,
		"w3c_validator" ,
		"zao" ,
		"zyborg" 
		);

		$useragent = strtolower( $_SERVER["HTTP_USER_AGENT"] );
		$isspider = 0;
		while( $pattern = array_pop( $spiders ))
		{
			$isspider = strpos( $useragent, $pattern);
			if( is_integer( $isspider ) ) 
			{ 
				$isspider = $pattern; 
				break;
			}
		}
	}
	return $isspider;
}



/*
	No gaurantees with this one,
	Its supposed to sniff out smallscreen devices based on user agent
	I found that there is no comprehensive list of useragents though

	returns the useragent string if true
*/

function is_smallscreen()
{
	global $_SERVER;
	static $issmallscreen = 'init';

	if( $issmallscreen != 'init' )
	{
// its set so don't do anything 
	} else {
// don't know if this is a spider or not yet
		$smallscreens = array(
			'alcatel',
			'ericsson',
			'nokia',
			'panasonic',
			'phillips',
			'samsung',
			'sanyo',
			'wml',
			'mot',
			'wap',
#			'ppc',	# caused issues with Mac OS X
			'windows ce'
		);

		$useragent = strtolower( $_SERVER["HTTP_USER_AGENT"] );
		$issmallscreen = 0;	
		while( $pattern = array_pop( $smallscreens ))
		{
			$issmallscreen = strpos( $useragent, $pattern);
			if( is_integer( $issmallscreen ) )
			{ 
				$issmallscreen = $pattern; 
				break;
			}
		}
	}
	return $issmallscreen;
}

?>
Return current item: PHP Functions for detecting Spiders and Small Screen Devices