尝试在 html 中替换两个 php 函数时会出现奇怪的行为


Weird behaviour when trying to replace two php function results in html

模板文件是.php的,并且具有以下占位符:

<ul>
<li><a href='a.php'>{{placeholder1}}</a></li>
{{placeholder2}}
</ul>

这是替换它们的代码:

$file = file_get_contents($template);
$file = str_ireplace('{{placeholder1}}', count_messages(), $file);
$file = str_ireplace('{{placeholder2}}', show_link(), $file);
print $file;
函数

没有什么特别的('函数.php'):

function count_messages()
{
  ob_start();
  if (preg_match('/^[A-Za-z0-9_]{3,40}$/', $_SESSION['username']))
  {
    $table = $_SESSION['username'];
  }
  else
  {
    header('Location: login.php');
    exit();
  }
  try
  {
    $db = new PDO('sqlite:site.db');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $result = $db->prepare("SELECT Count(*) FROM `$table` WHERE `isRead` = '0'");
    $result->execute();
    $count = $result->fetchColumn();
    if ($count > 0)
    {
      print "<b style='color: #00ff00; text-decoration: blink;'>$count</b> <b style='font-size: 6pt; text-decoration: blink; text-transform: uppercase;'>unread</b> ";
    }
    unset($db);
  }
  catch(PDOException $e)
  {
    echo $e->getMessage();
  }
  ob_end_flush();
}

function show_link()
{
  ob_start();
  if ($_SESSION['username'] == "admin")
  {
    print "<li><a href='admin_panel.php' target='main_iframe'><b style='color: #ffff00;'>Admin Panel</b></a></li>;
  }
  ob_end_flush();
}

第一个计算消息并使用一些样式输出数字,如果用户名为"admin",则第二个添加到菜单"管理面板"链接。

问题是(php日志中没有错误):count_messages() 有效,但在页面上的所有元素上方输出"n unread"。show_link() 不输出链接。

文件$template是可读的,并命名为模板.php:

<?php
session_start();
if(!$_SESSION['islogged'])
{
  header('Location: login.php');
    exit();
}
require_once('functions.php');
?>
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta charset="UTF-8" />
<meta name="description" content="Documents" />           
<link rel="stylesheet" type="text/css" href="style.css" />
<title>Documents</title>
</head>
<body>
<div id="main">
<iframe src="documents.php" name="main_iframe" id="main_iframe">
</iframe>
</div>
<div id="main_menu">
<ul id="menu_list">
<li><a href="messages.php" target="main_iframe">{{placeholder1}}Messages</a></li>
{{placeholder2}}
<li><a href="logout.php" style="font-weight: bold; color: #ff0000">Log out</a></li>
</ul>
</div>
</body>
</html>

该指数.php:

<?php
session_start();
require_once('functions.php');
$template = 'template.php';
if (file_exists($template))
{
  if (is_readable($template))
  {
    if(!$_SESSION['islogged'])
    {
      session_destroy();
      header('Location: login.php');
      exit();
    }
  }
  else
  {
    print "Template file cannot be opened";
  }
}
else
{
  print "Template file doesn't exist";
}
$file = file_get_contents($template);
$file = str_ireplace('{{placeholder1}}', count_messages(), $file);
$file = str_ireplace('{{placeholder2}}', show_link(), $file);
print $file;
?>

我希望这里有人知道是什么导致了这种行为......

您在str_ireplace函数调用中使用函数的结果值,但函数不返回任何内容,它们缺少return语句。

您可能打算在代码中使用return ob_get_clean();而不是ob_end_flush();