home | contact us
» Archive by category "php"

category: php


If you fancy some php reading and watching suggest you check out this PHP newsletter: http://phpweekly.info/archive/


 

If you are ever working with API integrations, either in or out bound, then it might be useful to set up a simple dumb logging system to assist you with developing and debugging things.

Here is a really simple snippet to help you along with that.

It literally takes exactly what it has received and logs it with no messing about. Brilliant!


<?php
$log['raw_input']=file_get_contents('php://input');
$log['_POST']=$_POST;
$log['_GET']=$_GET;

file_put_contents('inboundXML.log', var_export($log, true));


 

The function below is one that we have used a few times to allow certain “admin only” assets to be accessed or visible on the front end of a website.

The theory is that the htaccess file in the admin folder will be kept up to date and can therefore be used as the authoritative list of IP addresses that are allowed to access admin assets.

This function is simple enough and should be useful anywhere that you use htaccess to protect a certain folder and would like to implement the same white list rules in other places without having to maintain a duplicate list of authorised IP addresses.

The function also uses static variables – a lesser known bit of PHP functionality which can be a nice easy performance optimisation when working with procedural PHP code. If a function will be called many times and will always return the same result in a single request, you can actually cache that result to a static variable and serve that on any subsequent requests.

function isAllowedIp() {
    static $pass = null;
    if ($pass !== null) {
        return $pass;
    }
    $pass=false;
    $customer_ip = $_SERVER['REMOTE_ADDR'];    
    $htaccess = file_get_contents('admin/.htaccess');
    preg_match_all('%allow from ([0-9.]+)%', $htaccess, $matches);
    foreach ($matches[1] as $ip) {
        if ($ip == $customer_ip) {
            $pass = true; //this is stored statically for perfomance reasons
            return true;
        }
    }
    return false;
}

 

As you might be aware, the UK PHP Conference was held in London last weekend. Having been the lucky winner of a ticket from PHP NW I went along.

The Venue

The conference was held in The Brewery in London city centre, a well suited conference centre with a variety of different conference rooms. An old Whitbread brewery, it was modern and practical while still maintaining enough of its history to keep its character. Overall the venue was great and it was good to have the variety of different rooms for all of the different events

The talks

The talks on offer were on a wide variety of subjects, varying from the very specific libraries to things of use to any PHP developer. Each of the talks was led by a volunteer giving up their time to speak about a subject for which they held a passion, it came across very well and as ever is good to hear from active members of the community.

Talks I found to be of particular interest were

  • The Future of the PHP Development Environment A practical introduction to Vagrant, demonstrating its purpose in creating a reliable, predictable server environment. Covered was the installation, configuration and use of the software
  • OpenStreetMap for the Web An enthusiastic demonstration of the power of having openly accessible data of the world’s maps and the APIs which enable a plethora of different applications
  • A Hands-On Introduction to Writing Unit Tests Using PHPUnit A very practical (with live coding and demonstration of the Unit Tests, a risky move that paid off) intro to unit tests in PHP. Aimed at the very beginner, it started slowly with very simple tests but showed the power of them in much bigger projects

In summary

I thoroughly enjoyed the day, and would recommend anyone wanting to attend in future. The talks will be posted on the PHP UK website in the coming weeks so you should be able to get a sample of the events there


 

We are big fans of Google Apps and have built a large portion of our business processes around the system. For this reason we have absolutely no problem recommending the service to our clients and have assisted numerous clients with a migration to the system.

The first benefit you will get is your emails migrated over and a much better email service. You have the option of using normal IMAP access so you can continue to use your preferred desktop application however we suspect that you will start to use the web interface as it is really quite powerful with some unique features that you can not access on a normal desktop client.

Along with this you have great support of mobile devices, especially if you opt for Android with powerful and free native apps meaning that you can have all the access to your emails that could possibly wish for.

Along with the really excellent email functionality you also have powerful online documents giving you the basic office application functionality along with really powerful collaboration features allowing multiple people to be working on the same document simultaneously from geographically separate locations. This can be great when putting together proposals, specifications etc.

Now called Google Drive, the documents system has grown from managing purely office documents into a fully featured online file store that can be used to store and share all kinds of files across your organisation and with third parties.

Finally the calendar functionality is great. If you have never used Google Calendar then I suspect the first things you will notice is the abilty to easily drag and drop appointments around when reorganising your schedule. The ability to quickly create appointments using natural language such as “call steve at 11am” is really nice. A full barrage of reminders can be set up if you wish including on screen, SMS and emails meaning you should never miss an appointment again.

All in all we think Google Apps is great and would love to help you move your organisation across to the system. If you would like to discuss this in more detail please do get in touch today.

Edmonds Commerce are a Google Apps Reseller.


 

If you have a form designed to handle file uploads which is failing due to file size then you might like this:

$arrayMaxes = array(
    'upload_max_filesize' => intval(ini_get('upload_max_filesize')),
    'post_max_size' => intval(ini_get('post_max_size')),
    'memory_limit' => intval(ini_get('memory_limit'))
);
$maxUploadSize = min($arrayMaxes);
foreach ($arrayMaxes as $key => $value) {
    if ($value == min($arrayMaxes)) {
        $minimumOfThree = $key;
        break;
    }
}

echo "The maximum file size you can upload is $maxUploadSize, this is due to the php.ini setting $mininimumOfThree";

This will calculate the smallest value that will be allowed based upon php.ini settings.

You can then incorporate this into your form logic and display so that users have a clear understanding of how large a file they can upload.


 

When you’re uploading multiple files (HTML5 feature – remember IE as yet STILL does not support this), it may not be the fact you’re uploading multiple files that’s actually causing the problem.

The problem often is actually caused by the various php memory limits in play, one way to avoid getting caught by this is using the following snippet in the form’s php to remind you which restrictions are in play (note the min_by_key() function is from the php.net comments) :-

<?php
function min_by_key($arr, $key){
    $min = array();
    foreach ($arr as $val) {
        if (!isset($val[$key]) and is_array($val)) {
            $min2 = min_by_key($val, $key);
            $min[$min2] = 1;
        } elseif (!isset($val[$key]) and !is_array($val)) {
            return false;
        } elseif (isset($val[$key])) {
            $min[$val[$key]] = 1;
        }
    }
    return min( array_keys($min) );
}

$arrayMaxes = array(
    'upload_max_filesize'=>intval(ini_get('upload_max_filesize')),
    'post_max_size'=>intval(ini_get('post_max_size')),
    'memory_limit'=>intval(ini_get('memory_limit'))
);
$maxUploadSize = min($arrayMaxes);
foreach ($arrayMaxes as $key=>$value) {
    if ($value == min($arrayMaxes)) {
        $minimumOfThree = $key;
    }
}?>
<p>Max filesize <?php echo $maxUploadSize; ?>M Maximum filesize due to server setting <?php echo $minimumOfThree; ?> in php.ini</p>

 

PHPMD is useful as it helps keep coding problems to a minimum. There are some esoteric circumstances where it will flag up issues that while they are issues there is no good way to avoid them.

Consider the following code block which is overriding a method from a parent class:

<?php
class ExportSpecial extends Export
{
    public function getOrderItemValues($item, $order, $itemInc=1)
    {
        return array();
    }
}

With this PHPMD will flag the three unused parameters as an issue.
This is correct because they are unused however it’s also incorrect because in this instance the overriding method must accept these parameters to prevent a strict error.
This is one of the limited set of instances where suppressing the issues that PHPMD finds would be correct.
To do this you use the @SuppressWarnings option on the comments block above the method definition. There are many options that are suppressible within PHPMD, the list of errors to suppressible rules can be found here: http://phpmd.org/rules/index.html

So in the previous example the following change could be made to stop PHPMD flagging the erroneous issue.

<?php
class ExportSpecial extends Export
{
    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function getOrderItemValues($item, $order, $itemInc=1)
    {
        return array();
    }
}

 

I find it frustrating when importing data into Magento that it’ll successfully complain about a row with errors, but won’t tell you which row has the problem!

The code which generates the HTML for the error messages is at app/code/core/Mage/Adminhtml/Block/System/Convert/Profile/Run.php. Make a copy of this file at app/code/local/Mage/Adminhtml/Block/System/Convert/Profile/Run.php

Locate this part:

                $this->setBatchConfig(
                        ...
                        'template' => '<li style="#{style}" id="#{id}">'
                                    . '<img id="#{id}_img" src="#{image}" class="v-middle" style="margin-right:5px"/>'
                                    . '<span id="#{id}_status" class="text">#{text}</span>'
                                    . '</li>',
                         ...

And change:

'<span id="#{id}_status" class="text">#{text}</span>'

to:

'<span id="#{id}_status" class="text">Row #{id} - #{text}</span>'

 

If you want to be able to run multiple queries in a single function call, for example doing the classic drop table blah; create table blah; then you might like this function.

The use case is for things like database migration systems which you might copy and paste chunks of SQL including multiple queries from things like phpMyAdmin

/**
 * Run multiple queries passed in as a single string
 * This is optimised for copying and pasting from phpMyAdmin
 * 
 * Handy for things like database migration systems
 * 
 * @param string $sql  multiple queries terminated with ; and a new line
 */
function multiQuery($sql)
{
    $sqls = preg_split('%;$%m', trim($sql));
    foreach ($sqls as $q) {
        if (empty($q)) {
            continue;
        }
        mysql_query($q); //suggest you replace this with your custom query function or if not throw in some extra error checking at least
    }
}

 
rss icon