Disappearing Folders in OSX Lion

After installing Mac OS X 10.7 Lion I noticed folders were missing. For me the main issue was that the Library folder in my user account seemed to have vanished. As this folder holds nearly all the data that applications save without asking such as mail, preferences, calendars and virtual disks it was important to have an easy way to manage it, especially as I wanted to exclude the Steam folder located there in Time Machine.

Simply launch Terminal from your applications/utilities folder and type

chflags nohidden ~/Library

Disappearing Folders in OSX Lion Read More »

PHP Modulo Operator For Alternating Rows


Alternating row colours within a grid of data can make for a more pleasant viewing experience and has become a popular styling method in recent times. I was a little incredulous at some code I saw recently to determine alternating rows so without further introduction, meet the Modulo operator which is a quick and easy way to get up and running:

for($i=0;$i<10;$i++){ if($i % 2){ // This is an even row }else{ // This is an odd row } }

I'm sure you will be able to apply this to your own css or styling within your loops.

PHP Modulo Operator For Alternating Rows Read More »

Use cURL to obtain remote filesize


Sometimes you might want to determine a remote file size prior to determining further action to take. This is a quick function that utilises cURL to retrieve the file size in bytes.
<?php
function remoteFilesize ($URL){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$info = curl_exec($ch);
curl_close($ch);
$info = explode('Content-Length:', $info);
$info = explode("Connection",$info[1]);
return $info[0];
}
?>

Usage is simple:

echo remoteFilesize("test.com/file.html");

Use cURL to obtain remote filesize Read More »

Get Facebook Likes and Shares via Graph API


Facebook Like and Share count can easily be obtained from the Facebook Graph API. This is an extremely quick way to look up the JSON response from Facebook so that you can further process it for your own requirements.
<?php
$URL ='https://graph.facebook.com/cocacola';
$JSON = file_get_contents($URL);
$Output = json_decode($JSON);
$Likes = 0;
if($Output->likes){
  $Likes = $Output->likes;
}

echo "Number of likes for CocaCola's Facebook Page = ".$Likes."</br>";

$URL2 ='https://graph.facebook.com/http://www.coca-cola.com';
$JSON2 = file_get_contents($URL2);
$Output2 = json_decode($JSON2);
$Shares = 0;
if($Output2->shares){
  $Shares = $Output2->shares;
}

echo "Number of shares for coca-cola.com = ".$Shares;
?>

UPDATE 29/01/2012: I’ve written a slightly different method using the PHP SDK in this post here

Get Facebook Likes and Shares via Graph API Read More »

Currency Conversion using Yahoo Finance API


Just a quick and dirty PHP function to retrieve information from the Yahoo Finance API to perform a currency conversion
function ConvertCurrency($From,$To,$Amount){
$URL = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s='. $From . $To .'=X';
$Handle = @fopen($URL, 'r');

if ($Handle) {
$Result = fgets($Handle, 4096);
fclose($Handle);
}
$allData = explode(',',$Result);
$Value = $allData[1];

return $Value * $Amount;
}

Usage:
$Amount = ConvertCurrency("NZD","USD",25);

Currency Conversion using Yahoo Finance API Read More »

Using mod_rewrite to remove variables from a URL


The Apache mod_rewrite module offers an unparalleled toolset for manipulating URLs, – however configuration can be a tricky beast at times. While most people are comfortable enough removing file extensions like .php we recently had a user who wanted to hide all the variables in the address string.

For example index.php?id=id would end up being index/id
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^about/(.*)$ index.php?id=$1 [QSA]

The magic here is the QSA or Query String Append. If you just wanted to remove the .php you would use
RewriteCond %{REQUEST_URI} !.*\.php$
RewriteRule ^/(.*)$ $1.php [QSA]

Using mod_rewrite to remove variables from a URL Read More »

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 »

Reducing WordPress comment spam using .htaccess

Wordpress Logo

I haven’t actually tried this, but the thought has occurred to me that a great way to reduce comment spam would be to use the following in your .htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{REQUEST_URI} .wp-comments-post\.php*
RewriteCond %{HTTP_REFERER} !.*blogdomainname.* [OR]
RewriteCond %{HTTP_USER_AGENT} ^$
RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]
</IfModule>

Obviously you’ll need to change blogdomainname to your own domain

Reducing WordPress comment spam using .htaccess Read More »