如何在网站的所有其他PHP文件中包含PHP文件?

时间:2021-08-10 11:10:15

Hi I have to include a PHP file in all other PHP file of my site.

嗨,我必须在我的网站的所有其他PHP文件中包含一个PHP文件。

This is a sample of my sitemap:

这是我的站点地图的示例:

|index.php
|foolder1
||    file1.php
||    check.php
|foolder2
||    file2.php
|foolder3
||    file3.php

I want that all PHP file have included check.php, I must say that I can't edit php.ini and I know there is php_value auto_prepend_file "check.php" but I don't know how this function run then I'm asking help to use php_value auto_prepend_file or another way to include my file in all other PHP file? Thank you in advance.

我希望所有PHP文件都包含check.php,我必须说我不能编辑php.ini而且我知道有php_value auto_prepend_file“check.php”但我不知道这个函数是如何运行的然后我是请求帮助使用php_value auto_prepend_file或其他方式将我的文件包含在所有其他PHP文件中?先谢谢你。

EDIT:

I know that in StakOverflow there are some question like this but i can't edit php.ini (some server restrictions...) and I've just try to put php_value auto_prepend_file "foolder1/check.php" in .htaccess and it run for index.php but not for file1.php, file2.php,...

我知道在StakOverflow中有一些像这样的问题,但我无法编辑php.ini(一些服务器限制......)而我只是尝试将.htaccess中的php_value auto_prepend_file“foolder1 / check.php”放入其中运行index.php但不适用于file1.php,file2.php,...

2 个解决方案

#1


1  

A better way might be to use what is called the front controller pattern.

更好的方法可能是使用所谓的前控制器模式。

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On

    # Explicitly disable rewriting for front controllers
    RewriteRule ^app_dev.php - [L]
    RewriteRule ^app.php - [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ /app.php [QSA,L]
</IfModule>

You would get your web server to pass all requests into app.php and then you bootstrap the application and then pass the request on to the relevant scripts.

您可以让您的Web服务器将所有请求传递到app.php,然后引导应用程序,然后将请求传递给相关脚本。

// app.php
//
// Do shared steps like setup the database

switch($_SERVER['REQUEST_URI']) {
  case "foo/bar":
    require "foo/bar.php";
    break;
  case "baz/woo":
    require "baz/woo.php";
    break;
  default:
    require "404.php";
}

The main advantage here is that you are abstracting your URL:s from the actual file structure and also removing the server technology from your URLs which is a good practice.

这里的主要优点是,您从实际的文件结构中抽象出URL:从URL中删除服务器技术,这是一种很好的做法。

#2


0  

i am assuming you are using apache2.

我假设你正在使用apache2。

Lets say on Ubuntu 14.04

让我们说Ubuntu 14.04

Edit

etc/apache2/sites-available/000-default.conf

add below line inside the VirtualHost

在VirtualHost中添加以下行

php_value auto_prepend_file "/var/www/html/check.php"

restart apache server. i tested this and its working

重启apache服务器。我测试了它和它的工作

Edit: i tried with .htaccess, which is also working for me.

编辑:我试过.htaccess,这也适合我。

please note 2 things

请注意2件事

1) .htaccess is enabled How to enable use of .htaccess in Apache on Ubuntu

1).htaccess已启用如何在Ubuntu上启用Apache中的.htaccess

2) provide absolute path for check.php

2)为check.php提供绝对路径

anyhow both worked for me

无论如何两者都适合我

#1


1  

A better way might be to use what is called the front controller pattern.

更好的方法可能是使用所谓的前控制器模式。

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On

    # Explicitly disable rewriting for front controllers
    RewriteRule ^app_dev.php - [L]
    RewriteRule ^app.php - [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ /app.php [QSA,L]
</IfModule>

You would get your web server to pass all requests into app.php and then you bootstrap the application and then pass the request on to the relevant scripts.

您可以让您的Web服务器将所有请求传递到app.php,然后引导应用程序,然后将请求传递给相关脚本。

// app.php
//
// Do shared steps like setup the database

switch($_SERVER['REQUEST_URI']) {
  case "foo/bar":
    require "foo/bar.php";
    break;
  case "baz/woo":
    require "baz/woo.php";
    break;
  default:
    require "404.php";
}

The main advantage here is that you are abstracting your URL:s from the actual file structure and also removing the server technology from your URLs which is a good practice.

这里的主要优点是,您从实际的文件结构中抽象出URL:从URL中删除服务器技术,这是一种很好的做法。

#2


0  

i am assuming you are using apache2.

我假设你正在使用apache2。

Lets say on Ubuntu 14.04

让我们说Ubuntu 14.04

Edit

etc/apache2/sites-available/000-default.conf

add below line inside the VirtualHost

在VirtualHost中添加以下行

php_value auto_prepend_file "/var/www/html/check.php"

restart apache server. i tested this and its working

重启apache服务器。我测试了它和它的工作

Edit: i tried with .htaccess, which is also working for me.

编辑:我试过.htaccess,这也适合我。

please note 2 things

请注意2件事

1) .htaccess is enabled How to enable use of .htaccess in Apache on Ubuntu

1).htaccess已启用如何在Ubuntu上启用Apache中的.htaccess

2) provide absolute path for check.php

2)为check.php提供绝对路径

anyhow both worked for me

无论如何两者都适合我

相关文章