简体   繁体   English

避免在重复使用的php组件上重复执行代码

[英]Avoiding repeated code execution on reused php component

I am using a generic toolbar on multiple pages of my web application (using php include). 我在Web应用程序的多个页面上使用通用工具栏(使用php include)。

The toolbar houses some log in/log out functionality. 工具栏包含一些登录/注销功能。 Also, on load, it checks the log in status and updates its UI accordingly. 同样,在加载时,它会检查登录状态并相应地更新其UI。 The basic flow is that it starts by displaying a 'Checking log in' label, and then once the status is determined (using the Facebook javascript API), it updates the label to either 'logged in' or 'not logged in'. 基本流程是,首先显示“正在检查登录”标签,然后确定状态(使用Facebook javascript API),然后将标签更新为“已登录”或“未登录”。 This is working fairly well. 这工作得很好。

My problem comes when the user navigates to a different page, which also includes the toolbar. 当用户导航到另一个页面(其中也包括工具栏)时,就会出现我的问题。 The toolbar again goes through its on load status checks, meaning that on each navigation, it is updating the toolbar to 'checking sign in' until it has done its thing. 工具栏再次经过其加载状态检查,这意味着在每次导航中,它会将工具栏更新为“检查登录”,直到完成其工作为止。

How might I avoid this? 我如何避免这种情况? Keep in mind that the status check is partially useful - page navigation is a good time to re-check the status since I don't want to assume that the user is still logged in. But I would like to avoid constant label updates, or somehow hide them from the user. 请记住,状态检查在某种程度上很有用-页面导航是重新检查状态的好时机,因为我不想假设用户仍在登录。但是我想避免不断的标签更新,或者对用户隐藏它们。

So if I understand right, you are using Ajax to see if the user is logged in? 因此,如果我理解正确,那么您正在使用Ajax来查看用户是否已登录? Normally that would be handled with your PHP so that the page arrives pre-rendered with the toolbar saying the user is logged in or not. 通常,这将由您的PHP处理,以便页面随工具栏预渲染而显示用户是否登录。

If your javascript still needed to know whether the user is logged in, you can embed hidden JSON into the page that contains any needed user and login info. 如果您的JavaScript仍然需要知道用户是否已登录,则可以将隐藏的JSON嵌入到包含任何所需用户和登录信息的页面中。

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

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