$agent = $_SERVER['HTTP_USER_AGENT'];
if(preg_match('/iPhone/i', $agent)){
echo "You're using Iphone";
} else if(preg_match('/Android/i', $agent)){
echo "You're using Iphone";
} else if(preg_match('/Blackberry/i', $agent)){
echo "You're using Blackberry";
}
How to ideas when run a mobile is how model name, ex: Iphone => show model is Iphone 4S
You can do that by using library called WURFL.
This this, you can just easily do:
$user_agent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; NOKIA; Lumia 800)";
$requestingDevice = $wurflManager->getDeviceForUserAgent($user_agent);
$is_wireless = ($requestingDevice->getCapability('is_wireless_device') == 'true');
$is_smarttv = ($requestingDevice->getCapability('is_smarttv') == 'true');
$is_tablet = ($requestingDevice->getCapability('is_tablet') == 'true');
$is_phone = ($requestingDevice->getCapability('can_assign_phone_number') == 'true');
Related
This question already has an answer here:
Why do Chrome and IE put "Mozilla 5.0" in the User-Agent they send to the server? [duplicate]
(1 answer)
Closed 8 years ago.
I am trying to do some php browser testing. When I looked at
$_SERVER['HTTP_USER_AGENT'
I found that it returned this:
Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
even though I was on IE 11.
When I was on Chrome, it returned this:
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36
Which makes more sense. Why is there no MSIE in the IE, and how can I target it?
"Trident" is the layout engine for MSIE 11. As you can see there, while you were on IE 11 it loaded as Trident/7.0; rv:11.0) Just like when you were on Chrome it loaded as AppleWebKit/537.36.
If you're looking to get more information about the browser, you could always use PHP's get_browser() function.
its because the MSIE from 8 marks its version through TRIDENT. Ive found this script time ago. It also detects if the browser is in compatibility mode. It may help you, but its in JS:
EDIT: a simple search made i found the original code in github.
var ieUserAgent = {
init: function () {
// Get the user agent string
var ua = navigator.userAgent;
this.compatibilityMode = false;
// Detect whether or not the browser is IE
var ieRegex = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (ieRegex.exec(ua) == null)
this.exception = "The user agent detected does not contain Internet Explorer.";
// Get the current "emulated" version of IE
this.renderVersion = parseFloat(RegExp.$1);
this.version = this.renderVersion;
// Check the browser version with the rest of the agent string to detect compatibility mode
if (ua.indexOf("Trident/7.0") > -1) {
if (ua.indexOf("MSIE 7.0") > -1) {
this.compatibilityMode = true;
}
this.version = 11; // IE 11
}
else if (ua.indexOf("Trident/6.0") > -1) {
if (ua.indexOf("MSIE 7.0") > -1) {
this.compatibilityMode = true;
}
this.version = 10; // IE 10
}
else if (ua.indexOf("Trident/5.0") > -1) {
if (ua.indexOf("MSIE 7.0") > -1) {
this.compatibilityMode = true;
}
this.version = 9; // IE 9
}
else if (ua.indexOf("Trident/4.0") > -1) {
if (ua.indexOf("MSIE 7.0") > -1) {
this.compatibilityMode = true;
}
this.version = 8; // IE 8
}
else if (ua.indexOf("MSIE 7.0") > -1)
this.version = 7; // IE 7
else
this.version = 6; // IE 6
}
};
// Initialize the ieUserAgent object
ieUserAgent.init();
$(document).ready(function() {
if(ieUserAgent.compatibilityMode) {
//do stuff
}
if(ieUserAgent.version == 6) {
//do stuff
}
});
IE11 drops the "MSIE" portion of the user agent string. It doesn't stop there, either - navigator.appName will return Netscape and navigator.product returns Gecko.
The likely cause for this is that IE11 has caught up enough with modern web standards that Microsoft doesn't want it triggering the old if(IE) { // shitty simpler website } handlers all over the web. They want it seeing the same full-featured version Chrome/Firefox see.
If been searching for a while but don't find any good solution. I need to detect the iOS version so that I can decide whether to show some part of the website or not. Additionally this content should be shown on any non MobileSafari browser.
So basically:
If iOS and if version < iOS 8 than do nothing;
Else show content
With lots of thanks to #Lucas1 and #Daan I came up with this :)
<?php
if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') || strpos($_SERVER['HTTP_USER_AGENT'],'iPad' ) || strpos($_SERVER['HTTP_USER_AGENT'], 'iPod' ) !== false){
if (strpos($_SERVER['HTTP_USER_AGENT'], 'OS 8_0') !== false) {
echo "content here on ios";
}
else{echo "sorry no content for you";}
}
else {
echo "content here";
}?>
The HTTP_USER_AGENT will return the following:
Mozilla/5.0 (iPhone; U; CPU iPhone OS 8_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7
If you are trying to detect iOS 8, do the following:
<?php if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone OS 8_0') !== false) { };?>
I use below code to find user agent,
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match('/MSIE/i', $user_agent)) {
echo "Internet Explorer";
}
if (preg_match('/Firefox/i', $user_agent)) {
echo "FireFox";
}
if (strpos( $user_agent, 'Chrome') !== false)
{
echo "Google Chrome";
}
if (strpos( $user_agent, 'Safari') !== false)
{
echo "Safari";
}
if (preg_match('/Opera/i', $user_agent)) {
echo "Opera";
}
?>
But my chrome browser returning below useragent suddenly
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.155 Safari/537.22
It contains the word safari and chrome.so both the browser names are printed.what is the solution for this.thanks.
Chrome's user agent contains Safari but Safari's user agent doesn't contain Chrome so use if ... elseif:
if (stripos( $user_agent, 'Chrome') !== false)
{
echo "Google Chrome";
}
elseif (stripos( $user_agent, 'Safari') !== false)
{
echo "Safari";
}
Note: use stripos instead of strpos to account for case-variations.
Try this :
$browser = get_browser(null, true);
print_r($browser);
From doc : Attempts to determine the capabilities of the user's browser, by looking up the browser's information in the browscap.ini file.
ref: http://php.net/manual/en/function.get-browser.php
i want to include a php fie called top.php into my javascript code if it satisfy the condition. This is a code for checking browser version. what i need is to check the browser name and if its chrome then only need to display the top.php file. But in this code in every browser it include that page.
<script type="text/javascript">
var nVer = navigator.appVersion;
var nAgt = navigator.userAgent;
var browserName = navigator.appName;
var fullVersion = ''+parseFloat(navigator.appVersion);
var majorVersion = parseInt(navigator.appVersion,10);
var nameOffset,verOffset,ix;
// In Opera, the true version is after "Opera" or after "Version"
if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
browserName = "Opera";
fullVersion = nAgt.substring(verOffset+6);
if ((verOffset=nAgt.indexOf("Version"))!=-1)
fullVersion = nAgt.substring(verOffset+8);
}
// In MSIE, the true version is after "MSIE" in userAgent
else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
browserName = "Microsoft Internet Explorer";
fullVersion = nAgt.substring(verOffset+5);
}
// In Chrome, the true version is after "Chrome"
else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
browserName = "Chrome";
fullVersion = nAgt.substring(verOffset+7);
}
// In Safari, the true version is after "Safari" or after "Version"
else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
browserName = "Safari";
fullVersion = nAgt.substring(verOffset+7);
if ((verOffset=nAgt.indexOf("Version"))!=-1)
fullVersion = nAgt.substring(verOffset+8);
}
// In Firefox, the true version is after "Firefox"
else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
browserName = "Firefox";
fullVersion = nAgt.substring(verOffset+8);
}
// In most other browsers, "name/version" is at the end of userAgent
else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) <
(verOffset=nAgt.lastIndexOf('/')) )
{
browserName = nAgt.substring(nameOffset,verOffset);
fullVersion = nAgt.substring(verOffset+1);
if (browserName.toLowerCase()==browserName.toUpperCase()) {
browserName = navigator.appName;
}
}
// trim the fullVersion string at semicolon/space if present
if ((ix=fullVersion.indexOf(";"))!=-1)
fullVersion=fullVersion.substring(0,ix);
if ((ix=fullVersion.indexOf(" "))!=-1)
fullVersion=fullVersion.substring(0,ix);
majorVersion = parseInt(''+fullVersion,10);
if (isNaN(majorVersion)) {
fullVersion = ''+parseFloat(navigator.appVersion);
majorVersion = parseInt(navigator.appVersion,10);
}
document.write(''
+'Browser name = '+browserName+'<br>'
+'Full version = '+fullVersion+'<br>'
+'Major version = '+majorVersion+'<br>'
+'navigator.appName = '+navigator.appName+'<br>'
+'navigator.userAgent = '+navigator.userAgent+'<br>'
)
var OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";
document.write('Your OS: '+OSName+'<br>');
if(browserName == 'Chrome')
{
document.write("u r using chrome "+fullVersion);
<?php include("top.php") ?> ;
}
else {
alert("Not chrome");
}
</script>
You can't and you shouldn't. The browser doesn't execute any PHP code.
what i need is to check the browser name and if its chrome then only need to display the top.php file. But in this code in every browser it include that page.
I'd be against doing such things on the server side, but if you have to, you can try to check the browser through PHP by examining the request headers.
Try examining $_SERVER['HTTP_USER_AGENT'].
See also: http://php.net/manual/en/function.get-browser.php
In order to decipher what browser your users are using and according to the results include separate PHP files, you can use the $_SERVER['HTTP_USER_AGENT']; or even the built in getBrowser() function - http://php.net/manual/en/function.get-browser.php
<?php
$browser = get_browser(null, true);
print_r($browser);
?>
This code should give you an array similar to this -
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3
Array
(
[browser_name_regex] => ^mozilla/5\.0 (windows; .; windows nt 5\.1; .*rv:.*) gecko/.* firefox/0\.9.*$
[browser_name_pattern] => Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9*
[parent] => Firefox 0.9
[platform] => WinXP
[browser] => Firefox
[version] => 0.9
[majorver] => 0
[minorver] => 9
[cssversion] => 2
...
)
You can then test to see what browser you user is using and include separate files accordingly.
PHP is a sever side language. It runs inside server.
Javascript is client side language. In the sense, it runs in the browser
You cannot include aphp file from client side. Browser can only understand html, js css etc
You have to check the user agent in server array and load the page accordingly.
you can use the
$_SERVER['HTTP_USER_AGENT']; or getBrowser()
Its not possible to include php code in js file . The best way accroding to me is that
put this js script in the head than check the condition browser.
if(browser =='chrome')
{
document.getElementById('top').style.display='block';
}
else
{
document.getElementById('top').style.display='none';
}
<div id='top'>
<?php require_once("top.php"); ?>
</div>
I need to display additional information on a web page (PHP) if one of the following criteria is met:
.NET Framework Installed on the client machine in lower than 3.5
Impossible to determine if .NET Framework 3.5 is installed or not on the client machine
I know that some browser (if not only one, IE) is sending that information in his tag.
Can any of you provide me with his suggestions ?
Website is built in PHP.
Thanks!
EDIT:
Proposed answers are incomplete and/or don't provide me with a robust solution.
The only way to get this information is from the user agent that the browser sends. You can parse the .NET version from there. But note that client can spoof this information or omit it completely, so I wouldn't base any critical functionality on this.
I would try and do a strstr on the UserAgent in PHP, Example Below
A .NET User Agent : Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Pivim Multibar; GTB6.4; .NET CLR 2.0.50727)
The PHP:
<?php
function DotNetInstalled($ua = false)
{
$ua = $ua ? $ua : $_SERVER['HTTP_USER_AGENT'];
//return (strstr('.NET CLR',$ua) !== false);
$matches = preg_match('^\.NET CLR ([0-4]+\.)?([0-9]\.)?(\*|\d+)$',$ua);
if((int)$matches[1] > 0)
{
return array(
(int)$matches[1],
(int)$matches[2],
(int)$matches[3],
);
}
return false;
}
if(false !== ($version = DotNetInstalled()))
{
//Show me the money
//$version[0] = 2
//$version[1] = 0
//$version[2] = 50727
}
?>
I would also check out the following PEAR Package : http://pear.php.net/package/Net_UserAgent_Detect
?>
Here is another version I actually made for a friend of mine and realised that there was a bounty on this thread so what the hell :)
This is fully working but only with User-agent strings as there is no alternative means of doing so.
The core class:
class NETFrameworkChecker
{
//General String / Array holders
var $original_au,$ua_succesParse,$ua_componants,$ua_dotNetString,$CLRTag = "";
//IsInstalled
var $installed = false;
//Version holders
public $major = 0,$minor = 0,$build = 0;
public function __construct($ua = false)
{
$this->original_au = $ua !== false ? $ua : $_SERVER['HTTP_USER_AGENT'];
$this->ParserUserAgent();
}
public function Installed(){return (bool)$this->installed;}
public function AUTag(){return $this->CLRTag;}
//Version Getters
public function getMajor(){return $this->major;}
public function getMinor(){return $this->minor;}
public function getBuild(){return $this->build;}
private function ParserUserAgent()
{
$this->ua_succesParse = (bool) preg_match('/(?<browser>.+?)\s\((?<components>.*?)\)/',$this->original_au,$this->ua_componants);
if($this->ua_succesParse)
{
$this->ua_componants = explode(';',$this->ua_componants['components']);
foreach($this->ua_componants as $aComponant)
{
$aComponant = trim($aComponant);
if(substr(strtoupper($aComponant),0,4) == ".NET")
{
//We have .Net Installed
$this->installed = true;
$this->CLRTag = $aComponant;
//Lets make sure we can get the versions
$gotVersions = (bool)preg_match("/\.NET.CLR.+?(?<major>[0-9]{1})\.(?<minor>[0-9]{1})\.(?<build>[0-9]+)/si",$aComponant,$versions);
if($gotVersions)
{
$this->major = (int)$versions['major'];
$this->minor = (int)$versions['minor'];
$this->build = (int)$versions['build'];
}
break;
}
}
}
}
}
Example Usage:
$Net = new NETFrameworkChecker(); //leave first param blank to detect current user agent
if($Net->Installed())
{
if($Net->getMajor()> 2 && $Net->getMinor() >= 0)
{
//User is using a >NET system thats greater than 2.0.0000
if($Net->GetBuild() >= 0200)
{
//We can do stuff with so that's only supported from this build up-words
}
}else
{
//Redirect them asking them to upgrade :) pretty please
}
}
If you also want to check custom UA Strings from DB lets say
$Net = new NETFrameworkChecker("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Pivim Multibar; GTB6.4; .NET CLR 2.0.50727)");
Info about .NET CLR versions in the UserAgent:
http://www.hanselman.com/blog/TheNETFrameworkAndTheBrowsersUserAgentString.aspx
http://msdn.microsoft.com/en-us/library/ms537503.aspx