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
Javascript checking
Moderator: General Moderators
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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:The script simply sets a session variable that the user has Javascript enabled and kicks out the required image.
However, I try to write all my scripts to not absolutely require Javascript. Obviously, certain things are fairly unavoidable, such as Ajaxed applications...
Code: Select all
<script language="Javascript">document.write('<img src="someImageScript.php?j=1" />');</script><noscript><img src="someImageScript.php" /></noscript>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');-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Hmmm....clever approach...simple and effectivefeyd 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:The script simply sets a session variable that the user has Javascript enabled and kicks out the required image.Code: Select all
<script language="Javascript">document.write('<img src="someImageScript.php?j=1" />');</script><noscript><img src="someImageScript.php" /></noscript>However, I try to write all my scripts to not absolutely require Javascript. Obviously, certain things are fairly unavoidable, such as Ajaxed applications...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');
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!
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!
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!
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!