简体   繁体   English

好奇:除非用户登录,否则禁用jQuery和Javascript交互?

[英]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? 除非用户已登录,否则是否可以禁用jQuery和Javascript交互?

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: 首先,我想将所有交互放在一个单独的文件中,然后在用户登录时使用PHP加载它,但是有不同的方法来执行此操作,或者您认为最好执行以下操作:

Create a log-in screen before the user comes to the main part of the website. 在用户访问网站的主要部分之前创建一个登录屏幕。

We have HTML5 now! 我们现在有HTML5! Just create a HTML session or a php $COOKIE and control your javascript functions with a simple if statement. 只需创建一个HTML会话或php $ COOKIE,并使用简单的if语句控制您的javascript函数。

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 您可以在这里阅读更多信息http://www.w3schools.com/html/html5_webstorage.asp

PHP PHP

Easier way and personally my favorite is using PHP. 更容易的方式和个人我最喜欢使用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 您可以在这里阅读更多内容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. 当然,你可以总是代替拥有一个巨大的if语句,你可以在一个非常小的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: 如果你登录时页面上有一个对象,那么可能不存在,那么你可以在JS代码周围包装这样的东西:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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