Can I check whether Java is enabled from PHP - php

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...

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.

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

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>Click here to continue</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.

Related

PHP include file based on screen size

<?php
include 'components/server.php';
Is it possible to make it include server.php for desktops and server-mobile.php for mobile devices?
While technically possible, it's absolutely not the best way of doing things.
Why?
Because PHP runs on the server and only the output of that PHP execution is given to the browser. You would probably be wanting something using javascript which can load and then seamlessly react to the browser conditions, such as screen size and/or dimensions.
If you're trying to change which PHP script is running based on the browser criteria (as mentioned above) this sounds very much like your programming logistics are simply wrong.
If you somehow really do need to change PHP script execution based on end-client (browser) characteristics you could do this by calling a script based on javascript AJAX or using mechanisms mentioned in comments above, but as said, you're almost certainly "doing it wrong".
Alternative
It would be far better to load everything you need in PHP and then pass all of that content to the browser (as output; HTML, CSS, Javascript, etc.) for the Javascript in the browser to then decide which parts of the data it needs to use and ignoring the others.

How do I constantly update a variable in PHP as a user changes input?

I looked for answers to this question, and it seems that most people have a specific problem. I'm looking for a more general answer. I have a text box where a user will type in their name and I would like to have PHP constantly monitor the text box and change the value of the $name as it is typed or edited.
I would also like to do the same with buttons, and as different buttons are clicked, the content of a variable would change to match that which the button represents. Basically, is there a way to get PHP to constantly run on the page and gather information from a user as it is changed?
It seems like it should be possible, but my experience with PHP is limited, and I'm not sure how to begin, so I don't have any code to really show.
This sounds to me like you require an ajax script checking for input changes(eg/ keystroke, on_blur or on_click for your buttons) and sending back to a php script that will update your variables/tables and return the new variables to the ajax script once they are updated.
1 - Ajax checking for changes on the page, and firing off to a php script on server.
2 - Have a method in your js that waits for the action to be completed and load the new variables into the HTML document.
Basically look up Ajax/PHP - Check username availability, Then adapt to your specific needs.
:)
Simple ajax script will be what your after, there are many scripts available for checking username availability --- As for a lone PHP script, this will not be possible as the PHP code has already ran on the server before the html document is rendered to the browser.
My first answer so my wording may not be perfect comment back if i have confused you more.
It seems like it should be possible, but my experience with PHP is limited, and I'm not sure how to begin, so I don't have any code to really show.
Yes, it's possible, but most likely only due to the nature that the browser (e.g. Firefox) and PHP itself are Free Software.
A proof of concept is missing so far, so you really need to start at a very basic level.
You can download these software packages and modify them to your needs, e.g. make the browser interactively corresponding to the DOM and DOM events process PHP scripts that you embed like script tags inside HTML.
But well, as you wrote, you're starting, so I guess, you don't want to start with rewriting the browser and the PHP interpreter, so even if possible, it's perhaps best to stick that interactive part inside the browser to Javascript and some HTTP request / response programming on the server with PHP.

php communicate with html and javascript

I heard people said php server site script cannot communicated with html and javascript client-side script. But when I test it, seem php can tell html and javascript what to do. Here is my codes :
<?php if ((isset($username1))&&($username1 == $username2)){ ?>
<div style="position:relative;">
<img src="http://plekz.com/images/layouts/theme.png" onClick="showThemeDiv(); hideThemeTip();" style="margin-bottom:3px; margin-left:1px; position:relative;" onMouseOver="showThemeTip();" onMouseOut="hideThemeTip();" />
</div>
<?php } ?>
I tested it on IE, Firefox and Chrome, all works perfectly. But I still worry code this way will lead to problem when I move all the file to other online webhosting/server... Do I need to put all html and javascript codes into php echo? Or I can just code like this without having any problem in future? Is it standard way to code like this?
What you are doing is just PHP, in your code there is no actual communication between "html and javascript", which is on the client's side. And PHP, which is on the server side.
What people mean with communication is that the user can change something on the webpage dynamically without reloading the webpage. Such a thing can be done using AJAX.
In short, the code you are using will work on any webserver and with any browser.
It's a bad way when html and php code are in one file and much better when separate. But you can code like this and should not put all html to php function 'echo'. It's the same instead you don't need to trigger the php function. And more much understandable to read a code, IMHO.
PHP is simply a scripting language which gets interpreted at runtime whenever a request is made; people say it cannot communicate with HTML/Javascript because it is interpreted on the server side, it is not visible/accessible to the user agent once the page is rendered.
It is however possible to communicate with PHP scripts through AJAX calls.

Can I create a variable with JavaScript that PHP can recognize?

I have a series of PHP statements that I only want to run if javaScript is enabled.
if($js_enabled == 'yes') {
//Run a series of PHP statements
}
My problem is that I want to create that $js_enabled variable with javaScript. Therefore, if javaScript is enabled, the variable will be created and the PHP statements will be run, but if javaScript is not enabled, the variable will never be created and the PHP statements will not run.
How can I create a variable with JavaScript that PHP can recognize?
You can do
$browser = get_browser();
if ($browser['javascript'] == 1) {
// your code here
}
Please read the documentation for get_browser for caveats, especially
Note: In order for this to work, your browscap configuration setting in php.ini must point to the correct location of the browscap.ini file on your system.
browscap.ini is not bundled with PHP, but you may find an up-to-date ยป php_browscap.ini file here.
While browscap.ini contains information on many browsers, it relies on user updates to keep the database current. The format of the file is fairly self-explanatory.
Also checkout the link given in the comments to get_browser:
http://code.google.com/p/phpbrowscap/
EDIT As you correctly point out in the comments, the above will not tell you if JavaScript is enabled but just whether the browser is capable of running it. Finding out about the clientside from the serverside is impossible, because the serverside runs first. If anything, inform the serverside after a page has been served, e.g. like #mck89 suggested or simply set a cookie you can read on each subsequent request.
But generally speaking, if your site requires JavaScript enabled, you should simply add a message to inform the user about this requirement, e.g.:
<noscript>This page requires JavaScript to run properly</noscript>
In other words, don't check from PHP. Just set every variables the browser might use, if JavaScript is enabled and consider using graceful degradation or progressive enhancement.
Why don't you simply an ajax request? If javascript is disabled the ajax request can't be done so the PHP script will not be executed, if javascript is enabled the ajax request starts the execution of the PHP script.
You can store that value in a hidden field. and check it on server side.
If i would need it that badly I would use JavaScript to set a form variable (prefferably hidden) on page before and then check it when processing form.. Otherwise i would make my pages work without JS...
This can't be done. PHP is server side while javascript is interpreted only after the php interpretation has already been done and the code already sits in the user's browser. Try a workaround using ajax. if javascript_enabled -> call with ajax some php page.

Detecting AdBlocking software?

I was recently visiting a site and noticed that the page had a section that said it noticed that I was using AdBlocking software and could I kindly turn it off to help support a small site like that.
I was just wondering how you would do that? Would it be best done client-side or server-side?
This is something that simply can't be done server side - there's zilch reason for person to knock on your door and say "Look at me, I have AdblockPlus!". When on the client side, adblock is actively trying to influence the page content, which is something you can see happen and see that they are using an adblocker.
Anyway, I happened to know that newgrounds.com is doing this too. (their new layout was screwed up for adblock plus users - as a response they made a contest for the best "if you're not going to help us through our ads, go and buy something in the store"-banner.
A quick look in the source of newgrounds told me they are doing this with some simple javascript.
First in the document:
var user_is_leecher = true;
Next there is a external script tag: src=checkabp?thisistotrickabp=***adress of ad affilliate***
Now the joke: they simply trust adblock plus to filter that script out, as all that's in there is: user_is_leecher = false;
From there, they can do just about anything.
All off the methods mentioned here rely on the ad blockers to strip out code. This doesn't work for some adblockers(like NetBarrier on Mac). You also have to keep updating your code when the adblockers catch on.
To detect if the user is blocking ads, all you have to do is find a function in the ad javascript and try testing for it. It doesn't matter what method they're using to block the ad. Here's what it looks like for Google Adsense ads:
if(typeof(window.google_render_ad)=="undefined")
{
//They're blocking ads, do something else.
}
This method is outlined here: http://www.metamorphosite.com/detect-web-popup-blocker-software-adblock-spam
You could do it on server side by pairing requests for html pages and for the acording ads (probably with some unique identifiers to each request ...) ... But this is just an idea, i've never tried it and never even seen it used.
I found this part in the code which seems to look like how they did it:
/*MOOTOOLS*/
window.addEvent('domready', function(){
$$('.cat-item').each(function(el) {
var fx = new Fx.Morph(el,{ duration:300, link:'cancel' });
el.addEvents({
'mouseenter': function() { fx.start({ 'padding-left': 25 }); },
'mouseleave': function() { fx.start({ 'padding-left': 15 }); }
});
});
if ($$(".google-sense468")[0] && $$(".google-sense468")[0].clientHeight == 0 && $('block-warning')) $('block-warning').setStyle('display','block');
});
/*MOOTOOLS END*/
I guess there are several ways of doing it, but probably the easiest one would be to have some kind of background image, or text, that will be replaced when the ad is loaded. Thus, if the ad gets loaded, you see the ad. If the ad doesn't load, you see the text.
This example would be client side, done by either JavaScript or just plain CSS might even suffice.
There might be some server-side gimmicks that could do this too, but they would be unnecessarily elaborate and clunky. One method that springs to mind would include some kind of API with the advertiser that could be asked "did the user from IP such.and.such load any images?" and in that way get the answer. But I doubt there's such services - it would be much easier to do on the client side.
I believe that is much easier to do it on client side than in server side. Ad blockers are installed on the client, so they can manipulate DOM and block ajax requests. That's why I believe it makes more sense to detect on the client than on the server.
Anyway, this is a standalone simple plugin that detects users with ad blockers enabled, it's open-source and the full code is on github:
https://github.com/retargetly/mockingbird
It's more publisher oriented so they can easily show messages on the ads containers or in a popup. The plugin is frequently updated, and it's worth a try. This is the fiddle also:
http://jsfiddle.net/retargetly/9vsha32h/
The only method you need to use is
mockingbird.adsBlocked(Obj)
The call can be done anywhere in the code and you don't need jQuery to make it work.
Wish you luck !
I don't think there is an easy way to do this. What you can do is to create "trap". Make a php script listen to a very obvious url like yourdomain.com/ad.png. You can probably achieve this by url rewriting. If this page is loaded you can note this in a session variable and send back a 1x1 blank png.
On the next request you can see whether ad.png has been loaded. If it hasn't you can guess that the client is using some form of AdBlock software. Make sure you set the appropriate http headers to prevent clients from caching "ad.png".
This is the only server side approach I can think of at the moment and it has some flaws.
The png file can be cached regardless of the http headers
This will not work for the first http request
Some extra server load as browsers will keep hitting ad.png for each request
That the image gets loaded from the server is no guarantee for it actually being displayed
Probably more side effects that I haven't thought of
Please make a comment on this post if you decide to try it out.
Regarding a client side solution. This shouldn't be to difficult. You can create a tiny Javascript to run on page load complete. This script can check that the page contains the dom-nodes holding the ads. If you this when the page is loaded completely (not only the dom) you can check the width and height of your ad images. The most obvious drawback with this solution is that clients can disable javascripts.
A few good answers here, so I'll just add this:
use some ad management system (You can write Your own). With that, track every ad that's being displayed (and make it obvious, like ads.php or showad.php or whatever). If that script is never called, the user is using SOME form of ad blocking software.
Be sure to handle each and every ad through that handler, though. Mod_Rewrite isn't required, it can be done using simple PHP.
What you can do to detect the adblocker on the server-side is somithing like:
<?php
header('Content-Type: application/javascript');
//Save it to session
session_start();
$_SESSION['noAdblocker']=true;
?>
noAdblocker=true;
Save this file as ads.php
Now the index.php:
<?php
session_start();
$_SESSION['noAdblocker']=false;
?>
<!DOCTYPE HTML><html><head>
<!-- Now place the "ad-script" -->
<script src="ads.php"></script>
</head><body></body></html>
You can add javascript-code to your page, that is only executed if there's no adblocker, e.g. use "ad" as variable-name, use "ad.js" as file-name.
This code sends an ajax-event to the server, saying "this user doesn't use an adlocker". So if you don't receive that event, you know, that this user is blocking ads or even javascript altogether.

Categories