Friday, January 23, 2009

AJAX: Reading HTML

AJAX. Simple AJAX code to read HTML file and display in div.

<script> var obj; function readHTML(url) { if( window.XMLHttpRequest ) { obj = new XMLHttpRequest(); obj.onreadystatechange = processChange;"GET", url, true); obj.send(null); } else if( window.ActiveXObject ) { obj = new ActiveXObject("Microsoft.XMLHTTP");"GET", url, true); obj.send(); } else { alert("Your browser does not support AJAX"); } } function processChange() { if( (obj.readyState==4) && (obj.status==200) ) { var myDiv = document.getElementById("readhtmlid"); myDiv.innerHTML = obj.responseText; } else { alert("something went wrong."); } } readHTML("http://url/css.html"); </script> <div id="readhtmlid"></div>

Saturday, October 4, 2008

Troubleshooting XML DOM in PHP

DOM.If you have php5 and apache2 installed and DOM is enabled according to phpinfo, but you get following error when you use domxml_open_file($file), I get following error

Fatal error: Call to undefined function domxml_open_file() in …

This is due to domxml_open_file is DOM XML function. DOM XML is now moved to the PECL repository and no longer shipped with PHP 5. If you need DOM functions, you need to use DOM.

Installing LAMP on Ubuntu

Installing LAMP. If you want to use apt-get install the LAMP layer,

$ sudo apt-get install apache2 php5-mysql libapache2-mod-php5 mysql-server

Or you can use one taskseltry command:

$ sudo tasksel install lamp-server

Configuring MySQL. Set password for MySQL. tasksel will ask you for root password during installation.

$ mysql -u root mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('lamepassword');

Creating database and user

mysql> CREATE DATABASE lamedatabase; mysql> GRANT ALL PRIVILEGES ON lamedatabase.* TO 'lameuser'@'localhost' IDENTIFIED BY 'anotherlamepassword'; mysql> FLUSH PRIVILEGES;

Configuring Apache. If you get following error when you start apache2 server,

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

you need to add ServerName localhost in apache2 configuration file.

Wednesday, May 7, 2008

My First Digg API Application

Here is my very first Python program that I've ever wrote outside of a classroom. Basically, it display informations of all Digg friends.

One thing I need to figure out is how to handle non-ASCII characters. My code throws exception when it encounter Unicode or non-printable characters in full name.

import urllib
import xml.etree.ElementTree as ET

# You need to changes following lines 
uname = 'myusername'
appkey = urllib.quote('')

# ---------------------------------------------------------

count = 100
offset = 0
class AppURLopener(urllib.FancyURLopener):
  version = "My-Application/1.0"
urllib._urlopener = AppURLopener()
optstr = "&count=" + str(count) + "&offset=" + str(offset)
diggurl = '' + uname + \
            '/friends' + '?appkey=' + appkey + optstr;
diggxml = urllib.urlopen(diggurl).read()
et = ET.fromstring(diggxml)
nfriend = int(et.attrib["total"])

while True:

  for e in et:
    name = e.get("name", "")
    icon = e.get("icon", "")
    registered = e.get("registered", "")
    profileviews = e.get("profileviews", "")
    # Some of names contains non-ASCII character which
    # I still having figure out how to handle it
    # fullname = e.get("fullname", "")
    fullname = ""
    mutual = e.get("mutual", "")
    date = e.get("date", "")  
    print name + "|" + icon + "|" + registered + "|" + \
            profileviews + "|" + fullname + "|" +      \
            mutual + "|" + date
  offset += count
  if offset >= nfriend:
  optstr = "&count=" + str(count) + "&offset=" + \
  diggurl = '' + uname +   \
            '/friends' + '?appkey=' + appkey + optstr;
  diggxml = urllib.urlopen(diggurl).read()
  et = ET.fromstring(diggxml)

Thursday, February 21, 2008

CGI with Perl

Basic. Simple example using CGI module:

#!/usr/bin/perl use CGI qw(:standard); print "Content-type: text/html\n\n"; print "<html>\n"; $name = param("name"); print "<body>$name</body>\n"; print "</html>";

Wednesday, February 20, 2008

Ubuntu Apache Virtual hosts

Virtual Hosts in Ubuntu. The easiest way to create new virtual host is to create a configuration file in /etc/apache2/sites-enabled. In this directory, you should see a symbolic link to the default virtual host located on /etc/apache2/sites-available/ if default host is enabled.

Usually, you want to do

  1. cd /etc/apache2/sites-available
  2. sudo vim virtualdomain.conf
  3. Write or copy virtual host configuration (see example below)
  4. sudo vim /etc/hosts
  5. Point virtual domain to localhost. For example: localhost virtualdomain
  6. sudo a2ensite virtualdomain.conf
  7. sudo /etc/init.d/apache2 reload

a2ensite is a script that creates the symlink to /etc/apache2/sites-available/virtualdoain.conf in /etc/apache2/site-enabled directory. This allows to allows quickly disable or enable the virtual host without deleting or moving the configuration file.

Example of virtual host using IP and Port Note that if the server is not listen to port 81, you need to put Listen 81.

Listen 81 NameVirtualHost <VirtualHost> ServerAdmin webmaster@localhost DocumentRoot /home/auser/public_html <Directory /> Options FollowSymLinks AllowOverride None </Directory> ErrorLog /home/auser/log/apache2/error.log LogLevel warn CustomLog /home/auser/log/apache2/access.log combined ServerSignature On </VirtualHost>

Tuesday, February 12, 2008

Ubuntu Apache ServerName Warning

ServerName Warning. If you get following warning message:

apache2: Could not determine the server's fully qualified domain name, using for ServerName

Make sure to add ServerName theservername in /etc/apache2/apache2.conf.

If ServerName directive is not set, then the default virtual host will respond to all request. Hens, the web site will work but the apache2 will return warning message during /etc/init.d/apache2 restart.

Tips. If you want your server to respond to with/without www. prefix, try ServerAlias directive.