使用从父域到子域的外部类文件

发布于 2024-11-28 18:30:30 字数 1052 浏览 0 评论 0原文

我如何包含父域中的类文件以在我的服务器上的子域中使用?

例如,我有一个处理用户身份验证以及一些重要方法的类文件。如何在子域上使用该身份验证类?

我的文件夹结构是

父域

/home/<domain>/public_html/

子域

/home/<domain>/public_html/users/cluster_1/<sub>

当我创建一个新的子域时,我有一个模板index.php,它被复制到

/home/<domain>/public_html/users/cluster_1/<sub>/index.php

index.php中,我想包含我的身份验证类,以便我可以根据返回的数据做更多的事情通过其方法。

我知道我不能包含 http 请求,而且我也不想使用 dirname(dirname(__FILE__) 因为如果用户更改,他们可以看到该域的服务器文件夹结构。

我尝试了 fopen但我无法让它包含我的类,而且我也尝试了像这样的 cURL

function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
$returned_content = get_data('http://domain.com/api/ApiClient.php');

有一种优雅的方式来包含我的 ApiClient.php 类文件,即使用户更改 PHP 代码也看不到文件夹结构信息这是由 PHP 错误引发的。

how can i inlude a class file from my parent domain to be used in a subdomain on my server?

for example, I have a class file that handles user authentication along with some important methods. How can I use that authentication class on a subdomain?

my folder structure is

parent domain

/home/<domain>/public_html/

subdomain

/home/<domain>/public_html/users/cluster_1/<sub>

When I make a new subdomain I have a template index.php that is copied onto the

/home/<domain>/public_html/users/cluster_1/<sub>/index.php

in the index.php i want to include my authentication class so that I can do some more stuff based on the data returned by its methods.

i know that i cannot include http requests and also I do not want to use dirname(dirname(__FILE__) because if the user alters that they can see the server folder structure for the domain.

I tried an fopen but i couldn't get that to include my class, and also i tried a cURL like this

function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
$returned_content = get_data('http://domain.com/api/ApiClient.php');

is there an elegant way to include my ApiClient.php class file and even if the user changes the PHP code for them not to see folder structure information that is thrown by the PHP errors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

衣神在巴黎 2024-12-05 18:30:30

抱歉,不,不是真的。

虽然实际上可以通过 HTTP 下载 PHP 代码并在本地运行,但由于安全隐患,强烈不鼓励这样做:您的系统中存在一个小错误,或者代码服务器中存在一个小错误突然间,您正在运行来自未经检查的来源的恶意代码。他们默认禁用该行为是有充分理由的。

如果您的用户可以编辑和运行他们自己的(未经检查的)PHP 代码,那么您的用户看到您的目录结构就没有必要犹豫:除非您保护您的设置(听起来不像),否则他们可以正确地找到该结构现在。只需使用适当的函数即可遍历目录。

我只是使用直接包含方法。

require MAIN_CLASS_PATH . '/api/ApiClient.php';

如果您真的希望它们不知道父域:

  • 将共享类移动到单独的项目中,从而移动到单独的目录中(例如 /home/$user/shared/api
  • 通过设置 PHP 的 open_basedir 并禁用 exec 等函数来保护子域()(可用于规避 open_basedir)
  • 包含来自您的父域和子域的 /home/$user/shared/api 。

Sorry, nope, not really.

While it would actually be possible to download PHP code over HTTP and have it run locally, it's strongly discouraged because of the security implications: one little bug in your including system or one little bug in the code-server and suddenly you're running malicious code from an unchecked source. There's a good reason why they disable that behaviour by default.

If your users can edit and run their own (unchecked) PHP code, there's no sense in being hesitant about your users seeing you directory structure: unless you secured your setup (which it doesn't sound like) they could find out that structure right now. Just by using the appropriate functions to walk directories.

I'd just use the direct inclusion approach.

require MAIN_CLASS_PATH . '/api/ApiClient.php';

If you really really want them to be unaware of the parent domain:

  • Move the shared classes into a separate project and thus a separate directory (say /home/$user/shared/api
  • Secure the subdomains by setting PHP's open_basedir and disabling functions like exec() (which can be used to circumvent open_basedir)
  • Include from /home/$user/shared/api from both your Parent domain and your Subdomain.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文