PHP – Basic Whois


Whois services are extremely useful to get basic information about a domain name: owner, creation date, registrar, etc. Using PHP and the whois unix command, it is extremely easy to create a basic whois PHP function. Please note that the whois unix command must be installed on your server for this code to work.

$domains = array('home.pl', 'w3c.org');

function creation_date($domain) {
    $lines = explode("\n", `whois $domain`);
    foreach($lines as $line) {
        if(strpos(strtolower($line), 'created') !== false) {
            return $line;
        }
    }

    return false;
}

foreach($domains as $d) {
    echo creation_date($d) . "\n";
}

Javascript – Include your scripts at the bottom of your HTML files


Not so long ago, it was generally considered good practice to insert your Javascript files within the and tags of your html document.

But browsers read html files from top to bottom, and load external files dynamically. Which mean that inserting scripts within the and tags will make your Javascript load before some of the page content.

In order to always load scripts after the content, Javascript files should always been included at the bottom of your html files, as shown below:

    <script src="myscript.js?" type="text/javascript"></script>
  </body>
</html>

WordPress – Display Total Number of SQL Queries on Your Blog


Queries are evil. They slow down your site as you begin to accumulate more, which can negatively effect the user experience.
In fact, Google agrees too.

The first step to lowering queries is to know how many you actually have. Simply paste the following line of code somewhere on your site. I prefer going after the footer.php file.

<?php if (is_user_logged_in()) { ?>
    <?php echo get_num_queries(); ?> queries in <?php timer_stop(1); ?> seconds.
<?php } ?>

If you want this to be publicly shown, simply remove lines 1 and 3.

PHP -printing the backtrace in a complex application


The PHP debug_backtrace [man] function is very useful to understand where a function/method is called.

It prints the back trace of the code.

Example: In the framework I’m currently using there are ORM classes to access the DB. So, it takes long to understand where the query is launched when needed. Solution: save in a global variable (or in a field of the class) the list of the queries launched and the code that has launched each query.

Code:

class DB {
# …
public function query($query) {
if (DEBUG_MODE) { ##
$_dbt = debug_backtrace();
$_fromFile = isset( $_dbt[0]['file']) ? str_replace(_ROOT, “”,$_dbt[0]['file']) : “”;
$_fromLine = isset( $_dbt[0]['line']) ? $_dbt[0]['line'] : “”;
$_launchedFrom = “launched from $_fromFile:$_fromLine“;
$this->logQueries[] = “[$query][$_launchedFrom]“;
# ..query.
}
}
# …
}

Example of result:

Array
(
[0] => [set names utf8][launched from C:\wamp\www\dev\index.php:68]
[1] => [SELECT * FROM `Setting` WHERE `id` = 1][launched from \class\Setting.class.php:20]
[2] => [SELECT id FROM `User`WHERE id = 293968 LIMIT 1][launched from \class\User.class.php:598]
[3] => [SELECT * FROM `User` WHERE `id` = 293968][launched from \class\User.class.php:45]
[4] => [SELECT f.store_id FROM `Store_Monthly_Featured` f ORDER BY f.order ASC][launched from \class\Store_Monthly_Featured.class.php:16]
)

Useful function to print only line and number

function debug_backtrace_filelines() {

$ret = array();
$b = debug_backtrace();
foreach ($b as $elements) {
$ret[] = $elements['file'].‘:’.$elements['line'];
}
}

Happy New Year 2012!


Wishing you a fabulous 2010 with full of great achievements and experiences.
A meaningful chapter waiting to be written HAPPY NEW YEAR!