简体   繁体   中英

PHP detect disabled JavaScript using image request

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)?

<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==');

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.

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==');
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM