Page 1 of 1
Getting Javascript status into $_SESSION array
Posted: Mon Jul 05, 2004 5:47 pm
by Bill H
I am trying to detect whether the user has Javascript enabled and set a session variable accordingly, but not having any luck.
Code: Select all
<?php
// Page to redirect users entering website based on
// presence or absence of active javascript
session_start();
$_SESSION['Javascript'] = 1; // assume it is enabled
echo "<script>location.replace('home.php')</script>";
// if the above did not redirect, then it is not enabled
$_SESSION['Javascript'] = 0; // set it as disabled
header("Location:home.php");
?>
Arrives at "home.php" with $_SESSION['Javascript'] set at zero when Javascript is enabled.
Posted: Mon Jul 05, 2004 6:59 pm
by feyd
The header "location" will get processed prior to the echo (if you don't get a warning first). What I've done in the past is call an image or something the client has to request using javascript with a flag like so:
Code: Select all
<script language="Javascript">document.write('<img src="someimage.php?js=1" />');</script><noscript><img src="someimage.php?js=0" /></noscript>
Have someimage.php set the session variable accordingly.. You can also script the pages to be tolerant of it being off, in most cases...
Posted: Tue Jul 06, 2004 9:45 am
by Bill H
Thanks, it works but leaves a missing image symbol on the page. I imagine you meant to use a page which would do that thing and then redirect to the "home page" of the site, but I'm not having much luck doing that. Can't use header() since I've already sent output. I have tried:
Code: Select all
<meta http-equiv="refresh" content="url=home.php">
and that isn't doing it either.
A minor objection is that this requires three files to display the first web page., but I can certainly live with that if it gets the job done.
Posted: Tue Jul 06, 2004 10:15 am
by feyd
no, my solution does not use redirection. someimage.php is supposed to send an image you choose. someimage.php is supposed to set the session variable for javascript being enabled or not too.
Posted: Wed Jul 07, 2004 8:34 am
by Bill H
someimage.php is supposed to send an image you choose.
And how does it do that? It's setting the session variable, but not inserting the image code into the html page.
Code: Select all
<?php
session_start();
$_SESSION['Javascript'] = $_GET['js'];
echo "<img src='dot.gif' border=0>";
?>
Posted: Wed Jul 07, 2004 12:13 pm
by feyd
Code: Select all
<?php
session_start();
$_SESSION['Javascript'] = (bool)( !empty( $_GET['js'] ) ? $_GET['js'] : 0 );
header('Content-type: image/gif');
readfile('dot.gif');
?>
Posted: Wed Jul 07, 2004 6:01 pm
by Bill H
This is just insane -- I have been fooling around with this for over a week on three different forums. You are coming closer than anyone else, but it fails on the first hit, and works on the second hit. I'll post the link here so you can look at it and see what I mean. You can see the source by viewing after the page loads. The text above the image is blue if $_SESSION['Javascript'] is set and is greater than zero and red otherwise.
Go to:
http://sonora-sw.com/kpv/index.php
The someimage.php is thus:
Code: Select all
<?php
session_start();
if (!isset($_SESSION['Javascript']))
$_SESSION['Javascript'] = $_GET['js'];
header('Content-type: image/gif');
readfile('dot.gif');
?>
Posted: Wed Jul 07, 2004 6:12 pm
by Weirdan
so you can have the gateway page which calls the someimage.php and have meta refresh to the actual page.
Posted: Wed Jul 07, 2004 7:35 pm
by Bill H
I've tried that without success. Can't use headers, since output has already been sent to the browser, and half a dozen people on three forums have provided Javascript and/or html code to do that, none of which has worked.
I know php rather well, html more than a little, and almost nothing about Javascript. I have a third-party application that generates Javascript menus and the HOA wants me to use it for their site, but it has to be selective for people who have JS turned off. Fortunately I haven't agreed to that condition yet, since I didn't realize that it would border on impossible.
Silly me, I thought it would be relatively simple. Saved, in this case, by my untra-cautious nature. If I have to learn a lot of JS I will just tell the HOA to get somebody else. What I have seen of JS I dislike intensely.
Posted: Wed Jul 07, 2004 7:54 pm
by feyd
Code: Select all
<HEAD>
<TITLE>Kensington Park Villas Homeowners Association</TITLE>
<link href="kpv.css" rel="stylesheet" type="text/css">
<script language="Javascript">document.write('<img src="someimage.php?js=1">');</script>
<noscript><img src="someimage.php?js=0"></noscript>
</HEAD>
I'm not sure the image would even get called, it's inside the header.

Posted: Wed Jul 07, 2004 11:30 pm
by Bill H
I tried it with it in the body and it didn't work at all. With it in the header it works, but not when you initially hit the page, only when you refresh.
I have seen websites that had the ugly "This site requires Javascript. Click here if your browser does not support..." Now I know why they used such a cruddy method.
All the fancy flashing and dancing imagry, and it can't even do a simple thing like this. I knew there was a reason why I don't like JS. All glitz and no meat.
Posted: Thu Jul 08, 2004 11:31 am
by Weirdan
gateway page:
Code: Select all
<head>
<meta http-equiv="Refresh" content="2;actualpage.php">
</head>
<body>
<script language="Javascript">document.write('<img src="someimage.php?js=1">');</script>
<noscript><img src="someimage.php?js=0"></noscript>
Click <a href='actualpage.php'>here</a> if your browser does not redirect you automatically.
</body>
Posted: Thu Jul 08, 2004 6:02 pm
by Bill H
Thanks, Weirdan, that does set the session variable successfully, but it does NOT send the viewer to the next page unless the viewer clicks the link.
Is Javascript/HTML simply incapable of doing something so elementary as redirecting the viewer to a different webpage without user intervention? Amazing.
Posted: Fri Jul 09, 2004 9:02 am
by Bill H
My bad, Weirdan, it does kick to the next page after a delay. I think the delay is needed to assure time to process the image, so I added some info to the viewer:
Code: Select all
<head>
<TITLE>Kensington Park Villas Homeowners Association</TITLE>
<link href="kpv.css" rel="stylesheet" type="text/css">
<meta http-equiv="Refresh" content="2;home.php">
</head>
<body>
<script language="Javascript">document.write('<img src="someimage.php?js=1">');</script>
<noscript><img src="someimage.php?js=0"></noscript>
<center>
<span class='blue-14'><br><br><br><br><br>Testing your browser for compatibility.<br><br></span>
<span class='blue-8'>Click <a href='home.php'>here</a> if your browser does not forward you
automatically after a few seconds.</span>
</body>
and my problem seems to be solved. Thank you.

Posted: Fri Jul 09, 2004 10:01 am
by magicrobotmonkey
But what if they do have javascript enabled and click before someimage does it's work? Make sure the pages will deal with $_SESSION['Javascript'] not being set. You have to decide if you are going to default to javascript or no javascript. Otherwise, I'd say this is a pretty good method.