Solved

Javascript/CSS detection in PHP

Posted on 2003-10-26
5
1,114 Views
Last Modified: 2013-11-19
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?
0
Comment
Question by:jmfreema
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 2

Assisted Solution

by:errows
errows earned 75 total points
ID: 9623341
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
 

Assisted Solution

by:frankenstrat
frankenstrat earned 75 total points
ID: 9623347
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
 

Author Comment

by:jmfreema
ID: 9624865
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
 
LVL 11

Accepted Solution

by:
shmert earned 75 total points
ID: 9624997
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
 

Author Comment

by:jmfreema
ID: 9625060
Thank you all so much. That was very helpful.
0

Featured Post

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question