需要能够从任何目录访问Common.php

时间:2013-10-04 16:07:53

标签: php

<div id="phpbbBox"><?php

define('IN_PHPBB', true);
$phpbb_root_path = './forum/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx); 




// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();


if($user->data['is_registered'])
{

    echo "Hello " . "<a href=\"http://cgoava.com/forum/memberlist.php?mode=viewprofile&un=" . $user->data['username'] . "\">" . $user->data['username'] . "</a>"; //User is already logged in
    echo "<br /> You have " . "<a href=\"http://cgoava.com/forum/ucp.php?i=pm&folder=inbox\">" . $user->data['user_new_privmsg'] . " new PM!</a> ";
    echo "<br /> Last visit: " . $user->format_date($user->data['session_last_visit']);

}
else if(isset($_POST['login']))
{
    $username = request_var('username', '', true);
    $password = request_var('password', '', true);
    $autologin = (!empty($_POST['autologin'])) ? true : false;

    $result = $auth->login($username, $password, $autologin);

    if ($result['status'] == LOGIN_SUCCESS)
    {
        //User was successfully logged into phpBB


        // append/replace SID
        $redirect = reapply_sid($_SERVER['PHP_SELF']);
    meta_refresh(0, $redirect);
    echo "<script type=\"text/javascript\">
    location.reload();
    </script>";
    }
    else
    {
        echo 'Bad Login ' . $username; //User's login failed
    }
}
else
{
    echo '<form method="POST" action="">
        <input type="text" name="username" size="10">
        <input type="password" name="password" size="10"><br />
         Remember Me?: <input type="checkbox" name="autologin">
        <input type="submit" value="Submit" name="login">
        </form>';
}
?></div>

<div id="phpbbBox"><?php define('IN_PHPBB', true); $phpbb_root_path = './forum/'; $phpEx = substr(strrchr(__FILE__, '.'), 1); include($phpbb_root_path . 'common.' . $phpEx); // Start session management $user->session_begin(); $auth->acl($user->data); $user->setup(); if($user->data['is_registered']) { echo "Hello " . "<a href=\"http://cgoava.com/forum/memberlist.php?mode=viewprofile&un=" . $user->data['username'] . "\">" . $user->data['username'] . "</a>"; //User is already logged in echo "<br /> You have " . "<a href=\"http://cgoava.com/forum/ucp.php?i=pm&folder=inbox\">" . $user->data['user_new_privmsg'] . " new PM!</a> "; echo "<br /> Last visit: " . $user->format_date($user->data['session_last_visit']); } else if(isset($_POST['login'])) { $username = request_var('username', '', true); $password = request_var('password', '', true); $autologin = (!empty($_POST['autologin'])) ? true : false; $result = $auth->login($username, $password, $autologin); if ($result['status'] == LOGIN_SUCCESS) { //User was successfully logged into phpBB // append/replace SID $redirect = reapply_sid($_SERVER['PHP_SELF']); meta_refresh(0, $redirect); echo "<script type=\"text/javascript\"> location.reload(); </script>"; } else { echo 'Bad Login ' . $username; //User's login failed } } else { echo '<form method="POST" action=""> <input type="text" name="username" size="10"> <input type="password" name="password" size="10"><br /> Remember Me?: <input type="checkbox" name="autologin"> <input type="submit" value="Submit" name="login"> </form>'; } ?></div>

这一切都适用于:www.example.com/page.php

然而,它不适用于:www.example.com/ex/page2.php或www.example.com/ex/word/page3.php

努力访问common.php来自我所在的任何目录的位置,但是,它还需要是一个授权访问,因此一个简单的$ _SERVER包含一直没有用。

当我尝试将整个事物包含在内时,它仍然不起作用。主要目标是将所有这些代码放在其中一个核心包含中,以显示在每个页面的菜单中。

登录适用于同一页面。我需要登录和登录后代码才能在网站的每个页面上使用(40多页网站)。 我使用了2级包含来构建网站。

2 个答案:

答案 0 :(得分:0)

因为它可以在www.example.com/page.php上运行而不是在更深的目录中,看起来您的论坛目录位于/forum而不是./forum 所以改变

$phpbb_root_path = './forum/';

$phpbb_root_path = '/forum/';

(./是当前目录,因此它正在寻找www.example.com/ex/forum/common.php中的common.php)

答案 1 :(得分:0)

在这种情况下,您无法使用相对路径。如果你使$phpbb_root_path成为绝对路径,那么从该变量构建路径将正常工作。

尝试:

$phpbb_root_path = $_SERVER['DOCUMENT_ROOT'] . '/folder/to/forum/';