我正在尝试将 if 条件的开头和 if 条件的结尾添加到包含在另一个文件中的单独文件中。
我有一个名为 cp.php 的文件,其中包括页眉.php和页脚.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";
?>
标头.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
页脚.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) : ?>
的界限.我哪里弄错了。
改为执行此操作并将其从HTML代码中删除:
if (login_check($db) !== true) { header('Location: index.php'); exit; }
把整个逻辑放在开头
删除标题中第 9 行顶部的":".php。替换为空括号
<?php if (login_check($db) == true) {} ?>
PHP 首先解析文件,然后执行它们,这意味着它们都需要完整且正确。遗憾的是,您不能跨多个文件跨越 if 语句。
if
子句不能跨越多个文件。您必须将整个 if 逻辑移动到一个 PHP 文件中,或者将其添加到两个文件中。
if 条件结构应为:
if($a) {
echo $a;
}
else:
echo $c;
endif;
所以你会插入一个endif;在条件的末尾。
此链接将为您提供一个想法。