0

I would like to detect whether java is enabled in the user's browser when they use my website(php).

If java is enabled a certain div loads on the website if not another div loads.

I do the following:

in the PHP header file: (so whatever loads it checks whether java is enabled)

<?php $no_java=0;?>

<noscript><input type="image" src="whatever.jpg" id="java_checker" value="1"></noscript>

This a 1px by 1px transparent image which can't be seen but it's better than a text box which can be seen.

if(java_checker==1){
    $no_java=1;
}else{
    $no_java=1
}

I've been programming in PHP for a few months now. I know that the if(java_checker==1) is not right but I don't know how to check the value without submitting a form etc...

Anyone could tell me how to do it? the value="<?php $no_java=1;?>" is not good because the php part seems to load regardless to the <noscript> tags :(

Any idea? Or any other idea how I could "tell" php that there is no java so do whatever...

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
muttley8226
  • 1
  • 1
  • 2
  • If java is not enabled on the user's computer they can still use the website without java. everything is programmed in php and java and if no java then the php takes full part on the website. – muttley8226 Sep 10 '11 at 17:17
  • 3
    pretty sure you mean JavaScript and not Java...a VERY important distinction. – Codemwnci Sep 10 '11 at 17:21

3 Answers3

2

PHP is a server-side language whereas Java applets run inside the browser.

That means PHP can't directly detect whether the useragent has a Java plugin installed or not. In theory, it could, if there was a relevant HTTP request header but there isn't.

At best, PHP can emit JavaScript which then runs some tests inside the browser and reports back to server via AJAX.

See How can I detect the Java runtime installed on a client from an ASP .NET website? for details.

Community
  • 1
  • 1
Saul
  • 17,973
  • 8
  • 64
  • 88
1

You are taking it wrong way.

It's not in PHP you have to detect if Javascript enabled, but in JS itself.

Just make your PHP always enabled, but if JS is present too, let it take some job. Easy

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
1

First, I think you are confused between Java and JavaScript. Lack of JavaScript is dealt with the <noscript> tag, but I guess this approach will work fine for "Java applets" as well.

The key idea here, is that the only information PHP has about the browser is the "User Agent" string. This specifies which browser this is. The process of making a website work well with or without Javascript is called "Graceful Degradation". You can read about it elsewhere.

There is no way for the PHP to tell whether JavaScript is enabled or not. The least complicated workaround is to create a test page, which looks like this:

<html>
<script type="text/javascript">
     window.location.href = "myapp.php?js=enabled";
</script>
<noscript><a href="myapp.php?js=disabled">Click here to continue</a></noscript>

this would allow you to say $javascript_enabled = $_GET['js'] === 'enabled'; and determine whether JS is enabled or not.

Note that this is a very wrong method of building applications. You need to account for both cases.

antileet2
  • 323
  • 1
  • 2
  • 7
  • Thank you for all your replies. I consider them and act accordingly. – muttley8226 Sep 10 '11 at 18:02
  • I have solved it differently. I used jquery and the show() hide() APIs. When java is enabled it shows and hides certain div's, otherwise only the non java one shows. So I do not need to check whether java is enabled or not. – muttley8226 Sep 11 '11 at 13:08