ShadowIce Posted February 7, 2010 Share Posted February 7, 2010 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 Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 7, 2010 Share Posted February 7, 2010 Something like this should work. <?php session_start(); ob_start(); ?> <noscript> <?php if ($_SESSION['redirect'] != 'no') { $_SESSION['redirect']='no'; header('Location: enable-javascript.php'); } ?> HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
premiso Posted February 7, 2010 Share Posted February 7, 2010 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. Quote Link to comment Share on other sites More sharing options...
ShadowIce Posted February 7, 2010 Author Share Posted February 7, 2010 this is what i have, and it still wont work: index.php: <?php session_start(); ob_start(); ?> <noscript> <?php if ($_SESSION['redirect'] !== false) { $_SESSION['redirect']=false; header('Location: enable-javascript.php'); } ?> </noscript> Quote Link to comment Share on other sites More sharing options...
premiso Posted February 7, 2010 Share Posted February 7, 2010 This is not possible with PHP what you are trying to do. You seem to ignore and not read anything I write to you, good luck with this. Quote Link to comment Share on other sites More sharing options...
ShadowIce Posted February 7, 2010 Author Share Posted February 7, 2010 premiso, i dont ignore u. i just like teamatomic's idea better no offense Quote Link to comment Share on other sites More sharing options...
Buddski Posted February 7, 2010 Share Posted February 7, 2010 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.. Quote Link to comment Share on other sites More sharing options...
ShadowIce Posted February 7, 2010 Author Share Posted February 7, 2010 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 } Quote Link to comment Share on other sites More sharing options...
ShadowIce Posted February 7, 2010 Author Share Posted February 7, 2010 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(); ?> Quote Link to comment Share on other sites More sharing options...
Buddski Posted February 7, 2010 Share Posted February 7, 2010 Have you tested it? That may work but there is only one way to find out... Quote Link to comment Share on other sites More sharing options...
ShadowIce Posted February 7, 2010 Author Share Posted February 7, 2010 yes i tested it. no it didnt work for some reason its a blank page when i try to use it oO idk why Quote Link to comment Share on other sites More sharing options...
ShadowIce Posted February 7, 2010 Author Share Posted February 7, 2010 idk what went wrong. it looks right to me Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 7, 2010 Share Posted February 7, 2010 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 Quote Link to comment Share on other sites More sharing options...
ShadowIce Posted February 7, 2010 Author Share Posted February 7, 2010 that else statement didnt work. i right clicked the page, and i cant even see my mouse menu but when i take the code out, i can see menu Quote Link to comment Share on other sites More sharing options...
ShadowIce Posted February 7, 2010 Author Share Posted February 7, 2010 what could be stopping my menu AND making it blank? Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 7, 2010 Share Posted February 7, 2010 omit the output buffering and see what happens. HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 7, 2010 Share Posted February 7, 2010 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 Quote Link to comment Share on other sites More sharing options...
ShadowIce Posted February 7, 2010 Author Share Posted February 7, 2010 and now its returning YES JS even if js is off. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 7, 2010 Share Posted February 7, 2010 Then you have something else going on. How are you switching JS on and off? HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
ShadowIce Posted February 7, 2010 Author Share Posted February 7, 2010 By going to the tools menu in IE Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 7, 2010 Share Posted February 7, 2010 Have you checked that JS is actually being switched on/off. Go to a JS repository and try to run a js example to test if its really off. HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
ShadowIce Posted February 7, 2010 Author Share Posted February 7, 2010 yea its off alright when i turn off jscript and click 'run code' it doesnt run the code http://writecodeonline.com/javascript Quote Link to comment Share on other sites More sharing options...
greatstar00 Posted February 7, 2010 Share Posted February 7, 2010 shadowice, tell us what exactly u did all steps u did dont miss anything Quote Link to comment Share on other sites More sharing options...
ShadowIce Posted February 8, 2010 Author Share Posted February 8, 2010 I went to repository, typed in document.write('hello world'); turned off js and the button didnt work Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.