I don't know how to put this better and tried search some around but I wasn't really sure on what to search either for my situation.
I'm in the unfortunate situation of managing a very old website, that has been passed by the hands of at least 5 webmasters.
The whole site is built of several hundreds of pages, each made of a single .html file; there is no controller logic or anything.
Now I need to implement a couple of scripts globally throughout the site, and I was wondering if there is a quick and dirty way to accomplish this.
I was thinking to encapsulate every request through a sort of rudimental PHP controller that simply read the requested file, require it in the script and render it with the same exact content and URL, but with the necessary scrpts included before the closing </body> tag.
Unfortunately I'm quite a novice in this, and I'm afraid also to allowing some bad injection from external sources. What is the best way to achieve something like this? I'm on a linux apache server.
Here are two approaches:
1) PHP Wrapper
Along the lines of what you're asking for, you can use your web server redirect all incoming HTML requests to a PHP file that does the following:
Takes a reference to the HTML file
Opens and reads the file in to a string
Makes the changes
Outputs the modified content in the response
If you are using a server like Apache httpd you can use mod_redirect to handle redirection of the requested URL to your PHP file.
This approach is a bit inefficient since for each request the HTML content needs to be read and modified. The approach below of editing the files will be higher performance.
2) Edit HTML Files
You can write a script to go through all the .html files and programmatically edit them.
One implementation to do this would be to open each file and add some lines at the tome and some others near the bottom. A the top of each file you could add a require() for your PHP file:
<? php
require('path/to/myinclude.php');
?>
At the bottom of the HTML before the </body> tag, call your function in the myinclude.php file, for example:
....
<?
my_footer_function();
?>
</body>
</html>
Depending on your webserver, you may be able to have the PHP interpreter execute for the .html extension so you don't have to change file extensions, if that's your wish.
This way, after this edit, you can modify all the behavior in the my_footer_function() in all your files simply by changing the one myinclude.php.
Related
Have a abstract how-to question which I haven't found a solution.
Lets say you built a plugin for a CMS like wordpress, I'm using a MCMS called GetSimple.
And now within that plugin.. when a button is clicked by the user... two external php files have their code ran and their output taken and put collectively into a single static file.. say a html or css file.
So then in this kind of scenario... how can you (within the plugin) run an external php file without effecting the current page you are on, then take that output and put it as a string into a variable.. repeat this for another php file... then take the two string outputs, merge them... then put them into a single static file?
This has proven to me to be a very difficult task.
for more details you can see this question:
https://stackoverflow.com/questions/15080163/how-to-create-a-file-with-php
So how would you go about doing this?
I was looking at the possibility of saving each file's output into separate xml files and then merging those xml files... but the problem still remains of running external php and putting that data somewhere without affecting the current page PHP you are on.
If you can generate the response on the server-side, you could simply run those PHP scripts using for example shell_exec() or using Symfony2 Process Component, then gather the results using file() or file_get_contents() functions.
If you need to have this things generated on button click, you must notify server to handle that tasks, and to do so you need to make an AJAX calls calling that scripts using methods I've told you above.
I'm developing a web application where an html page is created for the user. The page could include anything that the user puts in it. We take these pages and add a little PHP at the top to check some things before outputting the actual html. It would look kind of like this:
<?php
require 'checksomestuff.php';
// User's html below
?>
<html>
<!-- user's html -->
</html>
Is there a way to stop PHP from parsing anything after my require? I need the html to be outputted, but, since the user can add anything they want to the html, I don't want any user-added PHP to be executed. Obviously that would be a security issue. So, I want the user's html to be outputted, but not parse any PHP. I would rather not have to put the user's html into another file.
One sensible way would be to offload the user created content to another file and then you should load this file (in your main php file) and output it as is - without parsing it as PHP.
There are many other ways to do this but if creating another file does the job for you then thats probably the best way forward.
UPDATE: Really must read last line of question!
You could encode the html into a variable using base64 encoding which you then just print out the decoded string.
If you don't store the file data in a php file, say n a txt or html file, the php won't be evaluated.
Alternatively you could read the file via file_get_contents() or by some other means which doesn't involve evaluating php.
Though I'm still tempted to ask why you want to do this (particularly this way), it sounds to me like one of the only things that can help you is the special __halt_compiler() function...
That should prevent it from executing the rest of the page, and may or may not output the rest of it. If not, well, read the first (and currently only) example in the PHP's manual for that function (linked above) for how to do it manually.
The only trouble I see with this method is that you'd probably have to have that code in every file you want to do this for, after your require.
my site has now become sufficiently large for me to think it's necessary to convert the pages to php pages to help me update it in the future. The problem is: my site has a number of links to it on various websites across the web. Eg these links point to www.example.com/page1.html but the page is now going to be renamed www.example.com/page1.php
How would people get around this problem? Simply redirect the html page to the php page? Are there any alternatives? Does this have any implications for SEO?
Thanks
URL Rewrite: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
More directly to the point: http://roshanbh.com.np/2008/02/hide-php-url-rewriting-htaccess.html
The least intrusive method is to simply have your webserver treat .html files as PHP files. That way your links can stay intact, and progressively replacing static .html pages with actual php-enabled pages can be done in an essentially transparent method to users.
Remember, there is no such thing as a "PHP script". There are only files that contain <?php ... ?> code blocks, which will get interpreted/executed when the containing file is passed through PHP.
Unless some of your html pages contain SAMPLES of php code that could be misinterpreted as actual code, then there shouldn't be any issues with making run through PHP.
As a minor side benefit, it wouldn't be immediately obvious that your site is running on PHP, as all the urls would say ".html". But then, that's security by obscurity and shouldn't be counted on to be anything in the way of real security.
You can do a 301 redirect (this works fine for SEO), or just rewrite the URLs so page1.html points to page1.php internally.
Both solutions can be done with the .htaccess file (assuming you are using apache as your webserver)
Maybe consider using a tool such as Dreamweaver to manage your website. That way you can easily rename pages and update the links with a few clicks.
:)
As answered by Marc B,
there is no such thing as a "PHP script". There are only files that contain code blocks, which will get interpreted/executed when the containing file is passed through PHP.
i would say that it's true and if you want to absolutely turn your html files into php files simply put <?php before your <DOCTYPE html!> line and then ?> after your </html> line save it and rename it as example.php if it is example.html
if you are windows8 or higher user then click on 'View' in file explorer and then check 'File name Extension'. Now you'll be able to see the extension example.html and many other files extensions like .jpg, .mp3 e.t.c...., This helps you to easily rename exactly like example.php but not example.php.html as .html will not be visible if you are not checked File name Extension.
I would suggest that you use CodeIgniter (kickass php framework).
You can maintain the existing site structure also, by making use of CodeIgniter's URL suffix option .
I have a site complete with CMS etc all working under one domain name. It turns out for legal reasons one page on this site has to sit on a different domain name. The page is hooked into the same CMS as the rest of the site (built using codeigniter). I don't want to have to do another installation just for this page.
Is there any simple way to display just this page under a different domain name without taking it out of the current application?
Thanks a lot
You should look at either (in order):
an include()with correct php.ini configuration
a file_get_content() and printing the variable into your page
an <iframe src="yoururl"> wich would be the easy peasy but unsafe way
using the on-purprose curllibrary
using fopen() wich theorically allows distant files to be opened, but based on my experience, it's not that reliable
Look at this site, it seems rather exhaustive regarding your problem.
Try including the file
<?php include 'http://www.domain.com/url/to/file/page.html' ?>
I think what you need here is a symlink, which is something I don't know too much about. My understanding is that the path displayed to the user does not in fact have to have anything to do with where the file is actually stored, meaning you can set this up to have a completely different URL while keeping it as part of your original application.
A simpler thing is doing a redirect...it's one line of code in your .htaccess file and you're good to go.
include is a possible solution depending on the format of the remote page (ie, this won't work very well if the remote page has a full DOM structure, and you're trying to include the remote page within the DOM structure of your CMS page), however more information about that remote page would be needed to help determine if include() alone would be enough.
Regardless, if include() does, work, you must make sure allow_url_include in php.ini is enabled, as by default script execution will terminate when encoutering a remote URL include statement.
I have a website which consists of a bunch of static HTML pages. Obviously there's a lot of duplication among these (header, menu, etc). The hosting company I plan to use supports PHP, which I know nothing about. Presumably PHP provides some sort of #include mechanism, but what changes to I need to make to my HTML pages to use it?
For example, suppose I have a page like this
index.html
<html>
<head></head>
<body>
<h1>My Common Header</h1>
</body>
</html>
Obviously I need to move the common part into it's own file:
header.html
<h1>My Common Header</h1>
Given the example above (and assuming all files are in the same directory):
What do I add within the body tag to get header.html included?
Do I need to rename index.html or add some special tags to indicate that it's a .php file?
Do I need to make any changes to header.html?
Update: I want to emphasise that my objective here is simply to find the lowest-friction means of reducing duplication among static HTML files. I'm a bit reluctant to go down the server side includes route because I don't yet know what type of server (IIS/Apache) I'll be hosting the files on, and whether includes will be turned on or off. I was drawn towards PHP only because it is about the only thing I can presume will be available that will be able to do the job. Thanks for the responses.
Thanks,
Donal
You are looking for include (or one of its derivative such as include_once, require, require_once):
header.php
<h1>My Common Header</h1>
index.php
<html>
<head></head>
<body>
<?php include('header.php'); ?>
</body>
</html>
And so on, for your footer for example.
You don't need to use PHP to get this functionality, and it's generally a bad idea to do so due to potential security concerns. Essentially, you're swatting a gnat with a nuclear bomb. If you're not using a dynamic language, then you're looking for server side includes.
In IIS, for instance:
<!--#include virtual="file.inc"-->
Be aware that you often have to configure the server to utilize them, as this feature is often turned off by default. Both IIS and Apache support server side includes, but they use different configurations.
You can find more information here:
Server Side Includes
EDIT: I don't mean that it's a bad idea to use PHP, just using PHP solely for including other files. It creates a larger attack surface by bringing PHP into the mix when it's not needed, thus the potential for security issues when the functionality of PHP is not required.
EDIT2: I think it's a bad idea to assume you won't be a target because of your size, and thus you can ignore security. Most sites are compromised by automated worms and turned into malware hosts, spam zombies, or pirated software/media servers. Apart from the fact that you might end up being involved with infecting others, your site can become blacklisted and it can cost you real money in bandwidth overage charges. We're talking hundreds or thousands of dollars.
Just because you're a small site doesn't make you any less of a target. Just being on the internet makes you a target.
Forget doing it on the server altogether.
If all you really want to do is maintain some static pages -- and don't anticipate ever having to really use PHP -- I'd just do it with Dreamweaver, which will allow you create and manage templates and variable content on your end.
No includes needed. No templating engine needed. (These would be overkill for what you are trying to accomplish.)
You should first change the file extensions of index and header to be .php, then you can do:
<html>
<head></head>
<body>
<? include 'header.php'; ?>
</body>
</html>
And your header.php file just has
<h1>My Common Header</h1>
While you can just use the "include", "require", or "require_once" directives to include things in one page, you might have better luck with a template engine like Smarty
While using an include file for the header is a solution I went a different route when I faced the problem several years back: I wanted all pages to use the same layout (which I assume is rather common ;-). Thus, as I only wanted to change the content of the page I made the page content the file that gets included and have a master template file that includes header and footer. For setting the page to be included I resorted to creating quite small php scripts that only set a variable that holds the page to get included. In some cases the page can also get named by a GET parameter. Of course this requires proper validation of that parameter. In the long run I don't need to worry about the HTML itself anymore -- all I do is write small snippets (which should be complete for themselves of course) that get included.
A possibly even better solution would be to use an existing template framework. Due to the contraints I had back then I wasn't able to do so, but I would do it when facing the same issue again.
Back in the day, I used SSIs (the "<!--#include virtual="file.inc"-->" method described above by Mystere Man) quite a bit for static HTML pages and I would definitely recommend using that.
However if you want to eliminate any uncertainty about whether support for that will be enabled on the server, you could develop your separate files locally and merge them into the resulting files before uploading to your server. Dreamweaver, for example, supports doing this in a seamless fashion.
Or you could do it yourself with a rather simple script in your language of choice by doing simple string replacement on markers in the files, replacing {{{include-header}}} with the contents of a "header.html" file and so on.
Edit
Oops! Somehow I didn't see Clayton's post with the same note about Dreamweaver.
OK this is a semi-programming related question only.
PHP does have include(), which is really easy to use, but it doesn't contribute to future maintainability. I wouldn't recommend it, especially for big sites.
I'm a pro-frameworks. I've used CodeIgniter, CakePHP and even Smarty template engine. If you are serious about PHP, do consider CakePHP. There's this "layouts" concept where you frame your header, footer, css, javascript outside of the main content; e.g. for the "about us" page, your content would be something like:
This is an about us page that tells you a whole bunch of stuff about us...
CakePHP takes this this content, and wraps your layout around it:
header
css
javascript
This is an about us page that tells you a whole bunch of stuff about us...
footer