解析错误:语法错误,单独文件上的意外文件结束

时间:2014-04-02 12:30:17

标签: php

我正在尝试将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) : ?>一致。我错在哪里。

5 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

执行此操作并将其从HTML代码中删除:

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

将整个逻辑放在开头

答案 3 :(得分:0)

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

答案 4 :(得分:0)

if条件结构应为:

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

所以你将插入一个endif;在条件结束时。

这个link会给你一个想法。