Frage

Is there a way to include a file on base of the first folder. An example:

includes/file1.php
includes/file2.php
includes/file3.php

The folders on my site:

www.mysite.com/folder1
www.mysite.com/folder2
www.mysite.com/folder3

When I browse to www.mysite.com/folder1 it will include file1.php. When I browse to www.mysite/com/folder1/somExtraFolder/somethingElse it also will include file1.php. When I go to www.mysite.com/folder2 it will include file2.php. When I browse to www.mysite/com/folder2/somExtraFolder/somethingElse it also will include file2.php and so on.

Is this possible in php or do I need to work with some kind of javascript/jquery to accomplish my goal.

War es hilfreich?

Lösung

Michael Berkowski has the correct answer, use PHP's auto_prepend_file

You can set this setting per directory using .htaccess (Apache) or .user.ini (CGI, FastCGI)

.htaccess style

php_value auto_prepend_file "file1.php"

.user.ini style

auto_prepend_file = "file1.php"

add the correct file type to the directory you want to modify (folder1, folder2, folder3) to have the auto_prepend_file automatically included, exactly as if you had used require or include, as the first script to run.

Andere Tipps

This is not a PHP question, this would be a function of your web server. Assuming Apache, you could use mod_rewrite and an .htaccess rule.

You could use a router.

I'd highly recommend this one, I've used it before: http://toroweb.org/

You could then implement it like:

<?php

class File1 {
    function get() {
        include "file1.php";
    }
}

class File2 {
    function get() {
        include "file2.php";
    }
}

Toro::serve(array(
    "/includes/file1.php" => "File1",
    "/includes/file2.php" => "File2",
));
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top