我有3个文件:home,Failed_attempt,login.
文件home和Failed_attempt都是引用登录文件.
令人讨厌的是他们犯了一个错误,说登录文件不存在.如果我这样做,home会抛出一个异常,但是fail_attempt不会.
- include_once("../StoredProcedure/connect.PHP");
include_once( “../名字/ sanitize_string.PHP”);
如果我这样做:
相反的情况发生,Failed_attempt抛出一个异常,但回家,不会.我该如何解决..
我是否通过把这个../告诉include包括一个页面,因此home.PHP不需要去一页因此它会引发异常..
我怎样才能这样做,以便两个文件都接受那些包含有效…也许不相对于它们的位置…即没有../
目录结构:
这有三种可能的解决方案.第二种方法实际上就是以巧妙的方式使用绝对路径的解决方法.
1:chdir进入正确的目录
- <?PHP
- // check if the 'StoredProcedure' folder exists in the current directory
- // while it doesn't exist in the current directory,move current
- // directory up one level.
- //
- // This while loop will keep moving up the directory tree until the
- // current directory contains the 'StoredProcedure' folder.
- //
- while (! file_exists('StoredProcedure') )
- chdir('..');
- include_once "StoredProcedure/connect.PHP";
- // ...
- ?>
请注意,只有当StoredProcedure文件夹位于可能需要包含其包含的文件的任何文件的最顶层目录中时,这才有效.
2:使用绝对路径
在您说这不可移植之前,它实际上取决于您如何实现它.这是一个适用于Apache的示例:
或者,再次使用apache,将以下内容放在根目录中的.htaccess中:
然后在example.PHP中:
- <?PHP
- define('MY_DOC_ROOT','/path/to/docroot');
- ?>
最后在你的文件中:
3:设置PHP的include_path
请参阅the manual entry for the include_path directive.如果您无权访问PHP.ini,则可以在.htaccess中设置,如果您使用的是Apache而PHP未安装为CGI,如下所示:
- PHP_value include_path '/path/to/my/includes/folder:/path/to/another/includes/folder'