How to see what browser (and version) a user is using? - php

Due to the CSS3 and HTML5, I would like to check what browser (and version) the visitor is using. What is the best way of doing that? I know that most ways screw up certain checks, for example phones or tablets, as well as some uncommon browsers, but there ought to be a way to get that information properly?

I recommend you take a look at Modernizr. It's a js library that does what you need

No need to have JS libraries or other included files!
$browser = get_browser(null, true);
echo $browser['browser'];
This will return something like "Firefox," "Opera," "Safari," etc.

You can use PHP for this
<?php
echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";
?>

$browser = $_SERVER['HTTP_USER_AGENT'];
OR you can use google analytic to view live data of your visitor

Related

PHP Limit users browser type

I need to limit what browsers can user use to view on my page. I want to allow only Chrome, FireFox 4+ and all WebKit based browsers. If user use i.e. explorer, PHP will produce output i.e.: "You have not supported browser, use Chrome, Firefox 4+ or WebKit based browser!"
How can I do it?
PHP sniffer is a library that handles extracting information about the user and user-agent (browser).
It uses the same data that get_browser() or $_SEREVER['HTTP_USER_AGENT'] can give you but it formats it into a nicely structured object that you can use in your code.
Use $_SERVER\['HTTP_USER_AGENT'\] or get_browser().
But you should really ask yourself why this is necessary. If your site doesn't work with all feature, than it's ok to show a message saying:
Please upgrade your browser in order to use all features.
You can also detect whether specific JS functions/objects exists so you won't run into Undefined identifier errors (credits to epascarello).
$_SERVER['HTTP_USER_AGENT'] will give you browser details, and from that you can work your way up to verification good luck
This may be so lame, since I am PHP newbie but, I would check if someone is using Mozilla (firefox)
by doing this:
$browser = $_SERVER['HTTP_USER_AGENT'];
if (strpos($browser,'Mozilla') !== false) {
echo 'You are using Mozilla';
} else {
echo 'You are not using Mozilla';
}
You can check server variable:
<?php
echo $_SERVER['HTTP_USER_AGENT'];
?>

How to detect between mobile & tablets in PHP

I read on this thread:
Simplest way to detect a mobile device
on how to know if the browser is a mobile device. the general code is this:
<?php include("Mobile_Detect.php"); include("demoData.php");
$detectIsMobile = new Mobile_Detect(); ?>
The problem is that I want to treat tablets (iPad & xoom).
I saw there that there is a isIpad() function that I have tested yet - but that still doesn't solve the difference between tablets and mobile phones.
Any idea?
thanks,
Alon
Use
<?php
if($detect->isTablet()){
// any tablet
}
?>
You can refer this page for more info http://code.google.com/p/php-mobile-detect/
The only way to do this is with a huge lookup table of User-Agent: strings.
get_browser() would probably be able to do what you want, but you would need to make sure that you keep the browscap file very up to date - new tablet models are being released on a weekly basis.
Alternatively there may some Javascript way to do it (although I don't know what that might be) but
you would still have to keep a very large lookup table updated
you should never rely on Javascript for any kind of functionality.
$detect = new Mobile_Detect;
$deviceType = ( $detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer');
echo $deviceType;

How to get a site screenshot quickly using PHP?

I want to create a web directory site, and I need to get these site screenshots. How to get a site screenshot quickly using PHP?
I tried IECAPT,webscreencapture, khtml2png, but they are all slowly. And they all get screenshot one url by one url.
Is IECAPT depends on a ie browser? if it is, why it can not open many ie tags so that work at the same time?
Is there anyone can recommend me a PHP screenshots software using online? according to my above requirements? Thank you.
Your requirements are unrealistic. Your best bet is to integrate with WebKit through something like CutyCapt that doesn't run an actual browser, but just the WebKit rendering engine. You shouldn't have any concurrency issues, but it it isn't going to be fantastic.
These external services are developing fast. Take a look at:
http://immediatenet.com/thumbnail_api.html
it renders thumbnails extremely fast and caches them like the other similar services.
Probably the easiest way is to use an external service. There used to be Alexa Site Thumbnail but it has been discontinued, so you must look for alternatives. For example http://www.pageglimpse.com/ seems to be one.
I have tried CutyCapt, I copied 3 CutyCapt.exe and renamed them. But it also catch the screenshot one by one , not run the 3 processes at one time.
<?php
set_time_limit(0);
$url1 = 'http://www.google.co.uk';
$out1 = '1.jpg';
$path1 = 'CutyCapt1.exe';
$cmd1 = "$path1 -u=$url1 -o=$out1";
//exec($cmd);
system($cmd2);
$url2 = 'http://www.google.com';
$out2 = '2.jpg';
$path2 = 'CutyCapt2.exe';
$height2 = '1200 ';
$cmd2 = "$path2 -u=$url2 -o=$out2";
//exec($cmd);
system($cmd2);
$url3 = 'http://www.google.co.jp';
$out3 = '2.jpg';
$path3 = 'CutyCapt3.exe';
$height3 = '1200 ';
$cmd2 = "$path3 -u=$url3 -o=$out3";
//exec($cmd);
system($cmd3);`
?>
I do not think many thumbnail service site, like pageglimpse.com, they install many browsers on their web servers. What is the technology they use?

How to determine the browser of the user using PHP?

How to determine the browser of the user using PHP?
So that if the users browser is IE then the variable $alert="onbeforeunload" and if it is not IE, for example Firefox (else) then $alert="onload.
Help is much appreciated.
Thanks
Also please note that I can not install browscap.ini on my PHP server.
See if this code works for you
<?php
function detect_ie(){
return (sizeof(explode("MSIE",$_SERVER['HTTP_USER_AGENT'])) > 1);
}
if (detect_ie())
$event = "onbeforeunload";
else
$event = "onload";
?>
You can't. Not with 100% accuracy. The best method that is to check the user agent, however the user is free not to supply it, or fake it so try to avoid relying on it.
$ua = $_SERVER['HTTP_USER_AGENT'];
If the browser is IE, it should (roughtly) match (Where # is a number)
Mozilla/#.0 (compatible; MSIE #.##;
For which the regex would be something like
'~^Mozilla/[0-9]\.0 (compatible;\s+MSIE~i'
Alternatively you could just check for the string "MSIE" which would be simpler and slightly less strict.
But as #Michal said, there are other (better) implmentations on the get_browser manual page
use $_SERVER['HTTP_USER_AGENT']
Check if it contains "IE" directly followed by a number.
I'd rather do this in javascript itself though.
if (typeof window.onbeforeunload !== "undefined") window.onbeforeunload = myFunc;
else window.onunload = myFunc;
Javascript libraries are much better to detect and handle different browser behaviour. Just let a mature library like jQuery handle this and you'll be fine.

Is it possible to detect what operating system a user is coming from using PHP? (mac or windows)

Let's say for example I wanted to echo "You are using Windows!" or "You are using Macintosh!", depending on the users OS. Is this possible?
By analyzing $_SERVER['HTTP_USER_AGENT'] it's possible to tell what system (and browser) the user is claiming to use.
It's easily spoofable, though.
Try the get_browser() function that's built into PHP.
$browser = get_browser(null, true);
echo "Platform: " . $browser["platform"] . "\n";
You can look at the user agent string, which often includes information about the OS.
Note, however, that there are operating systems other than Windows and OS X.
http://phpcode.mypapit.net/detect-ip-location-operating-system-and-browser-using-php-detector-library/46/
The library is handy for for creating web application which serve content depending on users location and type of operating system/browser that he use or for creating web application that collect web surfers statistical data.
code example:
require('detector.php');
$dip = &new Detector($_SERVER["REMOTE_ADDR"], $_SERVER["HTTP_USER_AGENT"]);
echo "$dip->town";
echo "$dip->state, $dip->ccode,$dip->town, ($dip->ipaddress) ";
echo "using : $dip->browser $dip->browser_version on $dip->os $dip->os_version";
Yes it is possible.
You want to use $_SERVER['HTTP_USER_AGENT'] which holds information about the user's operating system and browser.
You can use this resource to look up user agent strings (which are held in that variable).
However, it is possible for browsers to spoof this information so you can't assume that this is reliable.

Categories