Click to See Complete Forum and Search --> : php script to check if cookies or javascript are enabled.
Ness_du_Frat
05-26-2005, 01:13 AM
Hello !
I'm looking for a script in php that will test the user's browser, to see if he has cookies and javascript enabled.
What I want to do is redirect him to a page that doesn't need cookies and js, or redirect him to a page with javascript.
By doing some search on google, I found this code by pyro, which is exactly what I need, ^but the javascript test doesn't seem to work ( in three different browsers, all having javascript enabled and cookies too, it returns that cookies or javascript are disabled. By testing it pieces to pieces, I found out that the code for the cookies works perfectly, but not the code for javascript ).
Can anyone help me out with this one ?
<?PHP
setcookie("cookies","yes",time() +"3600");
$js = 0;
$cookies = 0;
if (isset($_GET["js"])) {
$js = 1;
}
if (isset($_COOKIE["cookies"])) {
$cookies = 1;
}
if ($cookies == 0 || $js == 0) { #in english: if $cookies equals zero or $js equals 0
header("Location:http://www.livejournal.com");
}
else { #if js and cookies are enabled
header("Location:http://www.yahoo.fr");
}
?>
<html>
<head>
<script type="text/javascript">
if (!window.location.search.substr(1)) {
window.location.href = "test.php?js=yes"; //set test.php to the name of this page...
}
</script>
</head>
<body>
</body>
</html>
thanks a lot ! Ness
NogDog
05-26-2005, 01:35 AM
Haven't had cause to try them, but there are a couple scripts here:
http://techpatterns.com/downloads/php_browser_detection.php
Ness_du_Frat
05-26-2005, 01:53 AM
The page you provided is quite cool ! You can do a lot of detections, like the os, the resolution, the browser type etc...
Unfortunately, the cookie and javascript parts are done with javascript... :(
( or maybe only the cookie part, I don't know.... but still, I need it all with php.... )
Do you have an idea why pyro's script doesn't work ?
Ness_du_Frat
05-27-2005, 05:38 AM
anyone ? :confused:
Stephen Philbin
05-27-2005, 05:48 AM
Well I'm assuming you're having two different pages for the user depending on whether they've got Javascript enabled or not right? So why bother finding out whether the browser has js or not and then have php decide where to from there at all?
why not just have
<?PHP
all yer initial php gubbing here
?>
<html>
<head>
<script type="text/javascript">
window.location = 'http://www.mysite.com/javascript_enabled_version.html';
#Or whatever it is for a Javascript redirect
</script>
</head>
<body>
Stick yer Javascript disabled page stuff here so this is your actual Js disabled page.
</body>
</html>
Ness_du_Frat
05-27-2005, 05:54 AM
hehe, not stupid !!!
I thought about that too, but then... I just forgot about it, silly me !
But anyway, I am just curious. Why doesn't the script work ? ( I might need it further in the website, for another javascript detection, and this time, I won't be able to redirect the page, I'll just need to know if js is enabled or not... )
Stephen Philbin
05-27-2005, 06:02 AM
Not really sure. I avoid the use of Javascript as much as possible and so don't know many of its objects or methods. So I don't recognise the search property of location and thus don't know what it is that that if statement is looking for the absence of. Looks like it does pretty much the same thing as the one I posted does though actually. Only mine is much more simple and would redirect from the browser supporting just about any form of Javascript regardless of whether the browsers implementation of Javascript would be able to perform the scripts you have intended for it or not.
Ness_du_Frat
05-27-2005, 06:21 AM
I see only one problem with your script, it's how I'm going to do it with the cookies, so that :
1.if cookies disabled, you go straight to the php page, no need to even think of having a look at the javascript to see if it's deisabled or not.
2.if cookies are enabled, you have a look at the js, and if the js is disabled, you stick to the php page.
Number 2 is easy, but I'm not sure I know how to implement number 1...
Ness_du_Frat
05-27-2005, 06:29 AM
mmm, drop it, I think I know how to do it...
Tell me if I'm wrong...
<?PHP
setcookie("cookies",time() +"3600");
$cookies = 0;
if (isset($_COOKIE["cookies"])) {
$cookies = 1;
}
if ($cookies == 1) {
printf "<script type=\"text/javascript\">
window.location = \'http://www.mysite.com/javascript_enabled_version.html'\;
</script>";
}
else {
//the rest of my page comes here
}
?>
I'm not 100% sure of the printf synthax, I'm not really advanced in php, but I guess something in this style ( with the right synthax ) would do the trick. What do you think ?
Also, is it possible to bypass the else thing, or make it do nothing ? As I think it will be easier for me to close the conditional statement, and start with the page in pure html, without having to care for the 10 000 " and ' and all...
Ness_du_Frat
05-27-2005, 08:28 AM
I got it working by doing that :
<?PHP
setcookie("cookies",time() +"3600");
$cookies = 0;
if (isset($_COOKIE["cookies"])) {
$cookies = 1;
}
?>
<html>
<head>
<?
if ($cookies == 1) { #in english: if $cookies equals zero AND $js equals 0
echo "<script type=\"text/javascript\">
window.location = 'http://www.yahoo.fr';
</script>";
}
else {
}
?>
</head>
<body>
c'est moi !!!!!
</body>
</html>
Just test things for the moment...
But, it works !!!
Thankssss !!! :p
Ness_du_Frat
05-27-2005, 04:01 PM
Ok, so the simple redirection works fine, but I have a big ( expected ) problem with this one :
<?PHP
setcookie("cookies",time() +"3600");
$cookies = 0;
if (isset($_COOKIE["cookies"])) {
$cookies = 1;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<?
if ($cookies == 1) {
echo "<script src=\"getcookiemain.js\" type=\"text/javascript\"></script>
<script type=\"text/javascript\">
document.write('<LINK REL=stylesheet HREF=\"'+parent.styleSheet+'\" TYPE=\"text/css\">')
</script>";
}
else {
echo "<p>blablabla</p>";
}
?>
</head>
<body>
... and the rest of the website here...
as I expected it, if both cookies AND javascript are disabled, it's fine.
It returns the "blablalba" thing ( which will be replaced by somthing else, of course ).
But, if cookies are activated, and javascript is disabled, then, it doesn't return the javascript thing ( which styles up the document ), which is normal, but then, it doesn't return the "blablabla" thing, which is not so fine... ( but which is logical, as you can see from the script )
So, I'd like to have a solution that, if javascript is disabled or if cookies are disabled, it returns the blabla thing...
I really need your help, please !!!! :(
Stephen Philbin
05-27-2005, 04:15 PM
Why not do it in a different order.
Instead of setting the cookie and then checking for the cookie and then checking the javascript, try setting the cookie, then checking the Javascript and then checking the cookie. That way, you'll already be at the page you want to be because of your javascript and you can then find the cookie setting for that specific page. If you check cookies and then do the Javascript, then you'll find out if the user has javascript, but then lose the info you had about cookies when you change pages. So changing pages then checking cookies should do better.
Ness_du_Frat
05-27-2005, 04:24 PM
thanks for the quick reply !!!
This one is different. The one we did together was for the first page, to redirect the user to a page with javascript or without javascript.
I don't care if javascript is enabled or disabled, because as long as the cookies are disabled, the user needs to go to the alternate page, the one without the javascript. So, the order is fine.
But, the example I'm talking about doesn't redirect a user to anythibg at all.
If cookies are enabled, it should test for javascript, and then, provide the js code.
If cookies are disabled, it should load the alternate stuff...
My problem is, if cookies are enabled and javascript disabled, it won't load the alternate stuff, because it only checks for the cookies, and then, load a javascript script. If the javascript cannot be loaded ( as in the redirecting example ), it should simply loads the other one. But I don't know how to specify that.
In clear : cookies enabled + javascript enabled : load javascript css and styling.
cookies enabled + javascript disabled : load alternate css and styling.
cookies disabled + javascript enabled OR disabled : load alternate css and styling.
So, it's only when cookies AND javascript are enabled that it should load the javascript part...
But as long as I don't have something to check the javascript status, I cannot do any conditional statement... :( :(
comptech520
01-17-2008, 08:38 AM
How can I make this so:
If Javascript enabled and cookies enabled display login.php
If Javascript disabled and cookies enabled display jsenable.php
If Javascript enabled and cookies disabled display cookiesenable.php
If Javascript disabled and cookies disabled display jscookiesenabled.php
comptech520
01-17-2008, 01:32 PM
I tried this and it didn't seem to work:
if ($cookies == 0 || $js == 0) {
header("Location:http://www.mydomain.com/enablecookiesjs.php");
}
if ($cookies == 1 || $js == 0) {
header("Location:http://www.mydomain.com/enablejs.php");
}
if ($cookies == 0 || $js == 1) {
header("Location:http://www.mydomain.com/enablecookies.php");
}
else { #if js and cookies are enabled
header("Location:http://www.mydomain.com");
}
That didn't work, does anyone have anything that could complete this?
Thanks
bluejon
06-05-2008, 05:18 AM
There is code for detecting javascript with php @ http://snippets.bluejon.co.uk
blujon
moise76
06-17-2008, 11:27 PM
I have been using the following to test if JavaScript is enabled in a webpage. If not you can forward the user to another page where a php file include that you have added to all of your webpages can be used to test if cookies are enabled; (using the php script as above).
Javascript enabled page?:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Title</title>
<noscript>
<meta http-equiv="refresh" content="0;url=http://www.domain.com/errorPage.php"/>
</noscript>
<?php include("/directory/cookiesEnabled.php"); ?>
</head>
<body>
</body>
</html>
The META tag refresh will only work if script has been disabled in the browser because of the <NOSCRIPT> tags. And the php file include would also be added to the errorPage.php to test if cookies are enabled.
It works but I haven't tested it extensively. Enjoy!