0

I am creating a dynamic website using PHP, which includes a user login system. There is a script in every page which checks whether the user is logged in (using cookies). If not, he is redirected to the login page. Since this script is exactly the same for every page, I am thinking of creating a separate file for it, and including that file in every page. That will make it easy to manage and make changes, if required.

My question is does "including" (using the include or require statement) the file make the execution of my main page slower? Does it have other disadvantages? Let me mention at this point that I am currently on a free host.

Also, I have already created a page (databaseDetails.php) where I am storing my database name, username and password in variables; and I am referring to it every time a page loads. So if the answer to the first part of my question is yes, then my main page (main.php, which the user will actually view) will include the checkWhetherLoggedIn.php (which checks if the user is logged in), which in turn will be including the databaseDetails.php.

Is such a sort-of-"nested including" okay? Or should it be avoided?

4

2 回答 2

4

Average PHP projects include (no pun intended) several dozen include statements and more, and they are perfectly fast enough. One or two certainly won't kill you. Splitting code up over several files is a pretty essential technique to keep large projects manageable; speeding up the minimal overhead can be mitigated by all kinds of tweaks and caches when necessary.

Worry about performance problems after you have tried it and deemed it too slow!

于 2013-07-03T10:52:56.963 回答
1

There is absolutely no problem to include some pages and nested includes

if you want to know exactly the difference, you can measure the script execution time with and without including your file, you will see it will almost not change

all websites use include, don't worry about include's performances.
Don't hesitate when you want to use a include, it will make you code faster !

when you use include, you're organizing your code, so, it's more easy to understand and to edit :)

于 2013-07-03T11:07:43.457 回答