简体   繁体   English

如何使用用户友好的URL在PHP中获取项目根目录

[英]How to get project root directory in php with user friendly URL

I created initialize.php to define my project's root directory. 我创建了initialize.php来定义项目的根目录。

defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);
defined('SITE_ROOT') ? null : define('SITE_ROOT', DS.'works'.DS.'myproject');

I include it with something like this from any directory. 我在任何目录中都包含了这样的内容。

require_once("../includes/initialize.php");

and I include other files easier. 而且我更容易包含其他文件。 I am going to upload the files to server, then I should only change the initialize.php file. 我将文件上传到服务器,然后只应更改initialize.php文件。

It works. 有用。 But, when I use user friendly URL's, and call ajax files from it, I don't know my directory hierarchy to include initialize.php. 但是,当我使用用户友好的URL并从中调用ajax文件时,我不知道我的目录层次结构是否包含initialize.php。

I don't want to do this: 我不想这样做:

require_once("/works/myproject/includes/initialize.php");

for every file. 对于每个文件。 Because it is going to change when I upload everytime. 因为每次上载时都会改变。

I don't want to use session or cookie for everyuser. 我不想为每个用户使用会话或cookie。

There should be a trick for this but I couldn't find. 为此,应该有一个技巧,但我找不到。 Is there a way for getting project root from anywhere while using user friendly URL's and ajax calls? 有没有一种方法可以使用用户友好的URL和Ajax调用从任何地方获取项目根目录?


I fixed it. 我修好了它。

when I call it with ajax it's ok. 当我用ajax调用它就可以了。 But I included it as php too for some conditions. 但在某些情况下,我也将其作为php包含在内。

Because of current and the ajax files are in different folders, it crashed. 由于当前文件和ajax文件位于不同的文件夹中,因此崩溃了。

So, when I change it to only require initialize.php when called with ajax, problem solved. 因此,当我将其更改为仅在使用ajax调用时需要initialize.php时,问题已解决。

If you're using Apache, you could try adding your includes directory to your include_path directive. 如果您使用的是Apache,则可以尝试将include目录添加到include_path指令中。

Find the current value by doing a phpinfo() and look for include_path and then try re-declaring it in your top-level web directory and add the absolute path to your includes directory. 通过执行phpinfo()查找当前值,并查找include_path ,然后尝试在顶级Web目录中重新声明它,并将绝对路径添加到includes目录。

So if the default value is: 因此,如果默认值为:

include_path = ".:/usr/bin/php:/some/other/directory"

just copy-paste the same thing in your .htaccess file but add your include directory on the end, separating it with a colon. 只需在.htaccess文件中复制粘贴相同的内容,然后在最后添加您的include目录,并用冒号分隔即可。

include_path = ".:/usr/bin/php:/some/other/directory:/works/myproject/includes"

and that way you shouldn't have to reference the absolute path every time. 这样,您不必每次都引用绝对路径。

This all depends on the permissions your web host gives you. 这一切都取决于您的Web主机为您提供的权限。 There are other (easier) ways to do this, but I find that most of them are usually restricted by hosting providers, and manually setting it via .htaccess is usually the most dependable way to get this done. 还有其他(更简便的)方法可以执行此操作,但是我发现大多数方法通常受托管提供程序的限制,而通过.htaccess手动设置通常是完成此操作的最可靠方法。

Here's a page listing a few different ways: Five ways to create include path for PHP 这是列出了几种不同方式的页面: 创建PHP的五种方式包括路径

Simply doing this: 只需这样做:

require_once("../includes/initialize.php");

is enough because PHP doesn't look at your friendly URLs. 足够了,因为PHP不会查看您的友好URL。 It includes whatever you give him to include relative to your script. 它包括您给他的与脚本有关的所有内容。

Also there is $_SERVER['DOCUMENT_ROOT'] that will give you an absolute path from your root directory. 此外,还有$_SERVER['DOCUMENT_ROOT']会为您提供根目录的绝对路径。

It is a good approach to define all your directories in a common file as you added initialize.php. 在添加initialize.php时,这是在公共文件中定义所有目录的好方法。 You will have to include this common file in every file on the project. 您将必须在项目的每个文件中包含此公用文件。 User friendly url's have no effect on the file inclusion. 用户友好的URL对文件包含没有影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM