1

I'm trying to unobtrusively detect when JavaScript is disabled by having an image inside of a noscript element. I'm pretty sure I'm on the right path though I'm not sure about what data I should be echoing exactly.

<noscript><img alt="" src="images/noscript.gif" /></noscript>

The base64 encoded data below is simply a 1x1 transparent GIF image.

 header('HTTP/1.1 200');
 header('Content-type: image/jpeg');
 echo 'data:image/gif;base64,R0lGODlhAQABAIAAAP8A/wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==';

What do I need to do to ensure I can have PHP intercept the image request and have the image successfully be displayed on the page (obviously while JavaScript is disabled)?

John
  • 1
  • 13
  • 98
  • 177
  • `src="images/noscript.php"` --> handle request --> set headers and content (so, return image). – Cerbrus Jun 30 '14 at 11:52
  • possible duplicate of: http://stackoverflow.com/questions/121203/how-to-detect-if-javascript-is-disabled – Naruto Jun 30 '14 at 11:53
  • I already have the Apache rewrite targeting the URL correctly, I just need to echo the data correctly. – John Jun 30 '14 at 11:53
  • What about simply `` ? – Sebastien C. Jun 30 '14 at 11:53
  • @Naruto This is not a duplicate; this is a SPECIFIC question, not some blind "I'll take anything I can get" question. – John Jun 30 '14 at 11:54
  • @sebcap26 No, because there is no request from the client to the server. – John Jun 30 '14 at 11:54
  • The real question is really: what is it you're trying to achieve? You're not displaying the image just for fun, but to "detect" that JavaScript is disabled. So you want to do something different when that is the case. But what? – jcaron Jun 30 '14 at 12:00
  • @jcaron It is for logging purposes; I detect browsers via the DOM since user agents can be [whats the darn word I can't remember right now that means faked?] and send the info via an AJAX request. In the absence of that I need to have the browser marked as "JavaScript Disabled" to know that statistical data is not as accurate and dependable as DOM detection (e.g. being forced to detect via user agent). – John Jun 30 '14 at 12:07
  • Well then just put the path to your php script as the `src` of the ``, and do whatever you want in your script. Also fix your `Content-Type` (set it up `image/gif`), and returns the base64-decoded version of the image. – jcaron Jun 30 '14 at 12:14

3 Answers3

2
<noscript><img alt="" src="path_to_script.php"/></noscript>

Script:

header('HTTP/1.1 200');
header('Content-type: image/gif');
echo base64_decode('R0lGODlhAQABAIAAAP8A/wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');
jcaron
  • 17,302
  • 6
  • 32
  • 46
  • Sorry, beatcha to it. :P I'll up-vote you for the effort though once I can accept my own. I was on the right path, I just was forgetting about the decode. – John Jun 30 '14 at 12:20
0

You could just make the src attribute the link of the PHP file:

<noscript><img alt = "" src = "[stuff].php" /></noscript>

Also, I don't know how PHP servers work, but in Python TCPServers and UDPServer, the server is run by a class with this handle() method that takes in data from the client and then sends data back. That data can be completely independent then the actual file from that link.

If this is the same in PHP, then you could get a request for a link from images/noscript.gif, not even have a images/noscript.gif file, and then just send back the data from the PHP file.

Noble Mushtak
  • 1,784
  • 10
  • 22
  • This won't work, the image data is already in the `src` attribute thus *no request to the server* will be made which is key to recording it on the server. That is why I'm using the server and client combined. I'm not going to down-vote this though you may want to recant your answer before someone else does since I clearly responded to this recommendation in comments. :-| – John Jun 30 '14 at 11:59
  • 1
    OK, but why not make the `src` attribute the PHP file? If that outputs an image, that should work. – Noble Mushtak Jun 30 '14 at 12:03
0

Ah, I forgot I can decode the image at the server. The key is having the client make an HTTP request otherwise the server won't be aware and thus can not log that JavaScript is disabled.

<?php
header('HTTP/1.1 200');
header('Content-type: image/gif');
echo base64_decode('R0lGODlhAQABAIAAAP8A/wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');
?>
John
  • 1
  • 13
  • 98
  • 177