How to detect browser? [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I detect the browser with PHP or JavaScript?
I need to show different information in different browser ( Internet Explorer, Firefox, Chrome, Safari and Opera ). So I need a way to detect the user's browser.
What is the best way to do that in my case ?
I could use PHP or Javascript but I want to avoid to use external library like jQuery.
Thanks :)

Browser detection is a really unreliable practice, and should be avoided. That being said, you typically get started by sniffing navigator.userAgent in JavaScript, or checking the userAgent (or get_browser()) as it comes across with the requests to your server.
However, don't do this - please (for the ponies). Use feature detection instead. Tools like Modernizr make it very easy for you to deliver content up only when it's supported, and to code around differences between browsers.

you can use the following:
php
$agent = $_SERVER["HTTP_USER_AGENT"];
if (preg_match("/MSIE/i",$agent)) { echo "its ie!"; }
javascript
navigator.appName
navigator.appVersion
you can combine/crosscheck and starting to make a large if/else chunk
with everything you find.
also check here
but i agree, this is non 100% reliable

Related

Web scraper that handles JavaScript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Make a JavaScript-aware Crawler
I'm trying to figure out what to use as the basis for a PHP based web scraper that can handle pages that render using JavaScript. Many web site scrape attempts (at least the ones I handle) now fail unless the JS in those pages is executed. The pages are not built to gracefully fall back to no-script implementations. This includes those that make heavy use of AJAX.
Would anyone have suggestions for where to start with the development of a web scraper that can handle modern and heavily JavaScript dependent web pages?
Something that can be used by PHP would be best.
It's possible to use a web browser engine in headless mode to load the page and analyze the DOM. Some googling pointed me at http://phantomjs.org/
Those sites that have heavy ajax usage, just call the same urls as the page does, and build your site content on that response rather than requesting the page.
Those sites that have heavy document.write or framework equivalent thereof, you could probably just strip space or match tags or relevant content using simple regex and again request the script responsible rather than the page that requests it ...
You could use Selenium which is a browser automation tool and then use one of the PHP bindings here, here, or here so you can automate Selenium from PHP.
You would have to have a JavaScript engine in PHP. Or some headless Webkit on the command line. And even then it would get hugely complicated. So the short answer would be: No, sorry, you can't do that.
PHP supports the V8 engine, so I guess you could pass over javascript to V8. Not a pretty thing to do though, I would use something else than straight PHP to do this.

Test Connection to YouTube Servers with JavaScript or PHP [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Detecting if youtube is blocked by company / ISP
Is there a way to test if a user's browser has access to YouTube servers using JavaScript or PHP?
Some companies block access to certain sites, like YouTube for obvious reasons, and therefore it's necessary to stream fallback videos from a different CDN if that is the case. I currently have a solution using ActionScript, but I would prefer to use PHP or JavaScript to replace the div instead if that's possible.
EDIT:
As #NathanKleyn said the php code below wil only check if your server has access to youtube, not the client that's using your tool. If this is what you want (which i guess it is after re-reading your question) the javascript solution below should be a solution too.
var request = new XMLHttpRequest();
request.open('GET', 'http://www.youtube.com', false);
request.send(null);
alert(request.status);
One way to achieve this is to request the headers on youtube.com with PHP's get_headers(), check if the HTTP code returned to determine if the site is accessible.
You could probably do this with curl too though it is more complex, yet alot faster.

How reliable is server side detection of flash?

In answering a question on stack overflow, one person recommended using the following code to detect flash server-side, he pointed out that it does not work in safari:
if (strstr($_SERVER['HTTP_ACCEPT'], 'application/x-shockwave-flash')){
$hasFlash = true;}
Testing for Flash capability on the server-side
My question: why does it not work in safari, and how reliably does it work with other browsers?
That's because Safari decided not to send the HTTP_ACCEPT headers of every single possible "acceptable" request, including Flash. It will be highly unreliable to perform the test like this.
A better way of doing it could be to test on the client side using Javascript, set a cookie, then redirect.
All I could think of is that the mime type would not be detected properly by Safari. Safari might not recognize what application/x-shockwave-flash means. I tried to find similar stories in Google but unsuccessful.

Block IE and Opera using PHP

I want to redirect user to a selected page if they use Any Version of IE or Opera. Because my website is using CSS 3 fully with different effects (animation effects) which are not supported by IE and Opera yet.
I want to do it using PHP. Should the code bellow work?
if(preg_match('/MSIE/i',$u_agent) || preg_match('/Opera/i',$u_agent))
{
header("Location: http://www.example.com/reject.html");
}
Or suggest me a better way please...
Use this
if(preg_match('/MSIE|Opera/i',$u_agent))
{
header("Location: http://www.example.com/reject.html");
}
Unfortunately there is no way to do this only with PHP. Browsers usually send a user agent string, but this can be spoofed quite easily. There is no way to be 100% sure of the browser the user is using, however the most reliable way to tell is with JavaScript. You can either write your own or do a quick Google search and find a premade one.

How to detect browser addons in Firefox, Opera and IE?

How to - using either JavaScript or PHP - detect addons that are used by a user? In particular the ORBIT addon.
I realize this is an old question but it popped up on a google search for me. It most definitely is possible to detect browser addons. Here are a couple of resources to check out:
http://ha.ckers.org/blog/20060823/detecting-firefox-extentions/
http://webdevwonders.com/detecting-firefox-add-ons/
In general, you can't.
Some addons (eg, Firebug) expose a client-side object model to the page, in which case you can detect them using Javascript.

Categories