Disable recursion (public DNS) with Bind

April 27th, 2010

I’ve just set up two new nameservers, and after only a few weeks, I’ve noticed that random IP addresses are hitting my nameservers requesting DNS records for 3rd party domains. What’s worse is that my nameservers are responding with the results.

To disable this in bind, add the following to the ‘options’ stanza within named.conf:

allow-recursion {“none”;};
recursion no;

Access denied for user ‘debian-sys-maint’@’localhost’

April 20th, 2010

Exporting all databases from a MySQL installation on Debian using –all-databases, and then importing them back into a new installation will overwrite your privileges table. Whilst this may be what you want, each MySQL installation on debian generates a unique /etc/mysql/debian.cnf which contains logon details for the ‘debian-sys-maint’ system account. This account is used by the custom Debian scripts to deal with things such as checking for crashed tables.

After your export and import, you will likely end up with an error on your new installation:

‘Access denied for user ‘debian-sys-maint’@’localhost’

The way to fix this, is to log in to MySQL as root, and issue:

GRANT ALL PRIVILEGES ON *.* TO ‘debian-sys-maint’@’localhost’ IDENTIFIED BY ‘xxxxx’ WITH GRANT OPTION

Where ‘xxxxx’ is the password found in your debian.cnf file.

Then issue: FLUSH PRIVILEGES; and restart MySQL

Timing operations in PHP with microtime

April 12th, 2010

Within PHP, we have a function called microtime(bool $float).

When microtime() is called without any arguments it will return a string in the form of “microseconds seconds” i.e. “0.70801200 1271088014” which is the number of seconds and microseconds since the Unix Epoch (0:00:00 January 1, 1970 GMT).

When microtime is called with a ‘1’, i.e. <? echo microtime(1); ?> it will return the number of microseconds alone which is going to be more useful for what we want to achieve, i.e. “1271088173.97”

So using this, we can now write a script that we want to time, for example:

<?php
echo “Script Startingn”;
$start_time = microtime(1);
for ($i = 0; $i < 10000000; $i++)
{
//perform some calculation
$n = $i%247;
}
$end_time = microtime(1);
echo “Our script took: ” . ($end_time – $start_time) . ” to runn”;
?>

The output of this script is:

adam@vm1-webserver01:/tmp$ php ./t.php
Script Starting
Our script took: 1.45122814178 to run

Now you can add a $start_time = microtime(1); to the top of any script that you have, and a $end_time = microtime(1); echo $end_time-$start_time; to the end of any script that you have to time it, or alternatively just time certain portions to see where you can optimize things.

PHP and curl

April 7th, 2010

Using curl with PHP is incredibly easy. Firstly you’ll need to make sure that you have the PHP curl library installed on your system. On Debian, this is as easy as apt-get install php5-curl

Now you can try the following:

<?php
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, “http://www.google.com/”);
curl_setopt($handle, CURLOPT_HEADER, 0);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($handle);
curl_close($handle);

print_r($output);
?>

You can also check http://uk.php.net/manual/en/function.curl-setopt.php to take a look at the other options that curl_setopt can take.

curl can also post data to the remote server via POST or GET and also has the ability to save and retransmit cookies.

Communicating with the Twitter API via curl

April 6th, 2010

Twitter provides an extensive API that allows developers to write interactive applications. Utilizing this API is deceptively simple, and here is an example with curl:

curl -u username:password http://api.twitter.com/1/statuses/friends_timeline.xml

Which will get the statuses of all your friends. You can of course use PHP’s curl library just as easily as the command line, and my next post will focus on using php5-curl