I'm trying to add some of my own PHP to a nearly-unreadable template file on a forum system. I know it all works perfectly as far as the server config, etc. goes, but PHP simply doesn't parse on this page. JS works fine. Any ideas? It's a simple .html page.
Try adding this to your .htaccess (or creating a new one in the appropriate directory):
AddType application/x-httpd-php .php .html
Is it possible that the template file is being read into a variable rather than being included or required? Eg. it's loaded using file_get_contents or something similar?
If this is the case you may need to eval() the template code after it has been loaded as file_get_contents does not parse php code, it just loads the text as it is into a variable. It's a very ugly solution but it may work for you. Please be careful if you do this as it does open up a whole can of security issue worms.
A lot of template systems use their own coding syntax. If this is the case it will not be possible to include PHP code in your template without opening up a lot of security holes.
Try to learn the specific templating language used, or find out where you should put the code without changing the template file (there may be a controller or plugin system built for such stuff).
Your page has to be a .php page.
Related
I named a file index.php while writing an app that uses PHP, Javascript and jQuery. In the end I realized there is no PHP code in the index.php file, so I changed the file extension to .html and, after some testing, it seems there are no problems.
Everything that touches the HTML in the index.html file is JavaScript. I'm reading fields from a form, writing to a MySQL database, and then reading the database and writing back to the HTML. The JavaScript is making Ajax requests to various PHP files.
If I use the filename index.html, am I likely I run into trouble if I move the files to a different Web server?
There is no reason to give a file a .php file extension if it doesn't contain any PHP.
There are various different reasons not to build your page so it only works if JavaScript is available though (which implies that the file should contain PHP, given that is your server side language of choice).
I prefer .php because if you want after time add some php code there you just cant.In .html cant be php code.
Please help me to understand how not to overcomplicate my site.
For example I have files:
index.php
other.php
etc...
these files have such code
include '../dir/index.php';
include '../dir/other.php';
etc.
How to have url like "site.com/news.php" without having actual file news.php, because only one thing it does is including code?
You have to configure your Apache to send all request to you main PHP script. You can do that via different ways. The "regular" way would be to use a .htaccess file and some mod_rewrite settings. Then within your main script you have to handle the problem of not finding the file which was requested. But be careful you can create a lot of security sink holes. For example if you just parse the request parameters and without sanitizing include a file.
For that you should just look at some modern PHP frameworks like Symfony, Laravel and others. There this kind of stuff is already handle quite well.
So I have a website with more than 10 pages. Now I want to export the head section, the header and the footer to other files, in order to facilitate further editing of any of those section, without having to go through every single page. Is it possible to make a header.html and have the other html files import the code from the separate file?
What you're describing is an "include" and here's how to do it with PHP. I recommend using a server side language like PHP to do the include because it will definitely be indexed by Google and you don't need to worry that the user has javascript disabled or poorly supported.
First of all, you won't be able to do this unless your webpages are either saved as PHP (with .php extension) or your HTML pages (.html extension) are being parsed by PHP. Obviously you'll also need PHP running on your server.
Take the chunks you want to "include" and save them into their own files (header.php, footer.php, etc.). Put them in their own folder under your assets area (such as a directory called includes or inc). Then, in your web pages, use PHP to include those files:
<?php include_once($_SERVER['DOCUMENT_ROOT'].'/include/header.php') ?>
I like to use the $_SERVER['DOCUMENT_ROOT'] variable so I don't need to remember the relative path to the include directory. Use that include line exactly where you would place the original header html.
If you want to keep your files as HTML but have them parsed as PHP, you'll need to make sure your server is parsing HTML files as PHP. You can direct the server to do this in the .htaccess (assuming Apache) where you'll need a line like this:
AddHandler application/x-httpd-php .php .htm .html
This line will be different for different host environments, so you may need to tweak that.
You could move your header and associated files to an html file and just call jQuery load to bring it in.
USAGE
http://api.jquery.com/load/
You can also use PHP to do a php include_once of your header.php file. This is similar to what wordpress does!
USAGE
http://php.net/manual/en/function.include-once.php
Good luck and let me know if theres other questions!
Yes, your problem is a general problem of preventing code duplication. As usual with general problems this problem was addressed a lot with a lot of different approaches over the years.
And there are more possible solutions, it would be a very hard task just to list all the possible solutions, so my answer will be very vague.
You can use the object tag to reuse your structure
You can load it with a client-side language, like Javascript (or using its very popular library, called jQuery)
Finally, you can generate your HTML using a server-side language
You can't do this with CSS. You can call those HTML contents from separated files using Ajax (it's pretty simple with jQuery) or you can directly include the files using PHP.
As a matter of precaution i always, now, name my index page: index.php, I do this whatever the project. Does anyone use index.html regularly? Can you be concretely sure you'll never need PHP for the page? Are there any performance issues encountered by always using index.php over index.html?
For servers that don't support PHP, avoid the .php extension unless you are trying to mask the server technology by faking a .php extension.
For static sites it doesn't really matter which extension you use as long as you know your server is configured correctly (see Dominic Rodger's answer). For that matter, not many of your visitors will care whether it's a static or dynamic site. Also, some dynamic sites accept URLs that end in .html as opposed to .php.
Are there any performance issues encountered by always using index.php over index.html?
The PHP interpreter will immediately hand your output back to your web server if there is absolutely no PHP code in it (all it does is send some engine-specific headers), so the performance difference is negligible if at all existent.
You should use index.html, and then if you decide you need PHP, create an index.php, and change your DirectoryIndex directive (if you're using Apache).
on most webservers ".html" files will not get parsed with the php interpreter. so i think, yes, there is an speed advantage.
i use .html files for very small sites, without anything special, shure, why not. the will never get updated so there is no need for it.
Of course...if u have static site u know that u wont have PHP code.
i guess that for .php files the server has to parse the file even if it doesnt contain any php tags or code, but i think that its really negligible...
For sure, the main topic's answer is that if you don't need anything in PHP for your site, you can use .html/.htm in the index page - as everybody stated.
But sometimes, I use it as a awesome trick: when I want to update some webpages or I want to fix some issue within the site or even say something for the visitors, I do an upload of an index.html page saying what I want. Note that, in this case, You will need to always use index.php for the site itself - the trick will work for sure.
Of course, your server need to accept PHP files :P
I hope I helped!
I have been developing my own PHP MVC framework. Now I have seen different frameworks implementing different extensions for the View files. I am using simply php extension for my view files.
Now is there anything wrong if i use php extension.
Are there any merits or de-merits of it?
Why use other extension such as:
phtml
etc
If you're talking about using these extensions in public facing URLs, then I would say don't use either:
File name extension. This is a very
common one. "cgi", even ".html" is
something which will change. You may
not be using HTML for that page in 20
years time, but you might want today's
links to it to still be valid. The
canonical way of making links to the
W3C site doesn't use the extension.
(Taken from W3C URL style guide)
You can achieve this with mod_rewrite, for example.
However, if you're talking about how to name your files in the filesystem, it's largely a matter of taste. I think both the extensions you suggested (phtml and php) make sense, the main thing is being consistent.
Edit: Also, since you said this is for a framework, you should consider choosing a non-standard extension may require extra webserver configuration. For example, to support both .phtml and .php in Apache:
AddType application/x-httpd-php .phtml .php
There's nothing wrong with using PHP extension if the code inside is valid PHP. It's nice to indicate somehow that a file is a view script. That's why some use .phtml. But I guess, you put them in a separate place-for-views anyway, right?
A benefit of .phtml is that it's obvious what kind of file it is when displayed in a "Jump to file" list. It's a feature of my IDE I use a lot: just typing a part of any file name in a project and picking the one to jump to.
It really doesn't matter what you use and as far as Apache goes its exactly the same if you are directly including the files.
Some use .tpl, some use .php and some use .phtml. Just pick the one you like the look of most.