Thursday, 11 October 2012

Welcome to my Advanced PHP Programming Techniques Page



PHP is not limited to creating just HTML output. you can create image files using PHP. It just needs to install GD libraray to create variety of image formats like GIF, PNG, JPEG, WBMP, and XPM. PHP output image streams directly to a browser. You will need to compile PHP with the GD library of image functions for this to work. GD and PHP may also require other libraries, depending on which image formats you want to work with.
In this post you will learn how to install PHP GD Module on Ubuntu.
Type the following command to install this module:


# apt-get install php5-gd
OR

$ sudo apt-get install php5-gd
Now restart your apache server

# /etc/init.d/apache2 restart
Test whether php5-gd library installed or not?
Type the following command:

$ php5 -m | grep -i gd
The command will return “gd”  if  GD library  installed
OR
you can test GD libraray install status by running below php code:


    <?php
       echo phpinfo();
     ?>

XML for PHP developers

PHP5 offers the developer a lot more muscle to work with XML. New and modified extensions such as the DOM, SimpleXML, and XSL make working with XML less code intensive. In PHP5, the DOM is compliant with the W3C standard. Most importantly, the interoperability among these extensions is significant, providing additional functionality, like swapping formats to extend usability, W3C's XPath, and more, across the board. Here you will look at input and output options, and you will depend on the Yahoo Web Services REST protocol interface to provide a more sophisticated showcase for the functionality of the now familiar DOM and SimpleXML extensions and conclude with the XSL extension.

XML in PHP5

Extensible Markup Language (XML), described as both a markup language and a text-based data storage format, offers a text-based means to apply and describe a tree-based structure to information. Here you'll look at XML in the context of Web services, probably one of the most important factors driving the recent growth of XML outside the enterprise world.In PHP5, there are totally new and entirely rewritten extensions for manipulating XML, all based on the same libxml2 code. This common base provides interoperability between these extensions that extends the functionality of each. The tree-based parsers include SimpleXML, the DOM, and the XSLT processor. If you are familiar with the DOM from other languages, you will have an easier time coding with similar functionality in PHP than before. The stream-based parsers include the Simple API for XML (SAX) and XMLReader. SAX functions the same way it did in PHP4.You can use to manipulate an XML file. Using the DOM is efficient only when the XML file is relatively small. The advantages to using this method are the solid standard of the familiar W3C DOM, its methods, and the flexibility it brings to coding. The disadvantages of the DOM are the difficulty in coding and performance issues with large documents.

The SimpleXML extension is a tool of choice for manipulating an XML document, provided that the XML document isn't too complicated or too deep, and contains no mixed content. SimpleXML is easier to code than the DOM, as its name implies. It is far more intuitive if you work with a known document structure. Greatly increasing the flexibility of the DOM and SimpleXML the interoperative nature of the libXML2 architecture allows imports to swap formats from DOM to SimpleXML and back at will.



An interesting application of XSL is to create XML files on the fly to contain whatever data has just been selected from the database. Using this technique, it is possible to create complete Web applications where the PHP scripts are made up of XML files from database queries, then use XSL transformations to generate the actual HTML documents. This method completely splits the presentation layer from the business layer so that you can maintain either of these layers independently of the other.

File Systems and PHP



Most hosting environments are very similar, and rather predictable. Many web developers are also very predictable. It doesn't take a genius to guess that a site's includes (and most dynamic sites use an includes directory for common files) is an www.website.com/includes/. If the site owner has allowed directory listing on the server, anyone can navigate to that folder and browse files.
Imagine for a second that you have a database connection script, and you want to connect to the database from every page on your site. You might well place that in your includes folder, and call it something like connect.inc. However, this is very predictable - many people do exactly this. Worst of all, a file with the extension ".inc" is usually rendered as text and output to the browser, rather than processed as a PHP script - meaning if someone were to visit that file in a browser, they'll be given your database login information.
Placing important files in predictable places with predictable names is a recipe for disaster. Placing them outside the web root can help to lessen the risk, but is not a foolproof solution. The best way to protect your important files from vulnerabilities is to place them outside the web root, in an unusually-named folder, and to make sure that error reporting is set to off (which should make life difficult for anyone hoping to find out where your important files are kept). You should also make sure directory listing is not allowed, and that all folders have a file named "index.html" in (at least), so that nobody can ever see the contents of a folder.
Never, ever, give a file the extension ".inc". If you must have ".inc" in the extension, use the extension ".inc.php", as that will ensure the file is processed by the PHP engine (meaning that anything like a username and password is not sent to the user). Always make sure your includes folder is outside your web root, and not named something obvious. Always make sure you add a blank file named "index.html" to all folders like include or image folders - even if you deny directory listing yourself, you may one day change hosts, or someone else may alter your server configuration - if directory listing is allowed, then your index.html file will make sure the user always receives a blank page rather than the directory listing. As well, always make sure directory listing is denied on your web server (easily done with .htaccess or httpd.conf).

Advanced PHP Techniques


At the most basic level good programming is determined by whether or not an application or script works as intended. This is where the beginning programmer will leave things, and there is nothing wrong with that. However, the advanced programmer will work past that point, striving toward improved efficiency, reliability, security, and portability. This book teaches you how to develop the skills of an advanced PHP programmer.
One thing the advanced PHP programmer does better than the beginner is learning to take advantage of more obscure or harder-to-comprehend features of the language. For example, while you probably already know how to use arrays, you may not have mastered multidimensional arrays: creating them, sorting them, and so on. You have written your own functions by this point but may not understand how to use recursion and static variables. Issues like these will be discussed as well as other beyond-the-basics concepts, like the heredoc syntax and the printf()/sprintf() family of functions.