PHP需要一次
问题描述:
我遇到问题,包括Auth.php
。该文件位于该确切的位置,这是我的WWW网站的根。PHP需要一次
指定$config
变量没有问题。
我的代码:
<?php
include("./head.inc");
// start a new session (required for Hybridauth)
session_start();
// change the following paths if necessary
$config = dirname(__FILE__) . '/www.bommachine.co.uk/site/modules/ProcessHybridAuth/hybridauth/config.php';
require_once("/www.bommachine.co.uk/site/modules/ProcessHybridAuth/hybridauth/Hybrid/Auth.php");
我的错误:
Compile Error: require_once(): Failed opening required '/www.bommachine.co.uk/site/modules/ProcessHybridAuth/hybridauth/Hybrid/Auth.php' (include_path='.:/usr/share/php:/usr/share/pear') (line 11 of /var/www/www.bommachine.co.uk/site/templates/Test.php)
不知道什么是最好的,最安全的,为什么解决这个问题。
答
您可以使用您已经使用的方法,在此定义$config
。指定包含基本目录的常数:你需要需要的文件
define('BASEPATH', dirname(__FILE__));
然后用这个:
require_once(BASEPATH . "/www.bommachine.co.uk/site/modules/ProcessHybridAuth/hybridauth/Hybrid/Auth.php");
但是,请注意,__FILE__
将包含完整的文件路径中你正在使用这个Magic constant。如果你在/var/www/index.php
中使用它,这将是路径。如果你在/var/www/subdir/file.php
中使用它,这将是__FILE__
的值。
+1
自PHP版本5.3.0起,还有'__DIR__'。只需使用它并保护你自己的一些代码;) – ToBe 2014-10-28 14:44:15
你是不是想要在你的'require_once'调用中做同样的事情? – 2014-10-28 13:35:17
你不要在'require_once'中使用$ config var – 2014-10-28 13:35:47
它看起来好像在你正在分配配置变量的那一行中,你正在预先设置一个'/ wwww ...'的路径,这会使它成为相对的。在第二种情况下,您使用的是require_once,但是,您正在使用'/ www ...'作为绝对路径。所以你可能想把它改成require_once(dirname(_ _ FILE _ _)。'/ www ....') – rethab 2014-10-28 13:36:23