简体   繁体   English

<button>从中</button>添加/删除 <div> <button>?</button>

[英]Add/Remove <button> from <div>?

Objective 目的

The goal is to provide features for inventory management for the authenticated users. 目的是为经过身份验证的用户提供库存管理功能。 Other visitors can consult the inventory only, and filter their findings. 其他访问者只能查询库存,并过滤他们的发现。

Authentication 认证

The authentication is through the ajaxSession() javascript function as described here. 如此处所述,通过ajaxSession() javascript函数进行身份验证。

  1. PHP / Ajax : How to show/hide DIV on $_SESSION variable value? (See answer) (查看答案)
  2. PHP: $_SESSION doesn't seem to get set. Any idea?

Now, the $.ajax() delegate gets the right answer from the PHP server. 现在, $.ajax()委托从PHP服务器获得正确的答案。 Now, I though of simply use CSS classes .hide and .show , but it doesn't seem to be a good idea for security reasons. 现在,我虽然仅仅使用CSS类.hide.show ,但它似乎并没有被出于安全原因,一个好主意。

The ajaxSession() function ajaxSession()函数

function ajaxSession(actionUrl) {
    $.ajax(function() {
        url: actionUrl
        success: function(authenticated) {
            if (authenticated == 'true') {
                // create buttons here.
            } else {
                // ensure to empty those div
            }
        }
    });
}

NB There are other members set in the $.ajax() call, but the important is there. 注意$.ajax()调用中还设置了其他成员,但重要的是在那里。

The security mechanisms should be implemented on the server side! 安全机制应在服务器端实现!

There won't be a security vulnerability if you create some login-specific buttons in your JS. 如果您在JS中创建一些特定于登录的按钮,则不会存在安全漏洞。 Just be sure that you don't output sensitive data from your backend (PHP) or that you perform any unauthorized operation (again from your backend). 只要确保您没有从后端(PHP) 输出敏感数据执行任何未经授权的操作 (同样从后端)即可。

Always check on the server side (for each operation) that the user is authorized! 始终在服务器端(每次操作)检查用户是否被授权!
Store the login state in a session or the like. 将登录状态存储在会话等中。 Don't rely on a URL parameter like isLoggedIn=1 or POST data. 不要依赖isLoggedIn=1或POST数据之类的URL参数。 They come from the client. 他们来自客户。 Clients are always the untrusted parts in an application. 客户始终是应用程序中不可信任的部分。

You could use random class names instead of hide/show if you're worried about people guessing them... '230583' or 'fss83hjg' etc. 如果您担心别人会猜测它们,可以使用随机类名代替隐藏/显示...'230583'或'fss83hjg'等。

If I understand correctly, is this what you're after? 如果我理解正确,这是您的追求吗?

function ajaxSession(actionUrl) {
$.ajax(function() {
    url: actionUrl
    success: function(authenticated) {
        if (authenticated == 'true') {
            // create buttons here.
            var buttons = '<input type="button" value="Button 1">'
            buttons += '<input type="button" value="Button 2">'
            buttons += 'etc...';
            $('#button-container-div').html( buttons );
        } else {
            // ensure to empty those div
            $('#button-container-div').html('');
        }
    }
});
}

When creating your elements in jQuery, you should do them dynamically so that the elements are not on the page by default (see above). 在jQuery中创建元素时,应该动态地进行操作,以使元素默认情况下不在页面上(请参见上文)。 However, if anybody is looking (not even that hard) they will be able to see what you're up to in your source code and could fairly easily use the console to recreate whatever you're doing if authenticated... I'd suggest using server side code (you're using PHP?) to produce your sensitive data. 但是,如果有人正在寻找(甚至不那么努力),他们将能够在源代码中看到您的工作,并且可以很容易地使用控制台通过身份验证来重新创建您正在做的任何事情。建议使用服务器端代码(您正在使用PHP?)来生成敏感数据。 (see below) (见下文)

<?php
// start sessions if you aren't including a global config file that does it for you
session_start();

if($_SESSION['is_logged_in'] == true) {
    // logged in, show buttons
    $buttons = '<input type="button" value="Button 1">';
    $buttons .= '<input type="button" value="Button 2">';
    $buttons .= 'etc...';

    echo $buttons;
} else {
    // not logged in
}
?>

For success: 为了成功:

Function myFunction()
{
    var btn=document.createElement("BUTTON");
    var t=document.createTextNode("CLICK ME");
    btn.appendChild(t);
    document.getElementById("theDiv").appendChild(btn);
}

For fail 为失败

function myFailFunction() {
   var node = document.getElementById("theDiv");
   while (node.hasChildNodes()) {
        node.removeChild(node.lastChild);
    }
}

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

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