11

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?

vedarthk
  • 1,253
  • 1
  • 14
  • 34
Joe Slater
  • 2,483
  • 6
  • 32
  • 49

8 Answers8

19

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

Nauphal
  • 6,194
  • 4
  • 27
  • 43
  • lol..how on earth did I forget about the plain old `include()`... – asprin Mar 07 '13 at 11:46
  • Thanks for the answer... But now I just have another question. do you know if I can edit a certain div within this php file. Like a div with a certain id? – Joe Slater Mar 07 '13 at 11:50
  • 1
    @AnkurSharma: [How to parse and process HTML/XML with PHP?](http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-xml-with-php) - Improve your search skills and you will get a lot of good answers to all your programming questions in no time. – hakre Mar 07 '13 at 11:54
12

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');
Prijm.com
  • 256
  • 1
  • 5
3

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");
}

Tsimtsum
  • 981
  • 9
  • 27
3

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';
    }
}
?>
asprin
  • 9,579
  • 12
  • 66
  • 119
3

Use functions like

include()
include_once()
require()
require_once()
file_get_contents()
Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82
2
<?php
if(some condition)
{
    //Dont allow access
}
else
{
    echo file_get_contents("your_file.html");
}
?>

This should do the trick

Or, as nauphal's answer say, simply use include()

Don't forget that, if file doesn't exists, you could have some trouble (so, maybe, check before include or getting content)

Community
  • 1
  • 1
DonCallisto
  • 29,419
  • 9
  • 72
  • 100
1

I think you want to include your HTML file or have I misunderstood the question.

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    include ("..html/myFile.html");
}
?>
AMadmanTriumphs
  • 4,888
  • 3
  • 28
  • 44
-1

Way 1:

ob_start();
include "yourfile.html";
$return = ob_get_contents();
ob_clean();

echo $return;

Way 2: Use templaters, like CTPP, Smarty, etc... Templaters are useful to transfer some logic from php to template, for example, in CTPP:

$Templater -> params('ok' => true);
$Template -> output('template.html');

in template html:

<TMPL_if (ok) >
ok is true
<TMPL_else>
ok not true
</TMPL_if>

The same ideas are in other templaters. Templaters are better, cause it helps you to standartize your templates and send all primitive logic to them.

Andrej Bestuzhev
  • 674
  • 6
  • 10