At the moment I have a file like this
<?php
if(some condition)
{
//Dont allow access
}
else
{
echo "<html>My HTML Code</html>";
}
?>
But I wanted to do something like this to keep my php file short and clean.
<?php
if(some condition)
{
//Dont allow access
}
else
{
//print the code from ..html/myFile.html
}
?>
How can I achieve this?
Extending nauphal's answer for a more robust solution..
<?php
if(some condition)
{
//Dont allow access
}
else
{
if(file_exists("your_file.html"))
{
include "your_file.html";
}
else
{
echo 'Opps! File not found. Please check the path again';
}
}
?>
you may have a look at PHP Simple HTML DOM Parser, seems a good idea for your needs! Example:
// Create a DOM object from a string
$html = str_get_html('<html><body>Hello!</body></html>');
// Create a DOM object from a URL
$html = file_get_html('http://www.google.com/');
// Create a DOM object from a HTML file
$html = file_get_html('test.htm');
save your html content as seperate template and simply include it
<?php
if(some condition)
{
//Dont allow access
}
else
{
include ("your_file.html");
}
?>
OR
<?php
if(some condition)
{
//Dont allow access
}
else
{
readfile("your_file.html");
}
?>
readfile
is faster and less memory intensive than file_get_contents
Use this code
if(some condition) { //Dont allow access } else { echo file_get_contents("your_file.html"); }
OR
if(some condition) { //Dont allow access } else { require_once("your_file.html"); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With