dsxay48646 2010-06-04 03:55
浏览 36
已采纳

需要在web开发抽象的所有层中访问相同的布尔结果

I am designing a web site, and it has the ability to log in. When someone is logged in, there is at times a need to know what group they're in. Specifically, whether they are in the officer group. Currently, I have a MySQL stored proc BOOL is_officer(INT id), where id is the user id number.

My question: Is it wise to make a PHP function in my library (bool) is_officer(), which uses $_SESSION['id'] and calls the MySQL stored proc?

Next, is it wise to make a page /ajax/is_officer.php, which would call the function in PHP, which would in turn call the MySQL stored proc, in case I need to (insecurely) know whether the user is an officer on-the-fly?

(Last would be making a JS function is_officer() which would send an AJAX request to is_officer.php).

tl;dr: Same function name in many languages, one calls another, closer and closer to the database -- good idea or bad idea?

I'm basically asking for your guidance here -- sort of a communal yay/nay vote. Does this match any design patterns you've seen in the past?

  • 写回答

2条回答 默认 最新

  • dpntq48842 2010-06-04 04:02
    关注

    This seems way too specialized a function to have. With this approach, you'll eventually accumulate a set of functions on various levels for each trivial bit of information. Also, making a roundtrip to the database can quickly become very wasteful.

    I'd prefer to store relevant information about the logged in user in a session. Something along the lines of this:

    if (/* user logged in successfully */) {
        $user = $Db->getUserInfo($id);
        // $user = array('id' => 1, 'name' => 'Foo', 'officer' => true, ...)
        $_SESSION['user'] = $user;
    }
    

    And whenever you need to know if the user is an officer, you just check for $_SESSION['user']['officer'].

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号