简体   繁体   中英

Curious: Disable jQuery and Javascript interactions unless user is logged in?

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.

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 } ?>

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
}

否。禁用JavaScript是一种浏览器功能,无法以编程方式控制。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM