简体   繁体   English

PHP性能问题

[英]PHP performance question

Wondering which would be better for performance. 想知道哪种会更好。 The site will be viewed by people that are logged in and people that are not. 该站点将由已登录和未登录的用户查看。 The site is almost the same for the users that are logged in except they have more privledges. 对于已登录的用户,该站点几乎相同,只是他们拥有更多特权。 So I am wondering what would be more efficient. 所以我想知道什么会更有效。

//OPTION ONE //选项一

if(isLoggedIn()){
Write the whole web site plus the content the logged in user can access
}
else {
Write the whole website again, minus the content the logged in users can access. 
}

//OPTION TWO
Write the website content and inset the login function wherever i need to restrict the access, so the function would be called a few different times.

I am wondering if it would be better for performance using option one because the function would first be checked once, and wouldn't need to be checked again, if the user is logged in the first chunk would be loaded, if the user is not logged in, it would ignore the first chunk and load the second chunk. 我想知道使用选项一是否会更好,因为该功能将首先被检查一次,并且如果用户登录,则无需再次检查,如果未登录则将加载第一个块登录后,它将忽略第一个块并加载第二个块。

Neither. 都不行 The best option is to check for isLoggedIn once, save the result, and do ifs inside the source to swap in each place. 最好的选择是一次检查isLoggedIn,保存结果,然后在源内进行ifs交换。

The second option is negligibly more performance heavy, but is your better option as it produces less code duplication. 第二个选项的性能负担可以忽略不计,但是它是更好的选择,因为它产生较少的代码重复。

Also, if you cache the result of isLoggedIn() in a static var, you do not have to perform all your checks with every call of the method. 另外,如果将isLoggedIn()的结果缓存在静态var中,则不必在每次调用该方法时都执行所有检查。 You can check your static var and jump out early. 您可以检查您的静态变量并提早退出。

function isLoggedIn() {
    static $is_logged_in = null;

    if(!is_null($is_logged_in)) {
        return $is_logged_in;
    }

    //... user is known not to have valid credentials

    $is_logged_in = false;

    // ... User has been validated 

    $is_logged_in = true;

    //...


}

Both. 都。

You don't want to check isLoggedIn() everytime (especially if it's going to hit the database) because that will be slow. 您不想每次都检查isLoggedIn() (尤其是如果要访问数据库时),因为这会很慢。 But you don't want to have 2 versions of HTML either because it's unmaintainable. 但是您也不想拥有2个版本的HTML,因为它们无法维护。

Check it once at the top and set a variable (or use a session variable and check that). 在顶部检查一次并设置一个变量(或使用会话变量进行检查)。 Then in the HTML use if statements against the variable to determine what to show. 然后在HTML中使用针对变量的if语句确定要显示的内容。 For example: 例如:

PHP: PHP:

$logged_in = false;
if(isLoggedIn() ) {
     $logged_in = true;
}

HTML: HTML:

<?php if($logged_in) { ?>
<div>
     Super-secret needs a login stuff
</div>
<?php } else { ?>
<div>
     Sorry! You have to login to see this cool stuff
</div>
<?php } ?>

我会说,如果可以的话,请为未登录的用户保留一个缓存的版本,并在他们登录时生成所有内容。

For separation of concerns, it might be viable to let the client browser add functions for logged in users. 为了分离问题,让客户端浏览器为登录的用户添加功能可能是可行的。 Meaning you send out one static version of the website, and Javascript checks client-side for the presence of a login cookie. 这意味着您发送了一个静态版本的网站,而Javascript会在客户端检查登录cookie的存在。 If it's present the few additional GUI elements or permissable links are displayed. 如果存在,则会显示一些其他GUI元素或允许的链接。

The obvious pitfall being that JS disabled browsers would not see anything. 明显的陷阱是禁用JS的浏览器看不到任何东西。 Unless you decorate elements with CSS .optional-func and disable/enable that: 除非您使用CSS .optional-func装饰元素并禁用/启用:

if (!document.cookies.match(/login/)) { $(".user-funcs").hide(); }

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

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