Alternative for ob_get_contents() for mixed PHP/HTML file - php

I'm trying to parse a mixed HTML/PHP file (viewfile.php) to output it into a html email using the following code:
<?php
//define variables and objects to be used in the view
...
ob_start();
include 'viewfile.php';
$email_content=ob_get_contents();
ob_end_clean();
send_email($email_content);
?>
The viewfile.php to be included looks somehow like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en-US">
<head>
<meta charset="UTF-8">
</head>
<body>
<p>Dear <?= $name ?>,</p>
<p>You've purchased the following items:</p>
<?php foreach ($items as $i){?>
<p><?= $i['name'] ?><br/>
<?php if (isset($i['item_options'])) foreach ($i['item_options'] as $io) {?>
<?= $functions->option_name($io['option']).': '.$functions->option_value($io['value']) ?><br/>
<?php }
}?></p>
<p>Best wishes</p>
</body>
</html>
Unfortunately, my webhost doesn't have output_buffering. So, the functions ob_start(); ob_get_contents(); and ob_end_clean(); don't work.
Is there any other way to read file into a variable and parse all the PHP in there.
I'm using PHP 5.4.
Thank you very much for your help.

Related

Php templating - Getting the html right

I'm developing a web app and I'm having a problem with the templating fase. The problem concerns the generation o html code in a php object. The code is as follows:
<?php class abcHtml {
function prepare() { ?>
<!DOCTYPE html>
<html>
<head>
<?php echo $this->meta; ?>
</head>
<body>
<div id="bg">
<div id="container_wrapper">
<?php echo $this->cont; ?>
</div>
</div>
</body>
</html>
<?php }} ?>
The goal is to have a php file with the cleanest html code possible(Don't want to use any framework).
<?php
class abc extends abcHtml
{
protected $meta;
protected $cont;
public function render(){
return parent::prepare();
}
public function setMeta($meta){
$this->meta = $meta;
return $this;
}
?>
This class inherites abcHtml to make possible to add diferent html modules. The problem is that when calling render(), the returned html code is note in the right order, the html code in the $meta and $cont variables appear before the html that is inside the prepare function.
For example:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css" href="public/css/base_style.css"/>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="bg">
<div id="container_wrapper">
</div>
</div>
</body>
</html>
This problem obviously means that php is running the php code first and then attaching the html. Is there a solution to fix this?

PHP redirection returns "Cannot modify header information"

I currently have a very simple page that redirects to another URL. However for some reason, I cannot get this to work.
I'm sure there is a very simple solution to this problem and any help to make me understand exactly what's going on would be appreciated.
Here is my page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test - Youtube</title>
</head>
<body>
<?php
ob_start();
header('Location: vnd.youtube:xjTEqVQVsQs');
ob_end_flush();
?>
</body>
</html>
Thanks
You are having some Outputs on your page before sending header and that's why you can not redirect, you should use output buffering like this:
<?php
ob_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test - Youtube</title>
</head>
<body>
<?php
header('Location: vnd.youtube:xjTEqVQVsQs');
die();
?>
</body>
</html>
<?php
ob_end_flush();
?>
<?php
ob_start();
header('Location: vnd.youtube:xjTEqVQVsQs');
ob_end_flush();
?>
remove all the html except above php code if it's only a redirect page.
This is how the code should look like:
<?php
ob_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test - Youtube</title>
</head>
<body>
<?php
header('Location: vnd.youtube:xjTEqVQVsQs'); // Are you sure that's the right
address?
?>
</body>
</html>
<?php
ob_end_flush();
?>
Anyway, the page is redirecting to another page, why do you need the html tags?
You should remove all the page content except PHP script:
<?php
ob_start();
header('Location: vnd.youtube:xjTEqVQVsQs');
ob_end_flush();
?>
Or
put your PHP script on top of the page:
<?php
ob_start();
header('Location: vnd.youtube:xjTEqVQVsQs');
ob_end_flush();
?>
Make sure there is no empty space before php tag above.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test - Youtube</title>
</head>
<body>
</body>
</html>
As redirect sends headers to the browser it MUST be sent before any output.
You can use the method below to redirect using a different method depending on whether the headers have been sent:
<?php
function redirect($filename) {
if ( ! headers_sent())
header('Location: '.$filename);
exit; // just a good practice to EXIT after redirecting
else {
echo '<script type="text/javascript">';
echo 'window.location.href="'.$filename.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$filename.'" />';
echo '</noscript>';
}
}
redirect('http://www.google.com');
?>
This will fallback to using javascript if the headers have already been sent.
If you want to use the header() method it must be at the top of your code before any other output (including whitespace).

embeding php in html

is it possible to embed this into html
if (empty($_POST['extras'])) {
$message .="<br /> No extras selected <br />";
} else {
foreach($_POST['extras'] as $extra)
{
$message .="<br />Extras: ".$extra."";
}
}
I would like to place the above php statement at the bottom of this html code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Booking System</title>
<link rel="stylesheet" href="css/bs-admin.css" type="text/css" />
</head>
<body>
<noscript>
<div class="js_error">Please enable JavaScript or upgrade to better browser</div>
</noscript>
<div id="index">
<h1>Thank you for your reservation!</h1>
<p>
<h3>Your Booking is as follows:</h3>
<p>Dear <b><?php echo $custInf[0] ?></b>,
<p>You have Booked: <?php echo $eventInf[0] ?>
<p>Booking Date: <?php echo $eventInf[2] ?>
<p>Booking descriptiong: <?php echo $eventInf[1] ?>
<p>Number of machines booked: <?php echo $qty ?>
<p>Street: <?php echo $comments ?>
<p>Suburb: <?php echo $suburb ?>
<p>Postcode: <?php echo $postcode ?>
<p>Dropoff: <?php echo $dropoff ?>
<p>Duraton: <?php echo $duration ?>
If it's got php code in it then it's no more HTML.
You have to call it .php or .phtml.
PHP generates, or outputs html.
You can have pure html in .php scripts (outside the <?php ?> tags), but not the other way around (i.e. no php code in regular .html files).
If you want to add some logic (the PHP code) within it, you need to have it parsed by a webserver which will, in turn, generate html.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Booking System</title>
<link rel="stylesheet" href="css/bs-admin.css" type="text/css" />
</head>
<body>
<noscript>
<div class="js_error">Please enable JavaScript or upgrade to better browser</div>
</noscript>
<div id="index">
<h1>Thank you for your reservation!</h1>
<div>
<h3>Your Booking is as follows:</h3>
<p>Dear <b><?php echo $custInf[0]; ?></b>,</p>
<p>You have Booked: <?php echo $eventInf[0]; ?></p>
<p>Booking Date: <?php echo $eventInf[2]; ?></p>
<p>Booking descriptiong: <?php echo $eventInf[1]; ?></p>
<p>Number of machines booked: <?php echo $qty; ?></p>
<p>Street: <?php echo $comments; ?> </p>
<p>Suburb: <?php echo $suburb; ?></p>
<p>Postcode: <?php echo $postcode ?></p>
<p>Dropoff: <?php echo $dropoff; ?></p>
<p>Duraton: <?php echo $duration; ?></p>
</div>
<?php
$message = "";
if (empty($_POST['extras'])) $message .="<br /> No extras selected <br />";
else
{
foreach($_POST['extras'] as $extra)
{
$message .="<br />Extras: ".$extra;
}
}
echo $message;
?>
</div>
</body>
</html>
I think you're needing to create an empty variable $message before you could start appending "extras" to it. Then all you need to do is echo $message.
Yes you can, however that's not a good way to do it. You should go for the Model-View-Controller model. It separates the HTML code from the actual code that does processing. Many PHP Framework does this. (Though personally i find them too clunky and wrote my own)
Also, embedding HTML into PHP is bad, as again, code should be separated from the HTML by as much as possible.
Example of views and controllers (From my framework):
Controller:
class Controllers extends BaseController{
function index($args=array()){
// process data
$this->render('index', array('data1'=>$data));
}
}
View:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My site</title>
</head>
<body>
<h1><?php echo $page->data1; // echos out $data from the view ?></h1>
</body>
</html>
This is much cleaner than your model of embedding php into the HTML and/or vice versa.
Take a look at frameworks, they are usually pretty helpful, although PHP frameworks are generally very restrictive as to what you can do.
Yes, you can do what you are asking. Make sure the extension is recognizable by the php interpreter (usually .php)
If you need to hack something up quick, this is ok. But for anything else than that, look into using some sort of templating language. This becomes an important point because you want to seperate your logic from your display for the sanity of yourself and other developers that will work on your code in the future.
edit: oh, also very important. Don't use $_POST this way without sanitizing the data. It's ripe for XSS injections.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Booking System</title>
<link rel="stylesheet" href="css/bs-admin.css" type="text/css" />
</head>
<body>
<noscript>
<div class="js_error">Please enable JavaScript or upgrade to better browser</div>
</noscript>
<div id="index">
<h1>Thank you for your reservation!</h1>
<!--- bunch of stuff omitted here -->
<?php
if (empty($_POST['extras'])) {
echo "<br /> No extras selected <br />\n";
} else {
foreach($_POST['extras'] as $extra)
{
echo "<br />Extras: ".$extra."\n";
}
}
?>

pChart does not render to web browser, only shows missing image

pCharts documentation says that you should be able to render the image to the browser using this code.
mypic.php
$myPicture->stroke;
mypage.html
<IMG SRC=‘mypic.php‘>
The img tag is supposed to invoke the php script. Within the PHP script, the stroke function sets the content-type: image/png.
So here is what I have:
netsales.php
<?php
include('../class/pData.class.php');
include('../class/pDraw.class.php');
include('../class/pImage.class.php');
/* query sales and create new image */
$myPicture->stroke;
?>
index.php
<?php
include ('netsales.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div>
<img src="netsales.php" />
</div>
</body>
</html>
I'm not getting any errors, just the red X for a missing image.
try removing the
<?php
include ('netsales.php');
?>
From index.php, and add
header('Content-Type: image/png');
Before the Stroke call.
I try something like this and works:
//html file//////////////////////
<?php include ('my.php'); ?>
<html>
<head></head>
<body>
<div> <img src="my.php" /> </div>
</body>
</html>
///////////////////////////////////
//my.php file/////////////////////////
<?php
/* pChart library inclusions */
include("pChart/class/pData.class.php");
include("pChart/class/pDraw.class.php");
include("pChart/class/pImage.class.php");
//...
$myPicture->autoOutput("picture.png");
?>
///////////////////////////////////
I just got it working for me I had comment include function in HTML file:
<?php //include ('my.php'); ?>

interpreting php as plain text when using php function require

I have main site to which i put code using require function, but when i add php functions into it, they appear as plain text. The php code is rather long so i won't paste it here. Could anyone help me with that?
Thanks
ok i am adding some code:
require part with only one function:
<?php
$content=<<<EOF
echo 'hello';
EOF;
require 'whatever.php';
?>
and main part:
<?php
echo <<<CONT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
...
</head>
<body id="body">
<div id="container">
$content
</div>
<div id="add_func">
</div>
</body>
</html>
CONT;
?>
Make sure that You wrap your code in <?php.....?> tags.
Update:
No need to use the heredoc syntax for what you are doing, you could modify you code like this:
<?php
echo 'hello';
require 'whatever.php';
?>
And:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
...
</head>
<body id="body">
<div id="container">
<?php echo $content; ?>
</div>
<div id="add_func">
</div>
</body>
</html>
just get rid of heredoc
require part
<?php
echo 'hello';
require 'whatever.php';
?>
main part
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
...
</head>
<body id="body">
<div id="container">
<?php echo $content ?>
</div>
<div id="add_func">
</div>
</body>
</html>
that's all
PHP is not Perl, heredoc is useless here, even if properly used.
Just never use this ugly syntax.

Categories