How to make php create an html file with rendered php code? - php

I've just begun learning php and I have created a php page that creates pages dynamically. For example I have used includes for menu.php, header.php and so on.
I would like php to create a html file that contains the rendered php automatically.
My project:
Every time I refresh the index page completely different content loads and I would like to have It automatically create a html version.

If I understand correctly, you want to store the output generated by the PHP script to a file, right? In this case, you could use output buffering:
<?php
// Turn on output buffering
ob_start();
// Create the HTML page content
print '<!DOCTYPE html><html><head><title>test</title></head><body><button type="button">Button</button></body></html>';
// At the end of the PHP script, write the buffered content to a file
$content = ob_get_clean();
file_put_contents('/my/html/pages/page.html', $content);
// Output the HTML to the browser
print $content;
?>
Visit php.net for more info.

Related

How can I stop html outputting?

I am writing an anti-ddos php script, and I want to write a custom load page, but the html page of the orginal html page would be rendered. Is there anyway for me to prevent the html showing up while showing up my php load page
<?php require "antiddos.php"?>
#Samuel Wang, you can try check that out by one of this examples.
below assume code of antiddos.php and other file from where you include it.
1. antiddos.php :
E.g. before your php code or you can keep html inside the file that won't be an issue.
<?php
echo"<p>This is a test html content of antiddos.php</p>";
function testcall($TextReceiver ){
return $TextReceiver;
}
function MathMultiplication($Prt1,$Prt2){
return $Prt1*$Prt2;
}
?>
2. testprocess.php :
You can use the ob_clean(); function next to your include file statement that will discards the contents / html output from the antiddos.php.
<?php require_once("antiddos.php");ob_clean();
echo testcall("Hello this is print call from testprocess.php to antiddos.php");
echo "<br>";
echo "MathMultiplication= ". MathMultiplication(4,3);
?>
Conclusion by both the files output: First from the antiddos.php and second one from the testprocess.php

Can I assign an include_once file to a $var with PHP?

I am trying to roll my own MVC purely for learning purposes, but am hitting a snag when I try to load in the view files. Here's my problem:
Front controller grabs appropriate page controller, page controller include_once() the appropriate view. The view is a file containing HTML and PHP. When I return the include file from the controller, the included file content appear out of sequence of the PAGE layout intended. (example: I want to display HEADER CONTENT FOOTER, but when I include the view's contents, my controller spits out CONTENT, HEADER, 1, FOOTER).
In my front controller I am attempting to set my included file contents to a variable I then echo in between my HEAD and FOOTER in the PAGE template. But this does not work as intended. I can wrap my view file contents in: return "code" but I don't want the added headache of worrying about stray quotes/double quotes in my view code that could break the script. I have tried file_get_contents(), but this only spits out a string (albeit in the right place/sequence in my template) and does not the PHP in my view files correctly (ie at all).
My question is: Is there a way I can return my view file to my front controller using include_once() in a variable? I can't seem to find a adequate answer to this. I'd be happy to add my code if it would help.
You can assign a return value from an include(). A lesser-known feature of this function is that you can have a file like this:
<?php
// config.php
return array('database_host' => 'localhost');
and you can read it in thus:
<?php
$config = include('config.php');
However, you're wanting to do this:
<!-- text.html -->
<h1>Heading</h1>
<p>Hello</p>
and read it in with this:
$html = include('text.html);
You can try doing that, but as soon as the include() scans the file, the output will be sent to the web server. That's how PHP works. Since there is no explicit return value, moreover, the $html variable comes back empty.
Thus, we use the output buffering functions to modify PHP's behaviour: we start buffering, and then we fetch it and clear it. This is then not sent to Apache, unless of course you choose to turn off output buffering and echo what you have collected, in which case it goes back into the normal output buffer and is actually sent.

php post-processing publish

I was wondering if is it possible to change an already written variable in the html generated file. Maybe there is an option to 'publish/write' the html file in the very end of php processing.
<html>
<?php
echo '<h1>' . $pageTitle . '</h1>';
?>
[...]
<?php
[DB queries]
$pageTitle = "New Page title";
echo "<javascript-code-to-change-the-page-title>";
?>
Yes I could set $pageTitle before, but it may change along the code according to some query.
So, I figure out that I can change the page title on client side only.
I'm probably missing some logic here.
You could use a combination of (A) PHP's Output buffering, and (B) PHP's DomDocument class.
Basically, you would capture the HTML output by wrapping the output in ob_* commands. Once you get the output, you throw it in the DOM parser. Once you're there, you can then traverse the DOM document and make your changes. Afterwards, you can dump everything back out to the browser.
I most definitely would not rely on JS to do any changes on the page.
ALTERNATIVELY, you could do your PHP at the top of the file, then echo the variables out as needed.

replacing file contents in php and saving to a new file

So I would have a template file called template.php and inside, it would have:
<html>
some other content
<?php $name ?>
</html>
Essentially, I want to pull the name from the database and insert it into $name and save that entire page as a new file.
So let say in the database, I had a name called Joe, the new file I create would have the following content inside:
<html>
some other content
Joe
</html>
The only issue I am having is finding the right functions to actually replace the variable from the template file, and being able to save it into a new file.
That's not how templates usually work. You should have a template file, substitute variables for their values, and display it to the user. You can cache these generated templates (the ones that are always the same) but you never make a new template file for each user and save it permanently. That makes managing and updating the application way more difficult than you want.
I recommend using a simple template system like RainTPL. That's what I use for most of my projects... it's very simple to use and provides all the basic functionality you would need.
You can also use just PHP for this... there are a few answers on SO that cover this so I won't get into it.
Using PHP as template engine
Using Template on PHP
Look at the answer I gave to this question a while back. There I explain how "variable parsing" usually works.
When I create a template loader I generally use output buffering with php include. This allows you to run the page as a php file without displaying its content before you are ready. The advantage to "parsing" your php files this way is that you can still run loops and functions.
Here's an example of how to use output buffering with PHP to create what you're wanting.
The Template template.php
<html>
some other content
<?php $name ?>
</html>
Where your database code and stuff is index.php
<?php
$name = 'John Doe';
if( /* some sort of caching system */ ) {
$parsed_template = file_get_contents('parsed_template.html');
}else {
ob_start();
include 'template.php';
$parsed_template = ob_get_clean();
file_put_contents('parsed_template.html', $parsed_template);
}
echo $parsed_template;
?>

getting the entire file code on the page while using include/ include_once/ require_once

While using include / include_once / require_once of the form include 'filename.js';
I get the entire code of the file displaying on the page. What am i doing wrong?
The included file has the text at the global level in the script. If you want the text to only be output on demand then you will need to put it inside a PHP function, and then call that function when appropriate.
You don't include a javascript file within the PHP script. You can embed it within an inline in the page, or more appropriately, you should have the following in your PHP (as part of the HEAD section of your returned page) so the client retrieves the JS file separately:
echo "<script type=\"text/javascript\" language=\"javascript\" src=\"".$js."\"></script>\n";
Where $js is a variable pointing to the script URL.

Categories