2014-04-02 70 views
0

我试图添加一个if条件的开始和if条件的结尾包含在另一个文件中的单独文件中。解析错误:语法错误,单独文件上的文件意外结束

我有一个名为cp.php文件,该文件包含的header.php和footer.php,看起来像这样:

<?php 
require_once "includes/header.php"; 
?> 
<p>Welcome <?php echo htmlentities($_SESSION['username']); ?>!</p> 
<p> 
    This is an example protected page. To access this page, users 
    must be logged in. At some stage, we'll also check the role of 
    the user, so pages will be able to determine the type of user 
    authorised to access the page. 
</p> 
<?php 
    require_once "includes/sidebar.php"; 
    require_once "includes/footer.php"; 
?> 

的header.php

<?php 
    include_once 'includes/db_connect.php'; 
    include_once 'includes/functions.php'; 
    sec_session_start(); 
?> 
<html> 
    <head></head> 
    </body> 
    <?php if (login_check($db) == true) : ?> // this if I am trying to end in footer.php 

footer.php

<?php else : ?> 
    <?php header('index.php'); 
    <?php endif; ?> 
    </body> 
</html> 

但我得到Parse error: syntax error, unexpected end of file in header.php on line 9这将是与<?php if (login_check($db) == true) : ?>线。我在哪里弄错了。

回答

0

做到这一点,而不是从HTML代码中删除:

if (login_check($db) !== true) { header('Location: index.php'); exit; }

把整个逻辑开始

+0

试过这个,它只在我登录时才起作用。如果我注销它不会去其他地方,将我重定向到index.php。 – user3467855

+0

有没有其他的需要。这段代码意味着如果我没有登录然后将我重定向到index.php,反正什么也不做,继续运行这个脚本。 (假设login_check在登录时返回true,如果没有则返回true) –

+0

在这种情况下,它的工作原理是这样的:即使我退出了,我仍然可以查看受保护的内容,而不是将其重定向到'index.php' – user3467855

0

删除header.php中第9行顶部的“:”。空方括号替换

<?php if (login_check($db) == true) {} ?> 
+0

差不多,现在我得到了'Parse error:syntax error,unexpected'else'(T_ELSE)in footer.php on line 1'。在这种情况下我将如何替换括号? – user3467855

+0

remove <?php else:?><?php header('index.php'); <?php endif; ?>替换为<?php else {require('index.php'); }?>' – mansuetus

+0

仍然是同样的错误。 – user3467855

0

PHP首先解析文件,然后执行它们,这意味着他们每个人都需要是完整的和正确的。不幸的是,你不能跨多个文件跨越if语句。

0

if子句不能跨越多个文件。您必须将整个if-logic移动到一个PHP文件中或将其添加到两个文件中。

0

的IF条件结构应该是:

if($a) { 
    echo $a; 
} 
else: 
    echo $c; 
endif; 

那么你将有一个插入ENDIF;在条件结束时。

这个link会给你一个想法。

相关问题