Search code examples
phpcdn

PHP Include for static remote HTML files on a CDN


I have an app that creates static HTML files. The files are intended to be hosted on a remote CDN, they'd be standard .html files.

I am wondering two things:

  1. If it's possible to do a PHP include on these files?
  2. Can you possibly have good performance doing it this way?

Solution

  • Can it be done?

    To answer the question directly, yes, you technically can include a remote file using the PHP include function. In order to do this, you simply need to set the allow_url_include directive to On in your php.ini.

    Depending on exactly what you intend to use this for, I would also encourage you to look at file_get_contents.

    To enable remote files for file_get_contents, you will need to set allow_url_fopen to On.

    Should it be done?

    To answer your second question directly, there are many factors that will determine whether you will get good performance, but all in all, it is unlikely to make a dramatic difference to performance.

    However, there are other considerations:

    • From a security perspective, it is ill-advised to enable either of these directives
    • By delivering the file from your server instead of the CDN you will be negating all of the benefits of the CDN (see below)
    • Is it really necessary?

    CDNs

    A frequent misunderstanding when it comes to CDNs is that all they do is serve your data from a closer location, thus it makes the request slightly faster... This is wrong!

    There are endless benefits to CDN's, but I have listed a few below (obviously depends on configuration and provider):

    • They strip out unnecessary headers
    • No cookies are sent as the CDN tends to be on a different, thus cookie-free, domain
    • They handle compression
    • They deliver your content from the nearest location
    • They handle caching
    • ... and a lot more

    By serving the file from your server, you will lose all of the above benefits, unless, of course, you set the server up to handle requests in the same way (this can take time).

    To conclude; personally, I would avoid including your .html files into PHP remotely and just serve them directly to the client from the CDN.

    To see how you can further optimise your site, and to see the many benefits that most CDNs offer, take a look at GTMetrix.