Php : Finding Chrome and Safari Browsers - php

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

Related

HTTP_USER_AGENT in PHP says Mozilla for IE

I need to be able to specify between IE 11 and Firefox in a PHP script. I have the following function. However, in IE it returns Mozilla. Is there another way to approach this to distinquish between Firefox and IE?
function browser() {
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
// you can add different browsers with the same way ..
if(preg_match('/(chromium)[ \/]([\w.]+)/', $ua))
$browser = 'chromium';
elseif(preg_match('/(chrome)[ \/]([\w.]+)/', $ua))
$browser = 'chrome';
elseif(preg_match('/(safari)[ \/]([\w.]+)/', $ua))
$browser = 'safari';
elseif(preg_match('/(opera)[ \/]([\w.]+)/', $ua))
$browser = 'opera';
elseif(preg_match('/(msie)[ \/]([\w.]+)/', $ua))
$browser = 'msie';
elseif(preg_match('/(mozilla)[ \/]([\w.]+)/', $ua))
$browser = 'mozilla';
preg_match('/('.$browser.')[ \/]([\w]+)/', $ua, $version);
return array($browser,$version[2],'name'=>$browser,'version'=>$version[2]);
}
Thanks!
Internet Explorer 11 didn't include an "MSIE" string, because they didn't want people to assume that it had the same problems as older versions of IE.
You could add lines like these:
elseif(preg_match('/(trident)[ \/]([\w.]+)/', $ua))
$browser = 'msie';
elseif(preg_match('/(firefox)[ \/]([\w.]+)/', $ua))
$browser = 'firefox';
Every web browser will contain the word "Mozilla" in its user agent string (it dates back to the Netscape era.)

php make google and facebook access my site

I want to access my site from ONLY my country and I want to google and facebook can access to my site. So I looked to the these pages:
https://developers.facebook.com/docs/sharing/best-practices#crawl
https://support.google.com/webmasters/answer/1061943?hl=en
And I writed this php code:
<?php
$agent = $_SERVER['HTTP_USER_AGENT'];
if (!preg_match("/facebook/", $agent) && !preg_match("/bingbot/", $agent) && !preg_match("/Googlebot/", $agent) && $agent != 'Facebot')
{
$country = geoip_country_code_by_name($_SERVER['REMOTE_ADDR']);
if($country != 'AZ')
{
$error = 'Sorry, we can only serve to Azerbaijan!';
include('error.php');
die;
}
}
?>
It works on facebook. But when I want to get my page speed results with google, i get "Sorry, we can only serve to Azerbaijan!" error
This is the one of the user agent of Google Page Speed Insight
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko; Google Page Speed Insights) Chrome/27.0.1453 Safari/537.36
Change this
!preg_match("/Googlebot/", $agent)
to this
!preg_match("/Google(bot| Page Speed Insights)/", $agent)

Detect iOS version lower than iOS 8 php

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) { };?>

How to show model name of mobile when using mobile

$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');

How to include a php file(top.php) inside a javascript code?

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>

Categories