Jump to content

Equivalent of PHP:Require for .html Files


OtagoHarbour

Recommended Posts

I was not sure if I should post this in the PHP or html folder.I would like a shorthand way for the same header (with link buttons etc.) to appear on every every page of my web site. I have found

<?php require("Whatever.php"); ?>

to be really good for .php files but was wondering if I could use the suffix .php for every page of my web site, including the home page. I would be grateful if someone could tell me whether:1/ There would be a problem with using a .php suffix for every page of my web site.2/ If there is an equivalent of PHP:require for files with the suffix .htmlMany thanks in advance,Peter.

Link to comment
Share on other sites

You can certainly use the include directive to "paste" sections of HTML into any document you choose. This is common for embedding common banners, menus, footers and so on. The included files can end in .html unless they also contain PHP code that needs to execute.I hope I understand the question.Also: there is no problem if every page on your site uses a .php extension. It's very common.

Link to comment
Share on other sites

The included files can end in .html unless they also contain PHP code that needs to execute.
They can actually be named whatever you want, PHP will always parse it as a regular PHP file (i.e., look for PHP tags, treat everything else as output). Entering the URL of the included file directly will not execute the PHP code if the extension is different, but when the files are included they always get treated as PHP files.
Link to comment
Share on other sites

They can actually be named whatever you want, PHP will always parse it as a regular PHP file (i.e., look for PHP tags, treat everything else as output). Entering the URL of the included file directly will not execute the PHP code if the extension is different, but when the files are included they always get treated as PHP files.
i see, then that's actually a good security measure also, ie, to NOT name your PHP files with the .php extension ?
Link to comment
Share on other sites

I usually just hide everything with .htaccess files. There are also short and simple code techniques you can use to keep files from executing without first being included in something.

Link to comment
Share on other sites

If you don't care about people viewing your PHP code, which is probably more of a threat than rogue code somehow finding its way into one of your files.
ahh, yes - if they *know* the include-s filename then it is a problem.
Link to comment
Share on other sites

Best will be if you put the include files outside of web root. so wheather anyone knows the include file he cant access your file isolately.

Link to comment
Share on other sites

Best will be if you put the include files outside of web root. so wheather anyone knows the include file he cant access your file isolately.
i see, i think...good tip - thanks !
Link to comment
Share on other sites

You can certainly use the include directive to "paste" sections of HTML into any document you choose. This is common for embedding common banners, menus, footers and so on. The included files can end in .html unless they also contain PHP code that needs to execute.I hope I understand the question.Also: there is no problem if every page on your site uses a .php extension. It's very common.
Thank you for your help. I couldn't get the include to work. I tried
<!--#include FILE="Menu.html" -->

and

#include FILE="Menu.html"

in the head and in the body but nothing worked. So I just changed the suffix to .php and used PHP as needed.Thanks,Peter.

Link to comment
Share on other sites

Thank you for your help. I couldn't get the include to work. I tried
<!--#include FILE="Menu.html" -->

and

#include FILE="Menu.html"

in the head and in the body but nothing worked. So I just changed the suffix to .php and used PHP as needed.Thanks,Peter.

that's because it's
<?php include(file.ext); ?>

http://www.w3schools.com/PHP/php_includes.asp

Link to comment
Share on other sites

They can actually be named whatever you want, PHP will always parse it as a regular PHP file (i.e., look for PHP tags, treat everything else as output). Entering the URL of the included file directly will not execute the PHP code if the extension is different, but when the files are included they always get treated as PHP files.
Thanks. I have fund that making every file a .php file is the easiest approach, for me anyway.
Link to comment
Share on other sites

Thanks. But is seems that that would require the .php suffix which would enable me to use require() anyway. Is there a way to do it with an .html suffix? Would that prevent people from seeing my .php code (not that it's very sophisticated anyway)?Thanks again,Peter.
It is not obvious it should be .php it can be anything. as justsomeguy stated at post 3 '.php' will be parsed where other will not be.require and include works same the difference is in require it will cause a FATAL ERROR and hault the scripts if the function fail. where include will run anyway throwing some warning.somepage.php<?phpinclude 'htdocs/file1.php'; <= it is in web root it will parse file1 and will include it. It will be publicaly available to world. means if some one access yoursite.com/file1.php it will be executedinclude 'htdocs/file2.html'; <=it is in web root it will NOT parse file2 and will include it as it is (plain text). Same will be other extenstion which is not meant to be parse by server. It will also be publicaly available to world. means if some one access yoursite.com/file2.html it will be availableinclude 'root/file1.php'; <= It is outside web root it will parsed when included. but wont be available in world. no one can accessinclude 'root/file2.html'; <= It is also otside web root it will include as it is without being parsed. same with other extension. it will not available also publicaly?>the code of somepage.php will not be visible to the user as it is being parsed (.php).
Link to comment
Share on other sites

Even if you use the .php extension, people won't be able to see your PHP code, anyway. There's no security risk to using the .php extension over another one.

Link to comment
Share on other sites

It seems that would require a .php suffix. I tend to use require() in that case.Thanks,Peter.
there's a specific reason i used a 'generic' suffix like .ext (stands for extension). However, I don't think you've been quite understanding what we've been telling you. I suggest you re-read the replies in this thread, in particular the one's from DD and JSG.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...