Page 1 of 1

Javascript checking

Posted: Thu Jan 05, 2006 8:18 am
by robokoder
Is it possible to check if the client has javascript enabled with php?

The only idea that I've come up with is forwarding the user to the real script, and posting an error message if they weren't forwarded. However, it's pretty simple just to look at the address bar after you've been forwarded, and go there directly without javascript next time!

Please help

Posted: Thu Jan 05, 2006 8:27 am
by feyd
If I need to detect Javascript being enabled, I hook it into an image that's on all pages, or can be hooked in by the php script it requests. Something like this:

Code: Select all

<script language="Javascript">document.write('<img src="someImageScript.php?j=1" />');</script><noscript><img src="someImageScript.php" /></noscript>
The script simply sets a session variable that the user has Javascript enabled and kicks out the required image.

Code: Select all

if(isset($_GET['j']) and $_GET['j'] == 1) {
  $_SESSION['JSenabled'] = true;
} else {
  $_SESSION['JSenabled'] = false;
}

header('Content-type: image/jpeg');
header('Content-length: '.filesize('image.jpg'));
readfile('image.jpg');
However, I try to write all my scripts to not absolutely require Javascript. Obviously, certain things are fairly unavoidable, such as Ajaxed applications...

Posted: Thu Jan 05, 2006 8:38 am
by robokoder
Thanks, I'll do that.

I have to use javascript as part of my authentication (ensuring people don't log in from proxies)

Posted: Thu Jan 05, 2006 9:38 am
by timvw
And would javascript help you to prevent ppl from logging in through proxies?

Posted: Thu Jan 05, 2006 3:32 pm
by alex.barylski
feyd wrote:If I need to detect Javascript being enabled, I hook it into an image that's on all pages, or can be hooked in by the php script it requests. Something like this:

Code: Select all

<script language="Javascript">document.write('<img src="someImageScript.php?j=1" />');</script><noscript><img src="someImageScript.php" /></noscript>
The script simply sets a session variable that the user has Javascript enabled and kicks out the required image.

Code: Select all

if(isset($_GET['j']) and $_GET['j'] == 1) {
  $_SESSION['JSenabled'] = true;
} else {
  $_SESSION['JSenabled'] = false;
}

header('Content-type: image/jpeg');
header('Content-length: '.filesize('image.jpg'));
readfile('image.jpg');
However, I try to write all my scripts to not absolutely require Javascript. Obviously, certain things are fairly unavoidable, such as Ajaxed applications...
Hmmm....clever approach...simple and effective :)

Posted: Fri Jan 06, 2006 5:16 am
by robokoder
See, I am trying to ensure that people only use a server-intensive php script once per 2 mins.

My hosts are complete XXXXs and they shut down my entire site when too many people used a script on it! I am basing my recording on IPs, and javascript allows me to ensure that their page location is within somesite.com, rather than proxysite.com/.../somesite/ which would let them use a different IP.

However, if the proxy disables javascript, I can't check, so I am using this as a method of making sure that they have javascript so I can check they are using their real IP.

Granted its not foolproof, but it will serve its purpose fine!

Posted: Fri Jan 06, 2006 5:59 am
by robokoder
Man, this is frustrating.

I've just realised that I don't know how to get the session var accross the page.

See, this is how it works:

html page with form and image to set session var
php page that checks the session var.

If the php script is script.php, should the form go to script.php?sid or what?!

Please help!