Narendra Dhami

My Site

Archive for August, 2008

300+ PHP Presentations Online

Posted by Narendra Dhami on August 28, 2008


Posted In: Best List, PHP, Webmaster by
Scott Spear

The PHP
Presentation System
hosts over three hundred presentations, covering
a slew of different topics including performance, testing, debugging,
XML, and security. The presentations are from speakers at a variety of
conferences. You will see (and hear if interested) presentations from
speakers like Derick Rethans, Tobias Schlitt, Rasmus Lerdorf and many
many more. These presentations have been at conferences all around the
world and contain a great deal of information. Most presentations are
between 25 and 40 slides. You will have to sift through some information
in the presentations because some build on each other.

The presentations are best viewed in
Firefox and are not available for use with IE. If you try to use IE you
will get a pretty funny error code; “IE is not supported – please use
Firefox, Safari, Konqueror or just about anything else” (view this presentation
in IE). There are presentations dated as far back as 2002 and as recent
as August 2008. Take a look around and see what’s there.

My Top 10 Presentations

  1. Input
    Filter
  2. Webservices
    with PHP
  3. XML
    and PHP
  4. Myth
    Busting
  5. PHP
    Best Practices
  6. Web
    2.0 and PHP 5
  7. XSS
  8. Help,
    I Found a Bug in my Code
  9. Hack
    Day
  10. Designing
    Good Databases

Posted in PHP | Leave a Comment »

Typical automated MySQL maintenance jobs

Posted by Narendra Dhami on August 26, 2008


The following maintenance jobs are typically run against a MySQL database:

  • Backup
  • Clean-up binary logs.
  • Optimize tables
  • Purge query cache
  • Rotate binary logs
  • Backup
    A backup is not a typical maintenance job. But it behaves more or less like one. The backup should be done regularly depending on the restore/PITR (Point in Time Recovery) requirements.

    Make sure, that in the backup all the necessary files (data files, transaction log files, configuration files and binary log files) are included. To prove that the backup process is working properly a regular restore should be performed. This can ideally be combined with the set-up of new database instances for developers or testing.
    Clean-up the binary logs

    The binary logs can be cleaned-up in two ways:

    a) Passive by MySQL itself:
    # my.cnf
    expire_logs_days = 7

    b) Active by the customers environment:
    mysql> PURGE MASTER LOGS TO ‘binarylog.000999’;
    mysql> PURGE MASTER LOGS BEFORE ‘2008-07-29 22:46:26’;

    Make sure NO binary logs are purged which are still needed by a slave. In this situation the slave is lost and has to be set-up from scratch.
    Make also sure binary logs are not removed by a file system operation (rm bin-log.*). Otherwise the database gets confused.

    Optimize table
    After large UPDATE or INSERT/DELETE operations or long time tables are blown up and contain a lot of unused space. This unused space can be partially reclaimed by optimizing the table again.
    mysql> OPTIMIZE TABLE ;

    This operation internally copies the whole table and therefore can take a long time!

    Purge query cache
    When there are SELECT queries with different sizes of result sets the query cache gets de-fragmented. This is shown by a lot of free space in the query cache but also a lot of not cached queries. Here it makes sense to purge the query cache from time to time.
    mysql> FLUSH QUERY CACHE;

    Binlog rotate
    Binary logs can only be rotated by size. Sometimes you want to have them rotated by time. You can do this as follows (for example with a cron job):
    mysql> FLUSH LOGS;

    Posted in MySQL | Leave a Comment »

    Tuning Apache and PHP for Speed on Unix

    Posted by Narendra Dhami on August 25, 2008


    Here is my compilation of tips on how to optimise Apache on Linux for PHP and CGI programs. These tips can also apply to Perl and Python. Links will open in a new window. More…

    Posted in PHP | Leave a Comment »

    Importing huge XML files using PHP5

    Posted by Narendra Dhami on August 25, 2008


    At work I had the task to implement the synchronization between an online
    shop and a commodity management system. Data exchange format was XML –
    one big XML file for all of the products (some thousands with dozens of
    attributes). Big question: How do I import the file in a way that is most
    convenient for me as a programmer – and without exceeding the machine’s
    RAM when loading a 1 GiB file?

    I personally prefer SimpleXML for everything XML related in PHP – even to
    generate XML; although it was never meant to do that primarily.
    The big problem is that SimpleXML uses DOM in the background which builds
    the whole XML tree in memory. That’s a no-go for large files.

    So what’s left? Yes, our old and rusty Sax parser. It’s not really
    convenient – you have to catch all this actions for open tags, close tag,
    data section etc. – but it reads the xml file iteratively. Parsing huge
    files is no problem if you use Sax. PHP5’s slightly enhanced Sax
    implementation/wrapper is XmlReader which I chose to make use
    of.

    On the other side – my program that synched the data with the database –
    I wanted to have something dead simple, like a foreach loop. Now
    the task was to combine XmlReader and
    SPL‘s Iterator

    interface.

    Sample XML

    
    
     
      Kate
      LGPL
      
       Editor
       Nice KDE text editor
      
      
       3.5.9
       4.0.5
      
     
    
     
      gedit
      LGPL
      
       Editor
       Standard gnome text editor
      
      
       2.22.3
       2.22.4-rc1
      
     
    
    

    Preferred PHP import code

    The following code is as easy and beautiful as reading an XML file can
    get:

    
    

    Iterator code

    Here is the iteration code – without comments – in case you (or /me) need
    to do the same thing again.

    strFile = $strFile;
        }
    
        public function current() {
            return $this->program;
        }
    
        public function key() {
            return $this->nKey;
        }
    
        public function next() {
            $this->program = null;
        }
    
        public function rewind() {
            $this->reader = new XMLReader();
            $this->reader->open($this->strFile);
            $this->program = null;
            $this->nKey    = null;
        }
    
        public function valid() {
            if ($this->program === null) {
                $this->loadNext();
            }
    
            return $this->program !== null;
        }
    
        /**
         * Loads the next program
         *
         * @return void
         */
        protected function loadNext()
        {
            $nCount = 0;
            $strElementName = null;
            $bCaptureValues = false;
            $arValues       = array();
            $arNesting      = array();
    
            while ($this->reader->read()) {
                switch ($this->reader->nodeType) {
                    case XMLReader::ELEMENT:
                        $strElementName = $this->reader->name;
                        if ($bCaptureValues) {
                            $arNesting[] = $strElementName;
                            $arValues[implode('-', $arNesting)] = null;
                        }
                        if ($strElementName == $this->strObjectTagname) {
                            $bCaptureValues = true;
                        }
                        break;
    
                    case XMLReader::TEXT:
                        if ($bCaptureValues) {
                            $arValues[implode('-', $arNesting)] = $this->reader->value;
                        }
                        break;
    
                    case XMLReader::END_ELEMENT:
                        if ($this->reader->name == $this->strObjectTagname) {
                            $this->program = $arValues;
                            ++$this->nKey;
                            break 2;
                        }
                        if ($bCaptureValues) {
                            array_pop($arNesting);
                        }
                        break;
                }
            }
        }//protected function loadNext()
    
    }
    ]]>

    There are some things missing, like: namespace and attribute support,
    handling of tags with the same name in different hierarchy levels,
    especially the main tag and generally tags that may show up several
    times. I didn’t need it, so do it yourself if it’s necessary.

    Posted in PHP | 2 Comments »

    100 Tips and Tools to Set Up Your Own Home LAMP Server

    Posted by Narendra Dhami on August 25, 2008


    LAMP (Linux, Apache, MySQL and PHP) servers are very popular for their ease of use and flexibility. They’re also easy to initiate, because the components are easy to aquire and there’s a lot of documentation available to help with getting started. Read on to find some of the best resources available for creating your own home LAMP server, from installation to maintenance. More…

    Posted in PHP | Leave a Comment »

    Useful Links for PHP

    Posted by Narendra Dhami on August 21, 2008


    sitecrafting.com
    gotapi.com
    swfupload.org
    pchart.net
    What Makes a Good DBA?
    Web 2.0 Design Generators For Developers
    Video PHP Blog
    JavaScript frameworks

    fix-mysql-database
    WebPage Test
    20-excellent-ajax-effects-you-should-know
    Build PHP Extension

    Posted in PHP | Leave a Comment »

    Practical Uses for the PHP Tokenizer

    Posted by Narendra Dhami on August 20, 2008


    In this article we take a look at the PHP tokenizer and its potential at analyzing and processing PHP source code. We will build several working examples, which you can start using and extending for your own purposes.

    Introduction

    When PHP has to process a request, the engine goes through several passes of parsing until the code is expressed as a set of instructions that the interpreter can execute. The first such step is “lexical scanning”, which splits the code into smaller strings called “tokens”. The token is the smallest meaningful unit of your source code, and it can represent a reserved word (for, while, class, if, etc.), operator (+, -, *, /, && etc.), value literals (integers, floats, strings etc.) and other special symbols.

    The same lexical scanner which PHP uses, is also available to userspace PHP developers via the function token_get_all(). It is very simple to use: you pass your PHP source code as text, and it returns an array of tokens, that we will further process in the examples of this article.

    More…

    Posted in PHP | Leave a Comment »

    PHP / MooTools 1.2 Accordion Helper

    Posted by Narendra Dhami on August 20, 2008


    The MooTools Accordion plugin seems to be the plugin that people seem to have the most problems with. It’s an awesome plugin, so I can see why so many people want to use it, but I think that may be part of the problem. I think people see and want it without sufficient knowledge of MooTools or javascript in general. That’s why I’ve created a PHP Accordion class to help make the develeopment of accordions easier and faster. More…

    Posted in PHP | Leave a Comment »

    PHP Performance Profiling With APD On Windows

    Posted by Narendra Dhami on August 20, 2008


    When writing code, its important to keep system performance in mind. No matter how useful your software is, if it places too much stress on the server, it won’t be used. How can you tell if your code is efficient? Well, experience helps, but it also pays to have good tools.

    Most of the code I write is fairly simple and should only place minimal load on the server, but I wanted to make sure that its as efficient as possible. I went looking for a PHP performance profiling tool to help me. More…

    Posted in PHP | Leave a Comment »

    CSS 3 Rounded Corners

    Posted by Narendra Dhami on August 20, 2008


    Border-radius property for FireFox and Safari 3 (only). And here are some nice examples how to use it.

    #box {
         background: #eee;
         border: 1px solid #ccc;
         padding: 15px;
         -moz-border-radius: 10px;
         -webkit-border-radius: 10px;
    }
    

    And of course you don’t have to make all corners rounded, it can be just top left etc, so the code goes like this (small not, for example you can’t make rounded corners on images and stuff like that)

    * -moz-border-radius-topleft and -webkit-border-top-left-radius
    * -moz-border-radius-topright and -webkit-border-top-right-radius
    * -moz-border-radius-bottomleft and -webkit-border-bottom-left-radius
    * -moz-border-radius-bottomright and -webkit-border-bottom-right-radius

    Posted in CSS | Leave a Comment »