Javascript checking

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
robokoder
Forum Newbie
Posts: 13
Joined: Wed Jan 04, 2006 9:23 am
Location: London, UK

Javascript checking

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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...
robokoder
Forum Newbie
Posts: 13
Joined: Wed Jan 04, 2006 9:23 am
Location: London, UK

Post 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)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

And would javascript help you to prevent ppl from logging in through proxies?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :)
robokoder
Forum Newbie
Posts: 13
Joined: Wed Jan 04, 2006 9:23 am
Location: London, UK

Post 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!
robokoder
Forum Newbie
Posts: 13
Joined: Wed Jan 04, 2006 9:23 am
Location: London, UK

Post 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!
Post Reply