Archive for September, 2007

Apache: Forwarding Requests to Another Server

Thursday, September 27th, 2007

This caused me a headache tonight. I’ll explain below how to forward requests from one Apache HTTP server to another on your network. Here are the main characters:

Server0: Your primary HTTP webserver that all external requests come to initially.
Server1: A secondary server you have setup (with Apache) to handle requests for xyz.com. The (internal) server IP is 192.168.2.10.

At the heart of making this possible is the mod_rewrite module available with Apache. This module seems very powerful and I only understand a small portion of it. First off you need to enable mod_rewrite and mod_proxy on Server0:

Code:
a2enmod rewrite
a2enmod proxy
/etc/init.d/apache2 force-reload

mod_proxy needs to be enabled and configured (proxy.conf), otherwise Apache won’t allow mod_rewrite to forward the request. Here is the relevant portion of proxy.conf (found in /etc/apache2/mods-enabled):

Code:
<Proxy *>
   Order deny,allow
   Deny from all
</Proxy>

<Proxy http://xyz.com>
   Order deny,allow
   Deny from all
   Allow from all
</Proxy>

Now that you have the prerequisites ready, you can create the site definition on Server0 (/etc/apache2/sites-available/xyz.com):

Code:
<VirtualHost *>
   ServerName www.xyz.com
   # RewriteLog "/var/log/apache2/rewrite.log"
   # RewriteLogLevel 9
   RewriteEngine     On
   RewriteRule       ^(.*)$        http://xyz.com$1  [P]
   ServerAlias xyz.com
</VirtualHost>

Notice the rewrite rule, which will forward the request. Since xyz.com (on server0) will now point to server1, you need to update your /etc/hosts file accordingly:

Code:
192.168.2.10    xyz.com
192.168.2.10    www.xyz.com

Now enable the the new site and restart Apache:

Code:
a2ensite xyz.com
/etc/init.d/apache2 restart

Now your done with Server0. You’ll be glad to know there is nothing out of the ordinary for you to do on Server1. All you have to do is create the site definition and enable (like you normally would). So in /etc/apache2/sites-available create a site definition called xyz.com:

Code:
<VirtualHost *>
   ServerName www.xyz.com
   DocumentRoot /var/www/xyz
   ServerAlias xyz.com
</VirtualHost>

Then enable the site, restart Apache and everything should work. Odds have it something will go wrong and if it does, make sure to look in the Apache logs (/var/log/apache2/). Also, enable the rewrite log (see above). The logs will most likely always tell you exactly what went wrong.

PHP: Creating a singleton class for serialization

Thursday, September 20th, 2007

There’s a lot of examples on the internet about creating and using the singleton pattern within your application. From the PHP Docs:

The Singleton pattern applies to situations in which there needs to be a single instance of a class. The most common example of this is a database connection. Implementing this pattern allows a programmer to make this single instance easily accessible by many other objects.

Within any class you want to have use the singleton pattern, just add a getInstance method, like I have in the Obj code listing below. To access that instance use the scope resolution operator:

Code:
$obj = Obj::getInstance();

A problem arises when you want to serialize/unserialize that object from the session variable. The issue is this: When unserialize is called, PHP will attempt to reconstruct the object in question – everything will get created properly but the $instance variable will now be NULL because it would be pointing to the object reference from the previous page. So the next time you would call getInstance, the function would return an entirely new object with no serialized data. All the serialized data would exist in whatever variable the unserialize function was assigned to initially.

Once you understand this subtlety fixing the problem is easy. Create a function called setInstance, which takes in an object reference and assigns the object to the static $instance variable within the class. So once you unserialize your object from the session, immediately call setInstance to let the class know the object exists:

Code:
$obj = unserialize($_SESSION['SessionControl']);
$obj->setInstance($obj);

The full code listings follow so you can test this out. Someone out there is probably thinking, why not just called setInstance from within the __wakeup function with $this as a parameter? That won’t work, the object won’t serialize properly.

Code:
<?php

class Obj {

        static private $instance = NULL;

        public function __construct() { }

	public static function getInstance() {
		if (self::$instance == NULL) {
			self::$instance = new Obj;
		}
		return self::$instance;
	}

	public function setInstance($o) {
		self::$instance = $o;
	}

        public function __sleep() {
		return array_keys(get_object_vars($this));
       }

       public function __wakeup() { }
}

?>

Code:
<?php

require 'obj.class.php';

session_start();

if(!empty($_SESSION['SessionControl'])) {
        $obj = unserialize($_SESSION['SessionControl']);
	$obj->setInstance($obj);
} else {
	$obj = Obj::getInstance();
}

function save_session() {
	$obj = Obj::getInstance();
	$_SESSION['SessionControl'] = serialize($obj);
}

register_shutdown_function(save_session);

?>

Why I’m Against Faith-Based Education Funding

Tuesday, September 11th, 2007

Isn’t racism and hate already a big problem in our society? Do we really want our children growing up in schools were all the kids around them are exactly the same race and religion? In a surprising and somewhat depressing statement, John Tory (the conservative MPP leader) has said if elected he would give public funding to faith based schools. The elections are October 10th 2007. I know many of you despise McGuinty, but the sad reality is that both leaders have no grasp on reality and only seek to satisfy their own personal agendas. Is it time to start looking at Hampton?

It’s interesting how Tory even believes that funding for faith based schools even exists. With all the cuts that are happening, wouldn’t the money be better severed improving our existing infrastructure? I guess I’m surprised that he didn’t see the backlash that would occur by taking this stance. On the other hand, I know that the Greek Patriarch of Canada is telling us to vote Conservative cause he wants to push his Greek Schools and I hear radio advertisements on 680 news supporting faith based schools by the Jewish Congress of Canada.

The argument of funding catholic schools is a good one, but I believe we first need to stabilize our existing structure before we can start rationing what little money we have already. Furthermore, I really feel raising the future generation of children in schools were everyone is the same race and religion can’t be positive, irrespective of what the catholic’s have.

Clio-maria has a good post on this topic.
I started a Facebook group about this.

PHP: Including scripts from within class functions

Monday, September 3rd, 2007

This surprised me today: Using PHP you can include scripts from within a class functions and access the included functions globally. It seems to go against what I feel should occur, but it works perfectly fine. I posted on a discussion forum at devshed to see if anyone has some insight into this.

temp_include.php:

Code:
<?php

function test_funct() {
    echo "function called";
}

?>

Main script:

Code:
<?php

Class ABC {
    public function __construct() {
        require_once 'temp_include.php';
    }
}

$o = new ABC();
test_funct();

?>

Devastation from Greece

Saturday, September 1st, 2007

Your Ad Here