Display short user agent (ie. ad links)

Started on the old forum in November 2011 · view the original in the archive · old links redirect here

4 replies · 1,332 views · started 15 years ago · 1 min read

4 participants
arnage Lead · 811 posts #1

This snippet is displaying short info of user agent like iPhone, Android, HTC, Nokia... I didn't intend to scrap all possible ua, just most commonly used. Any feedback is welcome.

function shortua() {
	/* SNIPPET BY ARNAGE http://wapforum.rs * DOWNLOAD AT http://coding-talk.com */
$userbrowser = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
$userbrowser = isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']) ? $_SERVER['HTTP_X_OPERAMINI_PHONE_UA'] : $userbrowser;
$userbrowser = isset($_SERVER['HTTP_X_ORIGINAL_USER_AGENT']) ? $_SERVER['HTTP_X_ORIGINAL_USER_AGENT'] : $userbrowser;
$userbrowser = isset($_SERVER['HTTP_X_DEVICE_USER_AGENT']) ? $_SERVER['HTTP_X_DEVICE_USER_AGENT'] : $userbrowser;
$userbrowser = isset($_SERVER['HTTP_X_MOBILE_UA']) ? $_SERVER['HTTP_X_MOBILE_UA'] : $userbrowser;
$userbrowser = isset($_SERVER['HTTP_X_BOLT_PHONE_UA']) ? $_SERVER['HTTP_X_BOLT_PHONE_UA'] : $userbrowser;
$useragent = !empty($userbrowser) ? htmlentities($userbrowser, ENT_QUOTES, 'UTF-8') : 'Browser';
if (strpos($useragent, '/') !== false) {
	$agent = explode('/', $useragent);
	if (stripos($useragent, 'Mini') !== false) {
		$short = $agent[0].'Mini';
	}
	elseif (stripos($useragent, 'Nokia') !== false) {
		if (strcmp($agent[0], 'Mozilla') == 0)
			$short = 'Nokia';
		else
			$short = $agent[0];
	}
	elseif (stripos($useragent, 'iPhone') !== false) {
		$short = 'iPhone';
	}
	elseif (stripos($useragent, 'MSIE') !== false) {
		if (stripos($useragent, 'HTC') !== false)
			$short = 'HTC';
		elseif (stripos($useragent, 'IEMobile') !== false)
			$short = 'WindowsMobile';
		else
			$short = 'PC';
	}
	elseif (stripos($useragent, 'Android') !== false) {
		if (stripos($useragent, 'HTC') !== false)
			$short = 'HTC Android';
		else
			$short = 'Android';
	}
	elseif (stripos($useragent, 'BlackBerry') !== false) {
		$short = 'BlackBerry';
	}
	elseif (stripos($useragent, 'lg-') !== false) {
		$short = 'LG';
	}
	elseif (stripos($useragent, 'sie-') !== false) {
		$short = 'Siemens';
	}
	elseif (stripos($useragent, 'Firefox') !== false) {
		$short = 'PC';
	}
	elseif (stripos($useragent, 'Chrome') !== false) {
		$short = 'PC';
	}
	else {
		$short = $agent[0];
	}
} else {
	$short = mb_strlen($useragent, 'UTF-8') > 10 ? mb_substr($useragent, 0, 10, 'UTF-8') : $useragent;
}
return $short;
}

Usage Example:

echo '<a href="http://somead.tld/?ref=00001">Free '.shortua().' Downloads!</a>';
<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
softwarefreak Senior · 349 posts #2

Bro,I guess & I'm sure,you're the most talented php programmer on earth! I'm not flattering 😀

I need some facebook likes, can you please help me 🤝 http://facebook.com/softwarefreakin I noticed social media is really powerful 🧙 Well DONE is better than well SAID

arnage Lead · 811 posts #5

Thanks guys, i'm glad you like it. 🙂

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close