0

The template file is .php and has those placeholders:

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

And this is the code which replaces them:

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

Functions are nothing special ('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();
}

First counts the messages and outputs number with some styling, the second adds to the menu 'Admin Panel' link if the username is 'admin.

The problems are (no errors in php log): count_messages() works but outputs 'n unread' above all elements on the page. show_link() doesn't output the link.

The file $template is readable and named 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>

The 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;
?>

I hope someone here knows what causes this behaviour ...

1000Gbps
  • 1,455
  • 1
  • 29
  • 34

1 Answers1

1

You are using the functions’ result values in the str_ireplace function call but the functions don’t return anything, they are missing a return statement.

You probably meant to use return ob_get_clean(); instead of ob_end_flush(); in your code.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Well, I returned the results, but I've only got the number 1 in both placeholders: 1Messages for first 1 for second – 1000Gbps Jun 03 '12 at 23:13
  • `ob_end_flush()` writes the results to output. Are you thinking in terms of `ob_get_clean()`? – DaveRandom Jun 03 '12 at 23:14
  • @KonradRudolph Although you've actually hit the nail on the head. He is basically passing `NULL` to `str_ireplace()` because he probabaly *should* be calling `ob_get_clean()`... or just working with string vars properly – DaveRandom Jun 03 '12 at 23:15
  • @KonradRudolph +1 for you now :-D – DaveRandom Jun 03 '12 at 23:17
  • Yes, I know that it doesn't return anything now, cause I already tested that and reverted the code to the original. And using ob_get_clean() now even won't return 'n unread' problem. – 1000Gbps Jun 03 '12 at 23:22
  • @1000Gbps Well, it’s wrong. It *needs* to return something, since you are *using* the return value. Try it with a minimal code and you’ll see. – Konrad Rudolph Jun 03 '12 at 23:25
  • Ok, it works now, memory of the server was eaten from ... something. Restarted the web-server, this is rly weird 0o Anyway thanks for the cooperation :) – 1000Gbps Jun 03 '12 at 23:27