Creating XML Document with DOM

As we go through the articles, we would be build a small library of functions which would prove to be very handy when you would start XML development.
php_dom_xml_library.php

function createFile()
{
    // create new dom document
    $xml = new DOMDocument();
    
    // save dom document to an xml file
    $xml->save('out.xml');
}

// call xml function
createFile();

output:

<?xml version="1.0"?>



XML DOM and PHP

XML is a markup language created to facilitate communication between different systems. Most implementations of XML serve to facilitate machine-to-machine communication.

Document Object Model (DOM) is an API which defines the logical structure and the ways to access and manipulate XML documents. XML presents data as documents. DOM is used to access and manipulate the data in XML documents.



PHP Error Logs

PHP errors are by default set to go to the apache error log. On Ubuntu, the error log is located at /var/log/apache2/error.log. If you are a PHP developer, it is good idea to open a small terminal window and type the following:

tail -f /var/log/apache2/error.log

This would essentially give you a live view of the error log. As you generate errors, you would see the error messages on the terminal.



Finding IP address on Linux

To find IP address of your system on Linux, simply type:

/sbin/ifconfig

It would return IP address, inet6 address, mask, etc.



without OIDS

All you need to know is that you should choose the option "without OIDS". If you wish to understand why, continue reading. OIDS are used by PostgreSQL's system tables. They refer to tables, types of data, etc. OIDS were created for system tables only, and should not be used for user tables. Unfortunately, some coders started using OIDs as default primary keys. If PostgreSQL removes OIDs, the applications coded by these guys would crash. So for the sake of backward compatibility, there is now an option "without OID".



Reading file contents to an array and string

PHP allows you to easily read your file contents into an array or a string. To read into a string, use file_get_contents() function. To read into an array, use file() function. The file function would copy each line of content into a different array element.

Grocery List

bread
eggs
milk

grocery_list.txt

PHP code

file() returns contents of file as array
file_get_contents() returns contents of file as string
Array ( [0] => bread [1] => eggs [2] => milk )
bread eggs milk 



Converting tabs to spaces

Tabs often breakup when the source code is moved between different system. In certain cases, tabs are not recognized and you get ugly looking symbols. The best way to avoid this frustration is to insert spaces instead of tabs. Obviously, you wouldn't want to hit the space tab 20 times on each line. The solution is to set the tab to insert predefined number of spaces instead of a tab. In vi, you can do this by setting expandtab as follows:

:set expandtab

To control the number of spaces, you still need to use tabstop as follows:



Encrpypting and Decrypting in PHP

The crypt() function provide one-way encryption. Using one-way encryption is like using a key to lock and unlock something. The key is your password.

To encrypt:
$pass = 'secret';
$encrypted = crypt($pass);
// $encrypted = $1$zaxz8vXb$.lZaoK40w/EtrkkogORYo0

To decrypt:
// entered password
$pass = 'secret';
// $passord is the encrypted password
if (crypt($pass, $encrypted) == $encrypted) {
print 'welcome';
} else {
print 'wrong password';
}



Ubuntu: could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName

Error: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName

Solution:
1. Open httpd.conf for editing

sudo vi /etc/apache2/httpd.conf

You might be surprised to see that the file is empty
2. type the following:

ServerName localhost

3. Save file and restart apache server

service apache2 restart



Installing phpPgAdmin on Ubuntu

phpPgAdmin is a web-based administration tool for PostgreSQL written in PHP. It is a very nifty tool with a very convenient GUI interface. If you have used phpMyAdmin for MySQL, this phpMyAdmin for PostgreSQL.

To install phpPgAdmin on Ubuntu 10.x, to the following:
1. sudo apt-get install phppgadmin
2. open your browser and type http://localhost/phppgadmin
3. Login and start using phpPgAdmin

If you see the error: Login disallowed for security reasons, do the following:

Syndicate content