Javascript/CSS detection in PHP

I'm fairly certain that javascript detection isnt directly possible in PHP. However, I need some way of determining if they have JS enabled and if their browser is CSS capable. Any suggestions?
jmfreemaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

errowsCommented:
For CSS:

<!-- ONE STEP TO INSTALL CSS SUPPORT REDIRECT:

  1.  Copy the coding into the BODY of your HTML document  -->

<!-- STEP ONE: Paste this code into the BODY of your HTML document  -->

<BODY>

<div id="checkcss" style="position:absolute;"></div>

<SCRIPT LANGUAGE="JavaScript">
<!-- Original:  Ronnie T. Moore, Editor -->
<!-- Web Site:  The JavaScript Source -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
var yes = "yes-css.html"; // CSS page
var no  = "no-css.html";  // Non-CSS page

// How should we look for the (CSS) DIV ?
// Netscape will look with:  document.checkcss
// Internet Explorer will look with:  checkcss

checktype = document.layers ? document.checkcss : checkcss;

window.location = (checktype) ? yes : no;
//  End -->
</script>


For JavaScript:

<HEAD>

<SCRIPT LANGUAGE="JavaScript">

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
jsver = "1.0";
// End -->
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript1.1">
<!-- Begin
jsver = "1.1";
// End -->
</SCRIPT>
<SCRIPT Language="JavaScript1.2">
<!-- Begin
jsver = "1.2";
// End -->
</SCRIPT>
<SCRIPT Language="JavaScript1.3">
<!-- Begin
jsver = "1.3";
// End -->
</SCRIPT>

<NOSCRIPT>
<B>Your browser does not currently support JavaScript.</B>
<P>If your are using Netscape 2 or later then you can enable JavaScript.
<P>Version 2 or 3: Choose Options|Network Preferences, choose the Languages tab, click Enable Javascript and then click OK.
<P>Version 4: Choose Edit|Preferences|Advanced, click Enable JavaScript and then click OK.
<P><HR><P>

With Internet Explorer 5:  Go to Tools Menu|Internet Options, Security Tab  and click on the Custom Level. Then select disable active scripting under the scripting section.
</NOSCRIPT>

<BODY>

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
document.write("<B>Your browser supports JavaScript version " + jsver + ".</B>")
// End -->
</SCRIPT>


That's not done in PHP though...
you should take all info on the first page and redirect your user accordingly...

errows
0
frankenstratCommented:
That is fairly straight forward. If a browser is javascript enabled, then you can use javascript  to forward the browser to page with javascripts.

<body onload="location.href='javascriptpage.html'">

I would say that it is extremely rare for a user to have javascript enabled and css disabled and therefore not really worth the time and effort to code a site for those six people.

Once you have determined that the user's browser is javascript enabled, you can use the following PHP script to present the appropriate stylesheet;

<?php

function inAgent($agent) {
   global $HTTP_USER_AGENT;
   $notAgent = strpos($HTTP_USER_AGENT,$agent) === false;
   return !$notAgent;
}

/*
*  $browser will contain one of the following values:
*      'iewin' : IE 4+ for Windows
*      'iemac' : IE 4 for Macintosh
*     'ie5mac' : IE 5 Macintosh
*      'nswin' : Netscape 4.x Windows
*     'nsunix' : Netscape 4.x Unix
*      'nsmac' : Netscape 4.x Mac
*        'ns6' : Netscape 6 / Mozilla
*/

if ( inAgent('MSIE 4') or inAgent('MSIE 5') or inAgent('MSIE 6') ) {
 if ( inAgent('Mac') )
   $browser = inAgent('MSIE 5') ? 'ie5mac' : 'ie4mac';
 elseif ( inAgent('Win') ) $browser = 'iewin';
} elseif ( !inAgent('MSIE') ) {
 if ( inAgent('Mozilla/5') or inAgent('Mozilla/6') ) {
   $browser = 'ns6';  
 } elseif ( inAgent('Mozilla/4') ) {
   if ( inAgent('Mac') ) $browser = 'nsmac';
   elseif ( inAgent('Win') ) $browser = 'nswin';
   else $browser = 'nsunix';
 }
} else $browser = "unknown";

switch ($browser) {
      case iewin:
      print "<LINK REL=\"StyleSheet\" HREF=\"test.css\" TYPE=\"text/css\">\n";
      break;
      case ie4mac:
      print "<LINK REL=\"StyleSheet\" HREF=\"test.css\" TYPE=\"text/css\">\n";
      break;
      case ie5mac:
      print "<LINK REL=\"StyleSheet\" HREF=\"test.css\" TYPE=\"text/css\">\n";
      break;
      case nswin:
      print "<LINK REL=\"StyleSheet\" HREF=\"test.css\" TYPE=\"text/css\">\n";
      break;
      case nsmac:
      print "<LINK REL=\"StyleSheet\" HREF=\"test.css\" TYPE=\"text/css\">\n";
      break;
      case nsunix:
      print "<LINK REL=\"StyleSheet\" HREF=\"test.css\" TYPE=\"text/css\">\n";
      break;
      case ns6:
      print "<LINK REL=\"StyleSheet\" HREF=\"test.css\" TYPE=\"text/css\">\n";
      break;
      case unknown:
      print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"3;URL=test\">\n";
      break;
      }
?>

Hope this helps
frankenstrat
0
jmfreemaAuthor Commented:
Those are both helpful, but is there any way to have it detect without forcing it to jump to another page? Like, for example, they go to my page and it loads index.php. Is it possible for index.php to know if javascript is enabled or not without forcing it to jump to index_js.php or index_nojs.php for example?
0
shmertCommented:
Yes, this is possible.  Take a look at the get_browser() function: http://php.net/manual/en/function.get-browser.php

It returns an object which contains lots of useful information about the browser which requested the current page.  Note that for this to work you need to download the browsecap.ini file and place it in the correct location.  This defines the feature sets of all the different browsers out there.

One problem is, I don't see any sort of CSS support in the list of keys for the browser object.  So if you want to include browser-specific stylesheets, you'll need to do a little research.  But the get_browser() function should help you to identify the family of browser very reliably.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jmfreemaAuthor Commented:
Thank you all so much. That was very helpful.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.