-1

所以我的 index.php 上有以下代码:

<?php 
define('WP_USE_THEMES', false); // Don't use WP themes
require('../wp-load.php'); // Allows the use of WP functions
?>

<?php // Check if the user is logged in. If not, show the login form 
global $user_ID, $user_identity; get_currentuserinfo(); if (!$user_ID) { ?>

    <script type="text/javascript">window.location.href='login.php';</script>

    <?php } else { // If the user is logged in

        include('header.php'); ?>

        <div id="content">

            <!-- Full Width Start -->
            <div class="full-width">        
                <span class="full-width-red-left"></span>
                <div class="full-width-red-wrapper">Department Chooser</div>
                <span class="full-width-red-right"></span>           
                <div class="full-width-content">
                    <div id="panel-icons">
                        <?php // Display the menu for each department the user is a member of.
                        if(is_eventsStaff()){ eventsPanelIcon(); }
                        if(is_helpDeskStaff()){ helpPanelIcon(); }
                        if(is_hxlStaff()){ livePanelIcon(); }
                        if(is_newsStaff()){ newsPanelIcon(); }
                        if(is_raresStaff()){ raresPanelIcon(); }
                        if(is_panelAdmin()){ adminPanelIcon();} ?>       
                    </div>
                    <div id="uploader-icon-wrapper">
                        <div class="panel-horizontal-split"></div>
                        <a href="#" id="upload-panel-icon"></a>
                    </div>
                </div>           
                <div class="full-width-footer"></div>
            </div>
            <!-- Full Width End -->
        </div>

    </div>  
<?php } else { echo "Test"; } }?>

然后是 header.php:

<link rel='stylesheet' href='CSS/build.css' type='text/css'/>
<title>Habbox Staff Panel</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<?php include_once('functions.php'); // Include staff panel functions ?>

<!-- Preload Image Hovers Start -->
<img src="images/topbar_home_hover.png" class="preload" />
<img src="images/topbar_admin_hover.png" class="preload" />
<img src="images/topbar_events_hover.png" class="preload" />
<!-- Preload Image Hovers End -->

<?php if(has_Roles()) { ?>
<!-- TopBar Start -->
<div id="topbar-wrapper">
<div id="topbar-inner-wrapper">
    <span class="topbar-radio-text">Welcome <span class="bold red"><?php echo $user_login; ?></span></span> 
    <div class="topbar-divider"></div>
    <a href="<?php echo $siteURL;?>/index.php" class="topbar-home"></a>

    <?php 

    if (isset($_COOKIE['timezone'])) { date_default_timezone_set($_COOKIE['timezone']); } else { date_default_timezone_set('Europe/London'); }

    if(is_eventsStaff()){
        echo '<a href="'.$siteURL.'/Events/index.php" class="topbar-events"></a>';
    }

    // Check if the user is an admin
    if(is_panelAdmin()){
        echo '<a href="'.$siteURL.'/Admin/users.php" class="topbar-admin"></a>';
    }



    ?>
    <div id="topbar-logout">
        <form>
            <div class="darkbutton"><span class="darkbutton-left"></span><a href="<?php echo wp_logout_url( home_url() ); ?>" title="Logout">Log Out</a><span class="darkbutton-right"></span>
            </div>
            <input type="hidden" name="redirect_to" value="/Staff">
        </form>
    </div>
</div>
</div>
<!-- TopBar End -->

<div id="page-wrapper">

但它给了我以下错误:

解析错误:语法错误,第 43 行 index.php 中的意外 T_ELSE

但我不明白为什么。如果我将 header.php 的内容复制到 index.php 中,它似乎工作正常,但当我使用包含时却不行。

4

4 回答 4

3

通读代码include并不完全等同于复制和粘贴代码。所有涉及的文件本身都必须是有效的 PHP 代码:您不能if()在包含的文件中打开并在主文件中关闭它。PHP 将分别解析每个文件并找到以下内容:

if(...){
}else{
}else{
}

...这显然是非法的。

于 2012-10-25T16:28:54.287 回答
2

如果出现以下情况,您还有另外两个:

if (!$user_ID) {

   <?php } else { // If the user is logged in

<?php } else { echo "Test"; } }?>
于 2012-10-25T16:29:18.157 回答
1

header.php您没有关闭此 IF 时:

<?php if(has_Roles()) { ?>

您可以在这部分代码中执行此操作:

if(is_panelAdmin()){
        echo '<a href="'.$siteURL.'/Admin/users.php" class="topbar-admin"></a>';
    }

 } // Closing has_roles() IF

    ?>
于 2012-10-25T16:28:13.450 回答
0

似乎我包含错误,每个文件本身必须是正确的语法。我只是将 if 和 else 语句放在 header.php 包含文件周围的索引文件中。一切似乎都有效:)

于 2012-10-25T17:01:33.117 回答