Development

Find the size of all databases on a server

A quick way to create a view to show file size statistics across databases on a server.

DROP VIEW IF EXISTS alldb;
CREATE VIEW AllDatabases AS
SELECT
s.schema_name AS 'Schema',
SUM (t.data_length) AS Data,
SUM ( t.index_length ) AS Indexes,
SUM (t.data_length) + SUM (t.index_length) AS 'Mb Used',
IF (SUM(t.data_free)=0,'',SUM (t.data_free)) As 'Mb Free',
IF (SUM(t.data_free)=0,'', 100 * (SUM (t.data_length) + SUM (t.index_length)) / ((SUM (t.data_length)+SUM (t.index_length) + SUM (IFNULL(t.data_free,0))) ) ) AS 'Pct Used', COUNT (table_name) AS Tables
FROM information_schema.schemata s
LEFT JOIN information_schema.tables t ON s.schema_name = t.table_schema
GROUP BY s.schema_name
WITH ROLLUP

Find the size of all databases on a server Read More »

Making a CSS div behave like a link

The requirements were that Javascript shouldn’t be necessary (bad SEO and accessibility) and that the HTML had to validate. For some examples have a look at Facebook advertisements. Here is how it was done:

  • Create the div as normal using HTML and CSS. In the div you will need to place the link you are wanting to be accessible.
  • Inside that link you will need an empty span tag.
  • Make sure that the div is styled in CSS with position:relative
  • Apply the following style to the empty span tag:

{ position:absolute;
width:100%;
height:100%;
top:0;
left: 0;
z-index: 1 }

Any other links you may have inside the div will require position:relative and a suitable z-index (greater than 1)

Making a CSS div behave like a link Read More »

Where is my Firebug icon?


The icon of the wonderful plugin Firebug recently moved from its familiar location at the bottom right corner of Firefox. This was due to a UI change in the status bar of Firebox from version 4 onwards. The Firebug blog does a good explanation of why this was necessary.

What I did to fix however was to type about:config in my Firefox address bar.
I accepted the “I’ll be careful, promise!” warning.
To narrow down the (many) configuration options I applied a filter “firebug.show”
Finally I changed “extensions.firebug.showStatusIcon” to true.

29/01/2012 From the number of pageviews this post receives I see this is common problem. I hope it helps people solve the issue.

Where is my Firebug icon? Read More »

IE blocking iFrame Cookies

Facebook recently moving their applications inside an iFrame has turned web development on its head once again. After being deprecated in XHTML and barely hanging on in HTML iFrames looked like they had all but become extinct. Not so!

So, although other browsers do not block and intercept your cookie within an iFrame (and hence Facebook application) Internet Explorer will do. The problem lies with the W3C standard called Platform for Privacy Preferences (P3P) and you can read about it here if you wish.

In PHP all you will need to ensure Internet Explorer accepts third party cookies is to send the below header:

header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');

Note: by using such a P3P statement you are making a legal statement about how your application handles cookies and if you are later found in violation of the statement you make, you may find yourself in criminal or civil hot water.

IE blocking iFrame Cookies Read More »

PHP Script Execution Timer Class

< Often for testing the efficiency of some PHP code we use script execution calculations such as the microtime function. What is infinitely more useful however is the ability to time the execution of all, some, or multiple sections of code or series of lines.
Unfortunately I cant remember the origins of this timer class – but this code allows you to stop, start, pause and resume timing of specific sections of your php code. Thank you, whoever you may be.

Download the class here

Available methods:

start() – start/resume the timer
stop() – stop/pause the timer
reset() – reset the timer
get([$format]) Which can defaults to Timer::SECONDS but can also be Timer::MILLISECONDS or Timer::MICROSECONDS

Usage example:

$timer1 = new Timer();
$timer2 = new Timer();

$timer1->start();
// do some code

// calculate the time it takes to run a function
$timer2->start();
functionX();
$timer2->stop();

$timer1->stop();

print $timer1->get();
print $timer2->get();

PHP Script Execution Timer Class Read More »

Image Placeholders

placekitten

A quick and simple service for obtaining pictures of kittens for use as placeholders in your designs or code.

Simply place the required image size after their url (for example http://placekitten/200/300) and you will be rewarded with a cute placeholder image. Great for quick real code mockups…

Check it out here

Image Placeholders Read More »

cURL in PHP to access protected sites

So I was trying to use the FaceBook PHP-SDK and ran into an issue. As the cURL was pointing to an HTTPS source I was getting this error:
Failed: Error Number: 60. Reason: SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

The problem was that cURL hasnt been configured to trust the servers HTTPS certificate, as by default cURL is not setup to trust any of the Certificate Authorities (CAs)
Browsers dont have this issue as the browser developers were kind enough to include a list of default CAs, however this doesnt help us out at all…

A quick fix is to simply configure cURL to accept any server certificate. Obviously from a security point of view this isnt great, but if you are not passing sensitive information back and forth you should be ok.

Before calling curl_exec() add the following code:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
This causes cURL to blindly accept an server certificate without doing any verification with the CA that issued it.

The Proper Fix
The proper fix is slightly more involved so I plan to cover it at a later stage. If you cant wait that long research the curlopt_cainfo parameter, and obtaining (and saving) a CA certificate to enable cURL to trust it.

cURL in PHP to access protected sites Read More »

Reading GET variables with JavaScript

Recently I needed a JavaScript implementation of PHP’s $_GET functionality. There are a lot of quite bulky solutions out there, but my favourite was by Josh Fraser here

<script type="text/javascript">
function $_GET(q,s) {
s = (s) ? s : window.location.search;
var re = new RegExp('&'+q+'=([^&]*)','i');
return (s=s.replace(/^\?/,'&').match(re)) ? s=s[1] : s='';
}
</script>

Short, simple, effective. Enjoy.

Reading GET variables with JavaScript Read More »