简体   繁体   中英

How can I detect IE9 or below then execute some PHP based on the outcome?

I know how to detect the browser using javascript, but how can I pass this information to PHP?

How can I do the following:

  1. Javascript detects the browser, returns 'yes' if the user has IE 9 or below and moves to step 2.

  2. Javascript communicates 'yes' to PHP and moves to step 3.

  3. PHP executes an if / else statement based on whether or not a 'yes' from javascript above is present.

User Agent is not a reliable method.
Personally I use an Agent Switcher to fool websites quite often.
This method would not be fooled.
My only option would be to disable JavaScript.

Pick a JS instruction that works in IE9+ and not previous versions

There is probably a better instruction to use other than getElementsByClassName but it demonstrates the concept.

Pick an instruction form a test page like this:

Browser Capability Tests

Then build a test page and test that page on a Cross Browser Test site.


This test will properly pass (TRUE) or fail (FALSE) based on Browser support for try{}catch{} on any Browser going back to 1999:

  • Netscape 6.0
  • FireFox 1.0
  • IE 5.5

It will Pass TRUE on:

  • Chrome
  • FireFox 3.0+
  • IE9+
  • Opera
  • Safari

Will fail (FALSE) only on IE5.5 - IE8


HTML

<div class="test"></div>

JS

var checkIE = false;
try {
   var el = document.getElementsByClassName('test');
   checkIE9 = true;
}
catch(e)  {
  checkIE9 = false;
}

Then redirect based whether checkIE9 is true or false

Replace mozilla.org with the IE9 page/script and microsoft.com with the old style page/script

if (checkIE9){
  window.location = 'http://mozilla.org';
}
else{
  window.location = 'http://www.microsoft.com';
}

Or submit a form based on true or false.

HTML

<form action="true.php" method="get"><div>
  <input type="hidden" name="chkIE" value="1"/>
  <button id="t" type="submit"></button>
</div></form>

<form action="false.php" method="get"><div>
  <input type="hidden" name="chkIE" value="2"/>
  <button id="f" type="submit"></button>
</div></form>

NOTE: Yes it could be one form with two buttons but forms are cheap, and I like to pass values with hidden inputs.

I did not use zero as a value in case the a user were to go direct to the page the forms link to.

Zero Check Code in the test.php:

$chckIE9 = intval($_GET['chkIE9']);
if($chckIE9 == 0){
  include('test.html');
  exit;
}

Or if you may want an intermediate page and link both true and false to the same script.

<form action="test.php" method="get"><div>
  <input type="hidden" name="chkIE" value="1"/>
  <button id="t" type="submit"></button>
</div></form>

<form action="test.php" method="get"><div>
  <input type="hidden" name="chkIE" value="2"/>
  <button id="f" type="submit"></button>
</div></form>

PHP:

$chckIE9 = intval($_GET['chkIE9']);
if($chckIE9 == 1){
  include('true.php');
}
elseif($chckIE9 == 2){
  include('false.php');
}
else{
   include('test.html');
}
exit;

JS
Here a decision must be made as to which way to default.
You could click 'f' if (!checkIE9)
Then default would be the IE9+
If there is a need to default it is probably better to default to the false

if (checkIE9){ 
  document.getElementById('t').click();
}
else{
  document.getElementById('f').click();
}

Snipppet

This snippet will display either "IE9+ Compatible" or "UPDATE YOUR BROWSER"
This code is tested with IE8 which pops up "UPDATE YOUR BROWSER"

 var checkIE = false; try { var el = document.getElementsByClassName('true'); checkIE9 = true; } catch(e) { checkIE9 = false; } if (checkIE9){ document.getElementById('t').style.display = 'block'; } else{ document.getElementById('f').style.display = 'block'; } 
 #t,#f,.hide{display:none;} 
 <!DOCTYPE html> <html lang="en"> <head></head><body> <div id="t" class="true"><h2>&#x2003;IE9+ Compatible</h2></div> <div id="f"><h2>&#x2003;UPDATE YOUR BROWSER</h2></div> </div> <form action="test.php" method="get"><div><input type="hidden" name="chkIE" value="1"/><button class="hide" id="t" type="submit"></button></div></form> <form action="test.php" method="get"><div><input type="hidden" name="chkIE" value="2"/><button class="hide" id="f" type="submit"></button></div></form> </body></html> 

$_SERVER['HTTP_USER_AGENT']

Contents of the User-Agent: header from the current request, if there is one. This is a string denoting the user agent being which is accessing the page. A typical example is: Mozilla/4.5 [en] (X11; U; Linux 2.2.9 i586). Among other things, you can use this value with get_browser() to tailor your page's output to the capabilities of the user agent.

You can not pass information from Javascript to the PHP without any interaction with server eg refresh page or ajax. It is because javascript operate on content generated with PHP server and sent as response to the browser. If the browser obtain a response (content) try render it and then you can handle what browser does via javascript. At this time PHP server doesn't know who are you or what you want to do next without any request.

So if you want to do with refresh page, you can handle version via javascript and refresh page with updated url.

Use case tip:

  • client request to -> server = i want a normal page
  • server response to -> client = here is it
  • client browser parsing = ha js tell me what browser version
  • javascript -> IE9, ok so do refresh with my own parameter, add as GET parameter to url
  • client request to -> server = ha give me page with param = IE9, generating..
  • server response to -> client = here is it !!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM