1

Is it possible to disable jQuery and Javascript interactions unless the user is logged in?

At first I wanted to put all the interactions in a seperate file, and then load it in with PHP when the user is logged in, but is there a different way to do this, or would you think it's better to do the following:

Create a log-in screen before the user comes to the main part of the website.

7
  • What kind of code are you trying to disable? Be specific. Commented Nov 25, 2014 at 13:46
  • savage way : make an intentional js error, no more js can be executed after error Commented Nov 25, 2014 at 13:47
  • Don't add any script or js file until user is logged in. I think, there is not need for script until user is logged in. If user session is out, redirect the page to login page. Hope it works!!! Commented Nov 25, 2014 at 13:55
  • That's a bad idea @BenjaminPoignant Commented Nov 25, 2014 at 13:58
  • 1
    I know, that's why I wrote 'savage way' Commented Nov 25, 2014 at 14:14

3 Answers 3

2

We have HTML5 now! Just create a HTML session or a php $COOKIE and control your javascript functions with a simple if statement.

WEB STORAGE

localStorage.setItem("is_logged_in", var);

if(is_logged_in){
    //all of your functions in here  
}

You can read more here http://www.w3schools.com/html/html5_webstorage.asp

PHP

Easier way and personally my favorite is using PHP.

$var = "is_logged_in";
setcookie($var , time() + (86400 * 30), "/")

And then for the check:

<?php if(isset($_COOKIE[$var])) { ?>
    //all of your functions in here
<?php } ?>

You can read more here http://www.w3schools.com/php/php_cookies.asp

EDIT

And ofcourse you can always instead of having a huge if statement you can disable all controls in a very smaller if.

<?php if(isset($_COOKIE[$var])) { ?>
     $('button').attr('disabled', 'disabled');
<?php } ?>
Sign up to request clarification or add additional context in comments.

3 Comments

WOW thats my very first accepted answer! Love you dude!
Congratulations, it's a good solution to check for cookies with php ^^
Yes it is a good practice but you have to alwasy check on the session variables. This results to be a pain if you have to do it by hand.
1

Potentially if there is an object on the page present when you are logged in, that isn't if you are not, then you could wrap something like this around your JS code:

if ($('LoggedInElement').length) {
   // All my code
}

3 Comments

no unfortunately there isn't thank you for your answer though.
Could you not create something to show in php? I'm not a PHP coder myself but it could be something like if (blnUserLoggedIn) { echo "Something" }?
I guess that would be a solution, but I believe using cookies as suggested below would be better, thank you for your effort though.
0

No. Disabling JavaScript is a browser function and cannot be controlled programmatically.

3 Comments

hmmm, is there a different way to block click events unless the user is logged in?
You could just include jQuery on pages accessible to those who are logged in. Not sure what you're trying to accomplish.
i was just curious, i'am thinking about a one-page design where everything interesting happends on a main page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.