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!
<?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.
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:
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:
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.
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.
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.
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.
<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>
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.
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:
<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>
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.