A safe common code base with cURL - "live design pattern".

With a focus on cURL
KBleivik
Site Admin
Posts: 178
Joined: Tue Sep 29, 2009 6:25 pm
Location: Moss Norway
Contact:

A safe common code base with cURL - "live design pattern".

Postby KBleivik » Thu Feb 09, 2012 9:12 am

I have seen enough of third party code, changing, not functioning and broken. Even ad code from my affiliate networks are suddenly blocked without notice. The worst example is here:

The true face of a global monopoly finally shows up.

I am still removing AdSense code from my sites.

So I have decided to make my markup, styling and code as independent as possible of any third party company or service. I want to change styling, markup and common code in only one html, JavaScript, html and css file and one php (I use php) script. One place is of course more correct, sinc you can have multiple files, for example common header and footer files, common css styling and common JavaScript libraries like jQuery, jQueryMobile, modernizr, http://xuijs.com/ etc. JavaScript files can be included in header and / or footer files. If a new versions of a JavaScript library, don't change the functionality you use, a new version can then be put in the common file. But sharing a common code base is not without risk. One way to do it is (if your hoster allow you to do it) to put the following file on the server:

php.ini

Code: Select all

allow_url_fopen = On
allow_url_include = On


That is not the recommended way to do. Instead you shall use the php cURL library.

Code: Select all

<?php
    // create curl resource and set URL
    $ch = curl_init("http://www.mysite.com/test/edu1.php");
    //return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // $output contains the output string
    $output = curl_exec($ch);
    // close curl resource to free up system resources
    curl_close($ch);     
    echo $output;
?>


or more advanced make a local php file from a main domain to an add on domain like this:

Code: Select all

<?php
    // create cURL resource and set URL
    $ch = curl_init("http://www.mysite.com/test/edu1.php");
    // Open file for writing
    $fp = fopen("example.php", "w");
    // Set cURL options
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    // close curl resource to free up system resources
    curl_close($ch);
    // close the file example.php that is now in the same directory on the web server.
    fclose($fp);
?>


That is the safe way

http://blog.unitedheroes.net/curl/

PHP, CURL, and YOU!

Getting data without risking death


to use remote files on your (add on) domain(s). In that way, I can start my local script by downloading the php files from a common code base and use them fairly safely on other domains.

Why is this an advantage? Why should you not include the absolute URL? Because that will for security reasons be deprecated in future versions of php as far as I know. It may function if you use php 4.+. And by default fopen is turned off by serious hosters. You can create a custom php.ini file and add "allow_url_fopen = 1" in it to turn this on. However, using cURL is much more secure.

Say that you have 100 sites with the same templates etc. You can then change them in the common code base and it will immediately show up on all your sites where you import the code to an add on domain or even from another server under your control with cURL. That is a secure way to handle multi site code in a common code base.

Her is a simple generalisation of the string example above to a function without error checking.

Code: Select all

function cURLtoString($url)
{
    // create curl resource and set URL
    $ch = curl_init($url);
    //return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // $output contains the output string
    $output = curl_exec($ch);
    // close curl resource to free up system resources
    curl_close($ch);     
    return $output;
}


The code can of course be used in classes. If you want to change your color from blue to green on all your sites and pages, it may be done by changing a color code in a css style file.

If you combine it with object relational mapping http://www.oopschool.com/books/ProPHP6Example.pdf and a good databaseplattform everything on your network or multi site platform can live. The whole concept may be abstracted to a "Live design pattern". It lives in the sense that everything that is shared in the common code base for a muliti site or a network may be changed by a few lines in one or more files. You can change almost everything (aside from page / site specific content) from a common code base. If in addition, you use WebKit http://www.webkit.org/ enabled browsers and PhoneGap http://phonegap.com/ very much of your web development can take place from a common code base.

Return to “URL solutions”

Who is online

Users browsing this forum: No registered users and 1 guest