0

模板文件是 .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;

函数没什么特别的('functions.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 是可读的并命名为 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>

index.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;
?>

我希望这里有人知道导致这种行为的原因......

4

1 回答 1

1

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

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

于 2012-06-03T23:12:01.300 回答