Jump to content

PLEASE HELP! Need to redirect only ONE time if javascript=off


ShadowIce

Recommended Posts

I can't get this code to stop redirecting more than one time if javascript is off

 

<?php
ob_start();
?>
<noscript>
<?php
header('Location: enable-javascript.php');
?>
</noscript>
<html>
<head><title>Test</title></head>
<body>
blah
</body>
</html>

 

and dont tell me to use metas because they redirect WAY too slow even if i set it to 0. the whole point is to keep the user from seeing index.php at all if js is off

Link to comment
Share on other sites

This is not possible with PHP what you are trying to do. As PHP will execute the code inside the <noscript> tag since it does not recognize or execute javascript. PHP simply processes code at the server level then gives the display to the browser for interpretting which is where it will be detected if Javascript is off.

 

http://www.inspirationbit.com/php-js-detection-of-javascript-browser-settings/

 

Is one way of doing it. I am sure there are tons of different ways like that to do it. But using that script I would do as team has suggested and set a session variable once you figure out if JS is on instead of having every page constantly testing by echoing out a form. That is only needed once. 

Link to comment
Share on other sites

The problem is.. you need the browser to load FIRST before you can determine whether or not JS is enabled..

You cannot get PHP which is server side to redirect on knowledge acquired via client side data..It just wont happen..

Unless SOMETHING tells PHP that JS is OFF

 

You would need a bridging page of sorts that tests for JS and passes a value to PHP to let it know that Js is disabled..

Link to comment
Share on other sites

would this help?

 

if (isset($_POST['jstest'])) {
  $nojs = FALSE;
  } else {
  // create a hidden form and submit it with javascript
  echo '<form name="jsform" id="jsform" method="post" style="display:none">';
  echo '<input name="jstest" type="text" value="true" />';
  echo '<script language="javascript">';
  echo 'document.jsform.submit();';
  echo '</script>';
  echo '</form>';
  // the variable below would be set only if the form wasn't submitted, hence JS is disabled
  $nojs = TRUE;
}
if ($nojs){
  //JS is OFF, do the PHP stuff
}

Link to comment
Share on other sites

here's what i have:

 

index.php:

 

<?php
ob_start();
?>
<?php
if (isset($_POST['jstest'])) {
  $nojs = FALSE;
  } else {
  // create a hidden form and submit it with javascript
  echo '<form name="jsform" id="jsform" method="post" style="display:none">';
  echo '<input name="jstest" type="text" value="true" />';
  echo '<script language="javascript">';
  echo 'document.jsform.submit();';
  echo '</script>';
  echo '</form>';
  // the variable below would be set only if the form wasn't submitted, hence JS is disabled
  $nojs = TRUE;
}
if($nojs){
header('Location: enable-javascript.php');
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>test</title>
<body>
blah
</body>
</html>
<?php
ob_end_flush();
?>

Link to comment
Share on other sites

I went to the link premiso suggested and I like that solution. I use a cookie set/check on some sites and am going to switch the code over to the js sent form.

 

shadowice:

stop and think about it. You get a blank page; you have js turned on...it works. Add an else to the if and see what happens

if($nojs){
header('Location: enable-javascript.php');
}
else {echo "JS is on";}

 

 

HTH

Teamatomic

Link to comment
Share on other sites

I used this:

<?php
if (isset($_POST['jstest'])) {
  $nojs = FALSE;
  } else {
  // create a hidden form and submit it with javascript
  echo '<form name="jsform" id="jsform" method="post" style="display:none">';
  echo '<input name="jstest" type="text" value="true" />';
  echo '<script language="javascript">';
  echo 'document.jsform.submit();';
  echo '</script>';
  echo '</form>';
  // the variable below would be set only if the form wasn't submitted, hence JS is disabled
  $nojs = TRUE;
}
if ($nojs){
  echo "NO JS";
}
else
{
echo "YES JS";
}
?>

and checked it by turning JS on/off via the mozilla addon webdeveloper, and it does work as advertised.

 

 

HTH

Teamatomic

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.