0

I am adding some include("file.php"); to several php pages.

Is there any way to be sure the include is added to every PHP page I create so I don't need to manually add those lines of code and also to prevent human error. If it is not possible to automatically do this, what would be the best way to achieve this.

I need to do this so I can do something like checking it the user is already authenticated then continue if not then redirect to the login page.

1

2 Answers 2

3

You want to use auto_prepend_file. Set this directive in your php.ini or .htaccess file to the path to your file.php file and any PHP file accessed will automatically have the contents of the config file prepended to it.

For .htaccess

php_value auto_prepend_file /full/path/to/file/file.php

or Try this

foreach (glob("classes/*.php") as $filename)
{
    include $filename;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Sounds like this is an overkill. I would recommend a require.php containing all paths of php required to load and use __autoload feature.
1

If the PHP are OOP classes & PHP 5 is used, here is a way to load classes ONLY when they are needed:

$class_dir = array(
    'some/class/path/model/',
    'some/class/path/dao/',
    'some/class/path/service/',
);
function __autoload($class_name) {
    global $class_dir;
    foreach ($class_dir as $directory) {
        if (file_exists($directory . $class_name . '.php')) {
            require_once($directory . $class_name . '.php');
            return;
        }
    }
}

Reference: PHP 5 Autoload

1 Comment

Thanks a lot but they are not classes so I will mark the other answer as correct but yours is correct too. Really thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.